Skip to content

Commit 94c8668

Browse files
authored
Add parsing benchmarks (#138)
1 parent 05eae61 commit 94c8668

File tree

4 files changed

+881
-0
lines changed

4 files changed

+881
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,64 @@ if you want to update them, you can run the following command:
8282
$ make update_test
8383
```
8484

85+
## Benchmarks
86+
87+
```sh
88+
go test -bench=. -benchmem ./parser
89+
```
90+
91+
Results
92+
93+
```
94+
orian@delik:~/workspace/clickhouse-sql-parser$ go test -bench=. -benchmem ./parser
95+
goos: linux
96+
goarch: amd64
97+
pkg: github.com/AfterShip/clickhouse-sql-parser/parser
98+
cpu: Intel(R) Xeon(R) CPU E5-2697 v3 @ 2.60GHz
99+
BenchmarkParseSQLFiles/access_tuple_with_dot.sql-28 23294 58467 ns/op 13448 B/op 293 allocs/op
100+
BenchmarkParseSQLFiles/query_with_expr_compare.sql-28 43560 25704 ns/op 6240 B/op 132 allocs/op
101+
BenchmarkParseSQLFiles/select_cast.sql-28 75055 16518 ns/op 4648 B/op 92 allocs/op
102+
BenchmarkParseSQLFiles/select_column_alias_string.sql-28 499798 2785 ns/op 704 B/op 13 allocs/op
103+
BenchmarkParseSQLFiles/select_expr.sql-28 488187 2448 ns/op 696 B/op 12 allocs/op
104+
BenchmarkParseSQLFiles/select_item_with_modifiers.sql-28 54124 23305 ns/op 6232 B/op 136 allocs/op
105+
BenchmarkParseSQLFiles/select_order_by_timestamp.sql-28 232302 5809 ns/op 1368 B/op 28 allocs/op
106+
BenchmarkParseSQLFiles/select_simple.sql-28 30602 50022 ns/op 9920 B/op 216 allocs/op
107+
BenchmarkParseSQLFiles/select_simple_field_alias.sql-28 178126 6316 ns/op 1712 B/op 39 allocs/op
108+
BenchmarkParseSQLFiles/select_simple_with_bracket.sql-28 71902 16172 ns/op 3824 B/op 85 allocs/op
109+
BenchmarkParseSQLFiles/select_simple_with_cte_with_column_aliases.sql-28 67050 19916 ns/op 4648 B/op 101 allocs/op
110+
BenchmarkParseSQLFiles/select_simple_with_group_by_with_cube_totals.sql-28 107047 10348 ns/op 2768 B/op 58 allocs/op
111+
BenchmarkParseSQLFiles/select_simple_with_is_not_null.sql-28 55285 21957 ns/op 5224 B/op 111 allocs/op
112+
BenchmarkParseSQLFiles/select_simple_with_is_null.sql-28 66648 22412 ns/op 4728 B/op 102 allocs/op
113+
BenchmarkParseSQLFiles/select_simple_with_top_clause.sql-28 269910 4166 ns/op 1088 B/op 22 allocs/op
114+
BenchmarkParseSQLFiles/select_simple_with_with_clause.sql-28 58494 18417 ns/op 5144 B/op 109 allocs/op
115+
BenchmarkParseSQLFiles/select_table_alias_without_keyword.sql-28 121261 10011 ns/op 2896 B/op 65 allocs/op
116+
BenchmarkParseSQLFiles/select_table_function_with_query.sql-28 98017 14929 ns/op 4168 B/op 81 allocs/op
117+
BenchmarkParseSQLFiles/select_when_condition.sql-28 220394 5457 ns/op 1304 B/op 28 allocs/op
118+
BenchmarkParseSQLFiles/select_with_distinct.sql-28 172948 6531 ns/op 1560 B/op 33 allocs/op
119+
BenchmarkParseSQLFiles/select_with_join_only.sql-28 286346 5594 ns/op 1520 B/op 32 allocs/op
120+
BenchmarkParseSQLFiles/select_with_left_join.sql-28 88200 13627 ns/op 3880 B/op 75 allocs/op
121+
BenchmarkParseSQLFiles/select_with_literal_table_name.sql-28 241094 5099 ns/op 1304 B/op 27 allocs/op
122+
BenchmarkParseSQLFiles/select_with_multi_join.sql-28 44700 31964 ns/op 8240 B/op 188 allocs/op
123+
BenchmarkParseSQLFiles/select_with_multi_line_comment.sql-28 363499 4460 ns/op 824 B/op 18 allocs/op
124+
BenchmarkParseSQLFiles/select_with_multi_union.sql-28 146233 7827 ns/op 2176 B/op 36 allocs/op
125+
BenchmarkParseSQLFiles/select_with_number_field.sql-28 129945 8746 ns/op 2352 B/op 51 allocs/op
126+
BenchmarkParseSQLFiles/select_with_query_parameter.sql-28 33850 36346 ns/op 9936 B/op 209 allocs/op
127+
BenchmarkParseSQLFiles/select_with_string_expr.sql-28 142882 7530 ns/op 1880 B/op 34 allocs/op
128+
BenchmarkParseSQLFiles/select_with_union_distinct.sql-28 147031 9601 ns/op 2352 B/op 47 allocs/op
129+
BenchmarkParseSQLFiles/select_with_variable.sql-28 179158 6669 ns/op 1880 B/op 36 allocs/op
130+
BenchmarkParseSQLFiles/select_with_window_function.sql-28 54925 31320 ns/op 6720 B/op 136 allocs/op
131+
BenchmarkParseSQLFiles/select_with_placeholder.sql-28 196771 5145 ns/op 1272 B/op 26 allocs/op
132+
BenchmarkParseSQLFiles/set_simple.sql-28 172419 7062 ns/op 2480 B/op 49 allocs/op
133+
BenchmarkParseComplexQueries/testdata/query/select_with_multi_join.sql-28 39056 36897 ns/op 8240 B/op 188 allocs/op
134+
BenchmarkParseComplexQueries/testdata/query/select_with_window_function.sql-28 47629 29916 ns/op 6720 B/op 136 allocs/op
135+
BenchmarkParseComplexQueries/testdata/query/select_simple_with_with_clause.sql-28 69210 19731 ns/op 5144 B/op 109 allocs/op
136+
BenchmarkParseComplexQueries/testdata/query/select_with_left_join.sql-28 74576 15338 ns/op 3880 B/op 75 allocs/op
137+
BenchmarkParseComplexQueries/testdata/benchdata/posthog_huge_0.sql-28 235 6231253 ns/op 1236189 B/op 26696 allocs/op
138+
BenchmarkParseComplexQueries/testdata/benchdata/posthog_huge_1.sql-28 279 4438280 ns/op 1043374 B/op 22717 allocs/op
139+
PASS
140+
ok github.com/AfterShip/clickhouse-sql-parser/parser 66.547s
141+
```
142+
85143
## Contact us
86144

87145
Feel free to open an issue or discussion if you have any issues or questions.

parser/benchmark_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package parser
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
// BenchmarkParseSQLFiles benchmarks parsing all SQL files in the testdata/query directory
10+
func BenchmarkParseSQLFiles(b *testing.B) {
11+
testFiles, err := filepath.Glob("testdata/query/*.sql")
12+
if err != nil {
13+
b.Fatalf("Failed to glob test files: %v", err)
14+
}
15+
16+
for _, file := range testFiles {
17+
content, err := os.ReadFile(file)
18+
if err != nil {
19+
b.Fatalf("Failed to read file %s: %v", file, err)
20+
}
21+
22+
b.Run(filepath.Base(file), func(b *testing.B) {
23+
for i := 0; i < b.N; i++ {
24+
parser := NewParser(string(content))
25+
_, err := parser.ParseStmts()
26+
if err != nil {
27+
b.Fatalf("Failed to parse SQL from %s: %v", file, err)
28+
}
29+
}
30+
})
31+
}
32+
}
33+
34+
// BenchmarkParseComplexQueries benchmarks parsing specifically complex SQL queries
35+
func BenchmarkParseComplexQueries(b *testing.B) {
36+
complexQueries := []string{
37+
"testdata/query/select_with_multi_join.sql",
38+
"testdata/query/select_with_window_function.sql",
39+
"testdata/query/select_simple_with_with_clause.sql",
40+
"testdata/query/select_with_left_join.sql",
41+
"testdata/benchdata/posthog_huge_0.sql",
42+
"testdata/benchdata/posthog_huge_1.sql",
43+
}
44+
45+
for _, queryFile := range complexQueries {
46+
content, err := os.ReadFile(queryFile)
47+
if err != nil {
48+
b.Fatalf("Failed to read file %s: %v", queryFile, err)
49+
}
50+
51+
b.Run(queryFile, func(b *testing.B) {
52+
b.ResetTimer()
53+
for i := 0; i < b.N; i++ {
54+
parser := NewParser(string(content))
55+
_, err := parser.ParseStmts()
56+
if err != nil {
57+
b.Fatalf("Failed to parse SQL from %s: %v", queryFile, err)
58+
}
59+
}
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)