Skip to content

Commit

Permalink
Add benchmark for BatchInsertBuilder (stellar#4086)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored and erika-sdf committed Dec 3, 2021
1 parent f7f9b2a commit 8985a82
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
51 changes: 45 additions & 6 deletions support/db/batch_insert_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"fmt"
"testing"

"github.com/stellar/go/support/db/dbtest"
Expand All @@ -20,6 +21,44 @@ type invalidHungerRow struct {
LastName string `db:"last_name"`
}

func BenchmarkBatchInsertBuilder(b *testing.B) {
b.StopTimer()
// In order to show SQL queries
// log.SetLevel(logrus.DebugLevel)
db := dbtest.Postgres(b).Load(testSchema)
defer db.Close()
sess := &Session{DB: db.Open()}
defer sess.DB.Close()
ctx := context.Background()
maxBatchSize := 1000
insertBuilder := &BatchInsertBuilder{
Table: sess.GetTable("people"),
MaxBatchSize: maxBatchSize,
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < maxBatchSize; j++ {
err := insertBuilder.RowStruct(ctx, hungerRow{
Name: fmt.Sprintf("bubba%d", i*maxBatchSize+j),
HungerLevel: "1202",
})
require.NoError(b, err)
}
}
err := insertBuilder.Exec(ctx)

b.StopTimer()
assert.NoError(b, err)
var count []int
err = sess.SelectRaw(ctx,
&count,
"SELECT COUNT(*) FROM people",
)
assert.NoError(b, err)
preexistingCount := 3
assert.Equal(b, b.N*maxBatchSize+preexistingCount, count[0])
}

func TestBatchInsertBuilder(t *testing.T) {
db := dbtest.Postgres(t).Load(testSchema)
defer db.Close()
Expand Down Expand Up @@ -87,8 +126,8 @@ func TestBatchInsertBuilder(t *testing.T) {
t,
found,
[]person{
person{Name: "bubba", HungerLevel: "120"},
person{Name: "bubba2", HungerLevel: "1202"},
{Name: "bubba", HungerLevel: "120"},
{Name: "bubba2", HungerLevel: "1202"},
},
)

Expand Down Expand Up @@ -122,8 +161,8 @@ func TestBatchInsertBuilder(t *testing.T) {
t,
found,
[]person{
person{Name: "bubba", HungerLevel: "120"},
person{Name: "bubba2", HungerLevel: "1202"},
{Name: "bubba", HungerLevel: "120"},
{Name: "bubba2", HungerLevel: "1202"},
},
)

Expand All @@ -145,8 +184,8 @@ func TestBatchInsertBuilder(t *testing.T) {
t,
found,
[]person{
person{Name: "bubba2", HungerLevel: "1202"},
person{Name: "bubba", HungerLevel: "1"},
{Name: "bubba2", HungerLevel: "1202"},
{Name: "bubba", HungerLevel: "1"},
},
)
}
6 changes: 3 additions & 3 deletions support/db/dbtest/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DB struct {
Dialect string
DSN string
dbName string
t *testing.T
t testing.TB
closer func()
closed bool
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (db *DB) Version() (major int) {
return major
}

func execStatement(t *testing.T, pguser, query string) {
func execStatement(t testing.TB, pguser, query string) {
db, err := sqlx.Open("postgres", fmt.Sprintf("postgres://%s@localhost/?sslmode=disable", pguser))
require.NoError(t, err)
_, err = db.Exec(query)
Expand All @@ -113,7 +113,7 @@ func execStatement(t *testing.T, pguser, query string) {
// of the running process. It assumes that you have postgres running on the
// default port, have the command line postgres tools installed, and that the
// current user has access to the server. It panics on the event of a failure.
func Postgres(t *testing.T) *DB {
func Postgres(t testing.TB) *DB {
var result DB
result.dbName = randomName()
result.Dialect = "postgres"
Expand Down

0 comments on commit 8985a82

Please sign in to comment.