-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_bench_test.go
More file actions
122 lines (114 loc) · 3.51 KB
/
Copy pathexec_bench_test.go
File metadata and controls
122 lines (114 loc) · 3.51 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package sqlpro
import (
"database/sql"
"testing"
)
// newBenchFakeDB returns a sqlpro wrapper connected to the in-package fake
// driver (fakedb_test.go), SQLITE3-mode, without needing a *testing.T.
func newBenchFakeDB() (*db, *fakeBackend) {
backend := &fakeBackend{}
conn := sql.OpenDB(&fakeConnector{backend: backend})
wrapper := newSqlPro(conn)
wrapper.sqlDB = conn
wrapper.driver = SQLITE3
return wrapper, backend
}
// benchExecRows builds n rows of the 16-column benchScanRow with the pk set
// non-zero, so every Insert/Update sees the identical 16-column value set.
func benchExecRows(n int) []*benchScanRow {
rows := make([]*benchScanRow, 0, n)
for k := 0; k < n; k++ {
rows = append(rows, &benchScanRow{
A: int64(k + 1),
B: "beta", C: "gamma", D: int64(k), E: int64(k * 2), F: "foxtrot",
G: "golf", H: int64(k), I: int64(k), J: "juliet", K: "kilo",
L: int64(k), M: "mike", N: int64(k), O: "oscar", P: "papa",
})
}
return rows
}
// BenchmarkInsertClauseFromValues isolates the per-row INSERT SQL build
// (exec.go insertClauseFromValues), the function InsertContext calls once per
// row of a slice.
func BenchmarkInsertClauseFromValues(b *testing.B) {
dbh, _ := newBenchFakeDB()
row := *benchExecRows(1)[0]
values, info, err := dbh.valuesFromStruct(row)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, _, err := dbh.insertClauseFromValues("bench", values, info); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkUpdateClauseFromRow isolates the per-row UPDATE SQL build
// (exec.go updateClauseFromRow), incl. its internal valuesFromStruct.
func BenchmarkUpdateClauseFromRow(b *testing.B) {
dbh, _ := newBenchFakeDB()
row := *benchExecRows(1)[0]
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, _, err := dbh.updateClauseFromRow("bench", row); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkInsertSlice100Fake measures InsertContext over a slice of 100
// structs end-to-end through the fake driver: per-row valuesFromStruct +
// insertClauseFromValues + replaceArgs + database/sql Exec.
func BenchmarkInsertSlice100Fake(b *testing.B) {
dbh, backend := newBenchFakeDB()
rows := benchExecRows(100)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := dbh.Insert("bench", rows); err != nil {
b.Fatal(err)
}
backend.mu.Lock()
backend.statements = nil
backend.mu.Unlock()
}
}
// BenchmarkUpdateSlice100Fake measures UpdateContext over a slice of 100
// structs end-to-end through the fake driver.
func BenchmarkUpdateSlice100Fake(b *testing.B) {
dbh, backend := newBenchFakeDB()
rows := benchExecRows(100)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := dbh.Update("bench", rows); err != nil {
b.Fatal(err)
}
backend.mu.Lock()
backend.statements = nil
backend.mu.Unlock()
}
}
// TestInsertSQLDistinct reports how many distinct SQL texts 300 single-row
// INSERT builds of the same struct produce. Every distinct text is a separate
// pgx prepared-statement cache entry (QueryExecModeCacheStatement keys on the
// exact SQL string).
func TestInsertSQLDistinct(t *testing.T) {
dbh, _ := newBenchFakeDB()
row := *benchExecRows(1)[0]
values, info, err := dbh.valuesFromStruct(row)
if err != nil {
t.Fatal(err)
}
distinct := map[string]bool{}
for i := 0; i < 300; i++ {
s, _, err := dbh.insertClauseFromValues("bench", values, info)
if err != nil {
t.Fatal(err)
}
distinct[s] = true
}
t.Logf("distinct INSERT SQL strings over 300 builds of one struct: %d", len(distinct))
}