Skip to content

Commit

Permalink
bench: add a benchmark for update with assigment casts
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
yuzefovich committed Sep 26, 2022
1 parent eaa0f86 commit 6dab55f
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ func BenchmarkSQL(b *testing.B) {
runBenchmarkInsertSecondaryIndex,
runBenchmarkTrackChoices,
runBenchmarkUpdate,
runBenchmarkUpdateWithAssignmentCast,
runBenchmarkUpsert,
} {
fnName := runtime.FuncForPC(reflect.ValueOf(runFn).Pointer()).Name()
Expand Down Expand Up @@ -572,6 +573,44 @@ func runBenchmarkUpdate(b *testing.B, db *sqlutils.SQLRunner, count int) {
b.StopTimer()
}

// runBenchmarkUpdateWithAssignmentCast benchmarks updating count random rows in
// a table where we need to perform an assigment cast to get the updated values.
func runBenchmarkUpdateWithAssignmentCast(b *testing.B, db *sqlutils.SQLRunner, count int) {
defer func() {
db.Exec(b, `DROP TABLE IF EXISTS bench.update`)
}()

const rows = 10000
db.Exec(b, `CREATE TABLE bench.update (k INT PRIMARY KEY, v INT)`)

var buf bytes.Buffer
buf.WriteString(`INSERT INTO bench.update VALUES `)
for i := 0; i < rows; i++ {
if i > 0 {
buf.WriteString(", ")
}
fmt.Fprintf(&buf, "(%d, %d)", i, i)
}
db.Exec(b, buf.String())

s := rand.New(rand.NewSource(5432))

b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
buf.WriteString(`UPDATE bench.update SET v = (v + 1.0)::FLOAT WHERE k IN (`)
for j := 0; j < count; j++ {
if j > 0 {
buf.WriteString(", ")
}
fmt.Fprintf(&buf, `%d`, s.Intn(rows))
}
buf.WriteString(`)`)
db.Exec(b, buf.String())
}
b.StopTimer()
}

// runBenchmarkUpsert benchmarks upserting count rows in a table.
func runBenchmarkUpsert(b *testing.B, db *sqlutils.SQLRunner, count int) {
defer func() {
Expand Down

0 comments on commit 6dab55f

Please sign in to comment.