-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertbulk_bench_test.go
More file actions
95 lines (85 loc) · 2.3 KB
/
Copy pathinsertbulk_bench_test.go
File metadata and controls
95 lines (85 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package sqlpro
import (
"database/sql"
"fmt"
"testing"
"time"
)
// benchBulkRow mirrors a typical fylr bulk-insert row: integer pk (omitempty,
// auto-assigned) plus 6 data columns, half strings, half numbers.
type benchBulkRow struct {
A int64 `db:"a,pk,omitempty"`
B string `db:"b"`
C string `db:"c"`
D int64 `db:"d"`
E float64 `db:"e"`
F string `db:"f"`
G int64 `db:"g"`
}
func benchBulkRows(n int) []*benchBulkRow {
rows := make([]*benchBulkRow, 0, n)
for i := 0; i < n; i++ {
rows = append(rows, &benchBulkRow{
B: fmt.Sprintf("object-%d", i),
C: "some text value with a 'quote' in it",
D: int64(i),
E: float64(i) * 1.5,
F: "kilo",
G: int64(i * 7),
})
}
return rows
}
// BenchmarkInsertBulkPrepFake measures InsertBulk of 5000 rows x 6 columns
// against the no-op fake driver, isolating the sqlpro-side preparation work
// (row materialization + SQL literal building) from real database cost.
func BenchmarkInsertBulkPrepFake(b *testing.B) {
backend := &fakeBackend{}
conn := sql.OpenDB(&fakeConnector{backend: backend})
defer conn.Close()
wrapper := newSqlPro(conn)
wrapper.sqlDB = conn
wrapper.driver = SQLITE3
wrapper.timeFormat = time.RFC3339Nano
const n = 5000
rows := benchBulkRows(n)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
backend.queueExec(int64(n), 0)
if err := wrapper.InsertBulk("bench_bulk", rows); err != nil {
b.Fatal(err)
}
// drop the recorded statement so memory does not grow with b.N
backend.mu.Lock()
backend.statements = backend.statements[:0]
backend.mu.Unlock()
}
}
// BenchmarkInsertBulkSQLite measures the same InsertBulk end-to-end through
// real SQLite (the non-postgres production path).
func BenchmarkInsertBulkSQLite(b *testing.B) {
if err := dbConn.Exec(`DROP TABLE IF EXISTS bench_bulk`); err != nil {
b.Fatal(err)
}
err := dbConn.Exec(`CREATE TABLE bench_bulk(
a INTEGER PRIMARY KEY AUTOINCREMENT,
b TEXT, c TEXT, d INTEGER, e REAL, f TEXT, g INTEGER)`)
if err != nil {
b.Fatal(err)
}
const n = 5000
rows := benchBulkRows(n)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := dbConn.InsertBulk("bench_bulk", rows); err != nil {
b.Fatal(err)
}
b.StopTimer()
if err := dbConn.Exec("DELETE FROM bench_bulk"); err != nil {
b.Fatal(err)
}
b.StartTimer()
}
}