Skip to content

Commit

Permalink
🐛 Support reused connection for better performance. (#753)
Browse files Browse the repository at this point in the history
Fixes: https://issues.redhat.com/browse/MTA-4007

---------

Signed-off-by: Jeff Ortel <jortel@redhat.com>
  • Loading branch information
jortel authored Oct 10, 2024
1 parent 8769075 commit 72fd8df
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ endif
test:
go test -count=1 -v $(shell go list ./... | grep -v "hub/test")

test-db:
go test -count=1 -timeout=6h -v ./database...

# Run Hub REST API tests.
test-api:
HUB_BASE_URL=$(HUB_BASE_URL) go test -count=1 -p=1 -v -failfast ./test/api/...
Expand Down
76 changes: 74 additions & 2 deletions database/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,48 @@ import (
"testing"
"time"

"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/model"
"gorm.io/gorm"
"k8s.io/utils/env"
)

var N, _ = env.GetInt("TEST_CONCURRENT", 10)

func TestDriver(t *testing.T) {
pid := os.Getpid()
Settings.DB.Path = fmt.Sprintf("/tmp/driver-%d.db", pid)
defer func() {
_ = os.Remove(Settings.DB.Path)
}()
db, err := Open(true)
if err != nil {
panic(err)
}
key := "driver"
m := &model.Setting{Key: key, Value: "Test"}
// insert.
err = db.Create(m).Error
if err != nil {
panic(err)
}
// update
err = db.Save(m).Error
if err != nil {
panic(err)
}
// select
err = db.First(m, m.ID).Error
if err != nil {
panic(err)
}
// delete
err = db.Delete(m).Error
if err != nil {
panic(err)
}
}

func TestConcurrent(t *testing.T) {
pid := os.Getpid()
Settings.DB.Path = fmt.Sprintf("/tmp/concurrent-%d.db", pid)
Expand All @@ -23,12 +58,35 @@ func TestConcurrent(t *testing.T) {
if err != nil {
panic(err)
}

type A struct {
model.Model
}

type B struct {
N int
model.Model
A A
AID uint
}
err = db.Migrator().AutoMigrate(&A{}, &B{})
if err != nil {
panic(err)
}

a := A{}
err = db.Create(&a).Error
if err != nil {
panic(err)
}

dq := make(chan int, N)
for w := 0; w < N; w++ {
go func(id int) {
fmt.Printf("Started %d\n", id)
for n := 0; n < N*10; n++ {
m := &model.Setting{Key: fmt.Sprintf("key-%d-%d", id, n), Value: n}
for n := 0; n < N*100; n++ {
m := &B{N: n, A: a}
m.CreateUser = "Test"
fmt.Printf("(%.4d) CREATE: %.4d\n", id, n)
uErr := db.Create(m).Error
if uErr != nil {
Expand All @@ -45,6 +103,20 @@ func TestConcurrent(t *testing.T) {
panic(uErr)
}
}
for i := 0; i < 10; i++ {
fmt.Printf("(%.4d) LIST: %.4d/%.4d\n", id, n, i)
page := api.Page{}
cursor := api.Cursor{}
mx := B{}
dbx := db.Model(mx)
dbx = dbx.Joins("A")
dbx = dbx.Limit(10)
cursor.With(dbx, page)
for cursor.Next(&mx) {
time.Sleep(time.Millisecond + 10)
fmt.Printf("(%.4d) NEXT: %.4d/%.4d ID=%d\n", id, n, i, mx.ID)
}
}
for i := 0; i < 4; i++ {
uErr = db.Transaction(func(tx *gorm.DB) (err error) {
time.Sleep(time.Millisecond * 10)
Expand Down
Loading

0 comments on commit 72fd8df

Please sign in to comment.