-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
234 lines (212 loc) · 6.74 KB
/
Copy pathbenchmark_test.go
File metadata and controls
234 lines (212 loc) · 6.74 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Benchmarks mirroring pg_query_go v6.2.2's benchmark_test.go: same
// benchmark names, same inputs, so `benchstat` can diff a run of this file
// against a run of oracle/benchmark_test.go (the cgo twin) directly.
//
// The Stress benchmarks use the largest corpus input — the 1.1 MB
// multi-VALUES INSERT (fingerprint suite, case 073) — the same query
// PLAN.md's milestone 12 names for memory profiling.
package oliphant_test
import (
"testing"
pg_query "github.com/sqlc-dev/oliphant"
"github.com/sqlc-dev/oliphant/internal/testfile"
"github.com/sqlc-dev/oliphant/parser"
)
// Prevent compiler optimizations by assigning all results to global variables
// (same trick as upstream's benchmark file).
var (
benchErr error
resultStr []byte
resultS string
resultRes *pg_query.ParseResult
)
const stressCaseFile = "parser/testdata/fingerprint/libpg_query.test"
// stressInput returns the 1.1 MB INSERT (case 073 of the fingerprint suite).
func stressInput(b *testing.B) string {
b.Helper()
cases, err := testfile.Read(stressCaseFile)
if err != nil {
b.Fatal(err)
}
big := 0
for i := range cases {
if len(cases[i].Input) > len(cases[big].Input) {
big = i
}
}
return cases[big].Input
}
func benchmarkParse(input string, b *testing.B) {
for i := 0; i < b.N; i++ {
resultRes, benchErr = pg_query.Parse(input)
if benchErr != nil {
b.Errorf("Benchmark produced error %s\n\n", benchErr)
}
}
}
func benchmarkParseParallel(input string, b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := pg_query.Parse(input)
if err != nil {
b.Errorf("Benchmark produced error %s\n\n", err)
}
}
})
}
func benchmarkRawParse(input string, b *testing.B) {
for i := 0; i < b.N; i++ {
resultStr, benchErr = parser.ParseToProtobuf(input)
if benchErr != nil {
b.Errorf("Benchmark produced error %s\n\n", benchErr)
}
if len(resultStr) == 0 {
b.Errorf("Benchmark produced empty result\n\n")
}
}
}
func benchmarkRawParseParallel(input string, b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var str []byte
var err error
for pb.Next() {
str, err = parser.ParseToProtobuf(input)
if err != nil {
b.Errorf("Benchmark produced error %s\n\n", err)
}
if len(str) == 0 {
b.Errorf("Benchmark produced empty result\n\n")
}
}
})
}
func benchmarkParseToJSON(input string, b *testing.B) {
for i := 0; i < b.N; i++ {
resultS, benchErr = pg_query.ParseToJSON(input)
if benchErr != nil {
b.Errorf("Benchmark produced error %s\n\n", benchErr)
}
}
}
func benchmarkScan(input string, b *testing.B) {
for i := 0; i < b.N; i++ {
_, benchErr = pg_query.Scan(input)
if benchErr != nil {
b.Errorf("Benchmark produced error %s\n\n", benchErr)
}
}
}
func benchmarkFingerprint(input string, b *testing.B) {
var str string
for i := 0; i < b.N; i++ {
str, benchErr = pg_query.Fingerprint(input)
if benchErr != nil {
b.Errorf("Benchmark produced error %s\n\n", benchErr)
}
if str == "" {
b.Errorf("Benchmark produced empty result\n\n")
}
}
}
func benchmarkNormalize(input string, b *testing.B) {
for i := 0; i < b.N; i++ {
resultS, benchErr = pg_query.Normalize(input)
if benchErr != nil {
b.Errorf("Benchmark produced error %s\n\n", benchErr)
}
if resultS == "" {
b.Errorf("Benchmark produced empty result\n\n")
}
}
}
func BenchmarkParseSelect1(b *testing.B) {
benchmarkParse("SELECT 1", b)
}
func BenchmarkParseSelect2(b *testing.B) {
benchmarkParse("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b)
}
func BenchmarkParseCreateTable(b *testing.B) {
benchmarkParse("CREATE TABLE types (a float(2), b float(49), c NUMERIC(2, 3), d character(4), e char(5), f varchar(6), g character varying(7))", b)
}
func BenchmarkParseSelect1Parallel(b *testing.B) {
benchmarkParseParallel("SELECT 1", b)
}
func BenchmarkParseSelect2Parallel(b *testing.B) {
benchmarkParseParallel("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b)
}
func BenchmarkParseCreateTableParallel(b *testing.B) {
benchmarkParseParallel("CREATE TABLE types (a float(2), b float(49), c NUMERIC(2, 3), d character(4), e char(5), f varchar(6), g character varying(7))", b)
}
func BenchmarkRawParseSelect1(b *testing.B) {
benchmarkRawParse("SELECT 1", b)
}
func BenchmarkRawParseSelect2(b *testing.B) {
benchmarkRawParse("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b)
}
func BenchmarkRawParseCreateTable(b *testing.B) {
benchmarkRawParse("CREATE TABLE types (a float(2), b float(49), c NUMERIC(2, 3), d character(4), e char(5), f varchar(6), g character varying(7))", b)
}
func BenchmarkRawParseSelect1Parallel(b *testing.B) {
benchmarkRawParseParallel("SELECT 1", b)
}
func BenchmarkRawParseSelect2Parallel(b *testing.B) {
benchmarkRawParseParallel("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b)
}
func BenchmarkRawParseCreateTableParallel(b *testing.B) {
benchmarkRawParseParallel("CREATE TABLE types (a float(2), b float(49), c NUMERIC(2, 3), d character(4), e char(5), f varchar(6), g character varying(7))", b)
}
func BenchmarkFingerprintSelect1(b *testing.B) {
benchmarkFingerprint("SELECT 1", b)
}
func BenchmarkFingerprintSelect2(b *testing.B) {
benchmarkFingerprint("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b)
}
func BenchmarkFingerprintCreateTable(b *testing.B) {
benchmarkFingerprint("CREATE TABLE types (a float(2), b float(49), c NUMERIC(2, 3), d character(4), e char(5), f varchar(6), g character varying(7))", b)
}
func BenchmarkNormalizeSelect1(b *testing.B) {
benchmarkNormalize("SELECT 1", b)
}
func BenchmarkNormalizeSelect2(b *testing.B) {
benchmarkNormalize("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b)
}
func BenchmarkNormalizeCreateTable(b *testing.B) {
benchmarkNormalize("CREATE TABLE types (a float(2), b float(49), c NUMERIC(2, 3), d character(4), e char(5), f varchar(6), g character varying(7))", b)
}
// --- beyond the upstream set ---
func BenchmarkParseToJSONSelect2(b *testing.B) {
benchmarkParseToJSON("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b)
}
func BenchmarkScanSelect2(b *testing.B) {
benchmarkScan("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b)
}
func BenchmarkRawParseStress(b *testing.B) {
input := stressInput(b)
b.SetBytes(int64(len(input)))
b.ResetTimer()
benchmarkRawParse(input, b)
}
func BenchmarkRawParseStressParallel(b *testing.B) {
input := stressInput(b)
b.SetBytes(int64(len(input)))
b.ResetTimer()
benchmarkRawParseParallel(input, b)
}
func BenchmarkFingerprintStress(b *testing.B) {
input := stressInput(b)
b.SetBytes(int64(len(input)))
b.ResetTimer()
benchmarkFingerprint(input, b)
}
func BenchmarkNormalizeStress(b *testing.B) {
input := stressInput(b)
b.SetBytes(int64(len(input)))
b.ResetTimer()
benchmarkNormalize(input, b)
}
func BenchmarkScanStress(b *testing.B) {
input := stressInput(b)
b.SetBytes(int64(len(input)))
b.ResetTimer()
benchmarkScan(input, b)
}