Skip to content

Commit

Permalink
Add benchmark to receive massive rows. (#1415)
Browse files Browse the repository at this point in the history
  • Loading branch information
methane authored May 7, 2023
1 parent 736b6fa commit 081308f
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,59 @@ func BenchmarkQueryRawBytes(b *testing.B) {
})
}
}

// BenchmarkReceiveMassiveRows measures performance of receiving large number of rows.
func BenchmarkReceiveMassiveRows(b *testing.B) {
// Setup -- prepare 10000 rows.
db := initDB(b,
"DROP TABLE IF EXISTS foo",
"CREATE TABLE foo (id INT PRIMARY KEY, val TEXT)")
defer db.Close()

sval := strings.Repeat("x", 50)
stmt, err := db.Prepare(`INSERT INTO foo (id, val) VALUES (?, ?)` + strings.Repeat(",(?,?)", 99))
if err != nil {
b.Errorf("failed to prepare query: %v", err)
return
}
for i := 0; i < 10000; i += 100 {
args := make([]any, 200)
for j := 0; j < 100; j++ {
args[j*2] = i + j
args[j*2+1] = sval
}
_, err := stmt.Exec(args...)
if err != nil {
b.Error(err)
return
}
}
stmt.Close()

// Use b.Run() to skip expensive setup.
b.Run("query", func(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
rows, err := db.Query(`SELECT id, val FROM foo`)
if err != nil {
b.Errorf("failed to select: %v", err)
return
}
for rows.Next() {
var i int
var s sql.RawBytes
err = rows.Scan(&i, &s)
if err != nil {
b.Errorf("failed to scan: %v", err)
_ = rows.Close()
return
}
}
if err = rows.Err(); err != nil {
b.Errorf("failed to read rows: %v", err)
}
_ = rows.Close()
}
})
}

0 comments on commit 081308f

Please sign in to comment.