From 2f7d3713613b2e9cbedbe0407d4d441cde5b05e1 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 19 Apr 2023 01:09:58 +0900 Subject: [PATCH] Add benchmark to receive massive rows. --- benchmark_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/benchmark_test.go b/benchmark_test.go index 97ed781f8..7c2731a58 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -372,3 +372,49 @@ func BenchmarkQueryRawBytes(b *testing.B) { }) } } + +// BenchmarkReceiveMassiveRows measures performance of receiving large number of rows. +func BenchmarkReceiveMassiveRows(b *testing.B) { + db := initDB(b, + "DROP TABLE IF EXISTS foo", + "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))") + defer db.Close() + + sval := strings.Repeat("x", 40) + stmt, err := db.Prepare(`INSERT INTO foo (id, val) VALUES (?, ?)` + strings.Repeat(",(?,?)", 9)) + if err != nil { + b.Errorf("failed to prepare query: %v", err) + return + } + for i := 0; i < 10_000; i += 10 { + _, err := stmt.Exec( + i, sval, i+1, sval, i+2, sval, i+3, sval, i+4, sval, i+5, sval, + i+6, sval, i+7, sval, i+8, sval, i+9, sval) + if err != nil { + b.Error(err) + return + } + } + + 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 + } + } + _ = rows.Close() + } + }) +}