|
| 1 | +// Benchmarks mirroring pg_query_go v6.2.2's benchmark_test.go: same |
| 2 | +// benchmark names, same inputs, so `benchstat` can diff a run of this file |
| 3 | +// against a run of oracle/benchmark_test.go (the cgo twin) directly. |
| 4 | +// |
| 5 | +// The Stress benchmarks use the largest corpus input — the 1.1 MB |
| 6 | +// multi-VALUES INSERT (fingerprint suite, case 073) — the same query |
| 7 | +// PLAN.md's milestone 12 names for memory profiling. |
| 8 | +package oliphant_test |
| 9 | + |
| 10 | +import ( |
| 11 | + "testing" |
| 12 | + |
| 13 | + pg_query "github.com/sqlc-dev/oliphant" |
| 14 | + "github.com/sqlc-dev/oliphant/internal/testfile" |
| 15 | + "github.com/sqlc-dev/oliphant/parser" |
| 16 | +) |
| 17 | + |
| 18 | +// Prevent compiler optimizations by assigning all results to global variables |
| 19 | +// (same trick as upstream's benchmark file). |
| 20 | +var ( |
| 21 | + benchErr error |
| 22 | + resultStr []byte |
| 23 | + resultS string |
| 24 | + resultRes *pg_query.ParseResult |
| 25 | +) |
| 26 | + |
| 27 | +const stressCaseFile = "parser/testdata/fingerprint/libpg_query.test" |
| 28 | + |
| 29 | +// stressInput returns the 1.1 MB INSERT (case 073 of the fingerprint suite). |
| 30 | +func stressInput(b *testing.B) string { |
| 31 | + b.Helper() |
| 32 | + cases, err := testfile.Read(stressCaseFile) |
| 33 | + if err != nil { |
| 34 | + b.Fatal(err) |
| 35 | + } |
| 36 | + big := 0 |
| 37 | + for i := range cases { |
| 38 | + if len(cases[i].Input) > len(cases[big].Input) { |
| 39 | + big = i |
| 40 | + } |
| 41 | + } |
| 42 | + return cases[big].Input |
| 43 | +} |
| 44 | + |
| 45 | +func benchmarkParse(input string, b *testing.B) { |
| 46 | + for i := 0; i < b.N; i++ { |
| 47 | + resultRes, benchErr = pg_query.Parse(input) |
| 48 | + if benchErr != nil { |
| 49 | + b.Errorf("Benchmark produced error %s\n\n", benchErr) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func benchmarkParseParallel(input string, b *testing.B) { |
| 55 | + b.RunParallel(func(pb *testing.PB) { |
| 56 | + for pb.Next() { |
| 57 | + _, err := pg_query.Parse(input) |
| 58 | + if err != nil { |
| 59 | + b.Errorf("Benchmark produced error %s\n\n", err) |
| 60 | + } |
| 61 | + } |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func benchmarkRawParse(input string, b *testing.B) { |
| 66 | + for i := 0; i < b.N; i++ { |
| 67 | + resultStr, benchErr = parser.ParseToProtobuf(input) |
| 68 | + if benchErr != nil { |
| 69 | + b.Errorf("Benchmark produced error %s\n\n", benchErr) |
| 70 | + } |
| 71 | + if len(resultStr) == 0 { |
| 72 | + b.Errorf("Benchmark produced empty result\n\n") |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func benchmarkRawParseParallel(input string, b *testing.B) { |
| 78 | + b.RunParallel(func(pb *testing.PB) { |
| 79 | + var str []byte |
| 80 | + var err error |
| 81 | + for pb.Next() { |
| 82 | + str, err = parser.ParseToProtobuf(input) |
| 83 | + if err != nil { |
| 84 | + b.Errorf("Benchmark produced error %s\n\n", err) |
| 85 | + } |
| 86 | + if len(str) == 0 { |
| 87 | + b.Errorf("Benchmark produced empty result\n\n") |
| 88 | + } |
| 89 | + } |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +func benchmarkParseToJSON(input string, b *testing.B) { |
| 94 | + for i := 0; i < b.N; i++ { |
| 95 | + resultS, benchErr = pg_query.ParseToJSON(input) |
| 96 | + if benchErr != nil { |
| 97 | + b.Errorf("Benchmark produced error %s\n\n", benchErr) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func benchmarkScan(input string, b *testing.B) { |
| 103 | + for i := 0; i < b.N; i++ { |
| 104 | + _, benchErr = pg_query.Scan(input) |
| 105 | + if benchErr != nil { |
| 106 | + b.Errorf("Benchmark produced error %s\n\n", benchErr) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func benchmarkFingerprint(input string, b *testing.B) { |
| 112 | + var str string |
| 113 | + for i := 0; i < b.N; i++ { |
| 114 | + str, benchErr = pg_query.Fingerprint(input) |
| 115 | + if benchErr != nil { |
| 116 | + b.Errorf("Benchmark produced error %s\n\n", benchErr) |
| 117 | + } |
| 118 | + if str == "" { |
| 119 | + b.Errorf("Benchmark produced empty result\n\n") |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func benchmarkNormalize(input string, b *testing.B) { |
| 125 | + for i := 0; i < b.N; i++ { |
| 126 | + resultS, benchErr = pg_query.Normalize(input) |
| 127 | + if benchErr != nil { |
| 128 | + b.Errorf("Benchmark produced error %s\n\n", benchErr) |
| 129 | + } |
| 130 | + if resultS == "" { |
| 131 | + b.Errorf("Benchmark produced empty result\n\n") |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func BenchmarkParseSelect1(b *testing.B) { |
| 137 | + benchmarkParse("SELECT 1", b) |
| 138 | +} |
| 139 | +func BenchmarkParseSelect2(b *testing.B) { |
| 140 | + benchmarkParse("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b) |
| 141 | +} |
| 142 | +func BenchmarkParseCreateTable(b *testing.B) { |
| 143 | + 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) |
| 144 | +} |
| 145 | + |
| 146 | +func BenchmarkParseSelect1Parallel(b *testing.B) { |
| 147 | + benchmarkParseParallel("SELECT 1", b) |
| 148 | +} |
| 149 | +func BenchmarkParseSelect2Parallel(b *testing.B) { |
| 150 | + benchmarkParseParallel("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b) |
| 151 | +} |
| 152 | +func BenchmarkParseCreateTableParallel(b *testing.B) { |
| 153 | + 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) |
| 154 | +} |
| 155 | + |
| 156 | +func BenchmarkRawParseSelect1(b *testing.B) { |
| 157 | + benchmarkRawParse("SELECT 1", b) |
| 158 | +} |
| 159 | +func BenchmarkRawParseSelect2(b *testing.B) { |
| 160 | + benchmarkRawParse("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b) |
| 161 | +} |
| 162 | +func BenchmarkRawParseCreateTable(b *testing.B) { |
| 163 | + 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) |
| 164 | +} |
| 165 | + |
| 166 | +func BenchmarkRawParseSelect1Parallel(b *testing.B) { |
| 167 | + benchmarkRawParseParallel("SELECT 1", b) |
| 168 | +} |
| 169 | +func BenchmarkRawParseSelect2Parallel(b *testing.B) { |
| 170 | + benchmarkRawParseParallel("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b) |
| 171 | +} |
| 172 | +func BenchmarkRawParseCreateTableParallel(b *testing.B) { |
| 173 | + 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) |
| 174 | +} |
| 175 | + |
| 176 | +func BenchmarkFingerprintSelect1(b *testing.B) { |
| 177 | + benchmarkFingerprint("SELECT 1", b) |
| 178 | +} |
| 179 | +func BenchmarkFingerprintSelect2(b *testing.B) { |
| 180 | + benchmarkFingerprint("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b) |
| 181 | +} |
| 182 | +func BenchmarkFingerprintCreateTable(b *testing.B) { |
| 183 | + 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) |
| 184 | +} |
| 185 | + |
| 186 | +func BenchmarkNormalizeSelect1(b *testing.B) { |
| 187 | + benchmarkNormalize("SELECT 1", b) |
| 188 | +} |
| 189 | +func BenchmarkNormalizeSelect2(b *testing.B) { |
| 190 | + benchmarkNormalize("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b) |
| 191 | +} |
| 192 | +func BenchmarkNormalizeCreateTable(b *testing.B) { |
| 193 | + 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) |
| 194 | +} |
| 195 | + |
| 196 | +// --- beyond the upstream set --- |
| 197 | + |
| 198 | +func BenchmarkParseToJSONSelect2(b *testing.B) { |
| 199 | + benchmarkParseToJSON("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b) |
| 200 | +} |
| 201 | +func BenchmarkScanSelect2(b *testing.B) { |
| 202 | + benchmarkScan("SELECT 1 FROM x WHERE y IN ('a', 'b', 'c')", b) |
| 203 | +} |
| 204 | + |
| 205 | +func BenchmarkRawParseStress(b *testing.B) { |
| 206 | + input := stressInput(b) |
| 207 | + b.SetBytes(int64(len(input))) |
| 208 | + b.ResetTimer() |
| 209 | + benchmarkRawParse(input, b) |
| 210 | +} |
| 211 | +func BenchmarkRawParseStressParallel(b *testing.B) { |
| 212 | + input := stressInput(b) |
| 213 | + b.SetBytes(int64(len(input))) |
| 214 | + b.ResetTimer() |
| 215 | + benchmarkRawParseParallel(input, b) |
| 216 | +} |
| 217 | +func BenchmarkFingerprintStress(b *testing.B) { |
| 218 | + input := stressInput(b) |
| 219 | + b.SetBytes(int64(len(input))) |
| 220 | + b.ResetTimer() |
| 221 | + benchmarkFingerprint(input, b) |
| 222 | +} |
| 223 | +func BenchmarkNormalizeStress(b *testing.B) { |
| 224 | + input := stressInput(b) |
| 225 | + b.SetBytes(int64(len(input))) |
| 226 | + b.ResetTimer() |
| 227 | + benchmarkNormalize(input, b) |
| 228 | +} |
| 229 | +func BenchmarkScanStress(b *testing.B) { |
| 230 | + input := stressInput(b) |
| 231 | + b.SetBytes(int64(len(input))) |
| 232 | + b.ResetTimer() |
| 233 | + benchmarkScan(input, b) |
| 234 | +} |
0 commit comments