Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Support reused connection for better performance. #753

Merged
merged 9 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading