-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqparser_benchmark_test.go
More file actions
156 lines (135 loc) Β· 4.68 KB
/
qparser_benchmark_test.go
File metadata and controls
156 lines (135 loc) Β· 4.68 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
package qparser
import (
"testing"
"time"
)
// BenchmarkIsQuery benchmarks the performance of IsQuery function
func BenchmarkIsQuery(b *testing.B) {
testCases := []string{
"title:hello world",
"plain text without query",
"title:hello author:john limit:10 tags:go,programming",
"",
"very long text without any query syntax just plain text to test performance",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, input := range testCases {
IsQuery(input)
}
}
}
// BenchmarkUnmarshal_Simple benchmarks simple parsing performance
func BenchmarkUnmarshal_Simple(b *testing.B) {
type SimpleFilter struct {
Keyword string `query:"keyword,default"`
Title string `query:"title"`
Limit int `query:"limit"`
}
query := "hello world title:foobar limit:42"
b.ResetTimer()
for i := 0; i < b.N; i++ {
var filter SimpleFilter
Unmarshal(query, &filter)
}
}
// BenchmarkUnmarshal_Complex benchmarks complex parsing performance
func BenchmarkUnmarshal_Complex(b *testing.B) {
type ComplexFilter struct {
Keyword string `query:"keyword,default"`
Title string `query:"title"`
Author string `query:"author"`
Limit int `query:"limit"`
Offset int `query:"offset"`
Tags []string `query:"tags"`
Published *bool `query:"published"`
Before time.Time `query:"before,format=2006-01-02"`
After time.Time `query:"after,format=2006-01-02"`
Score float64 `query:"score"`
}
query := `hello world title:"Go Programming" author:john limit:50 offset:100 tags:go,programming,backend published:true before:2024-12-31 after:2024-01-01 score:9.5 extra text`
b.ResetTimer()
for i := 0; i < b.N; i++ {
var filter ComplexFilter
Unmarshal(query, &filter)
}
}
// BenchmarkUnmarshalStrict_Simple benchmarks Strict mode performance
func BenchmarkUnmarshalStrict_Simple(b *testing.B) {
type SimpleFilter struct {
Keyword string `query:"keyword,default"`
Title string `query:"title"`
Limit int `query:"limit"`
}
query := "hello world title:foobar limit:42"
b.ResetTimer()
for i := 0; i < b.N; i++ {
var filter SimpleFilter
UnmarshalStrict(query, &filter)
}
}
// BenchmarkUnmarshalStrict_WithErrors benchmarks Strict mode performance with errors
func BenchmarkUnmarshalStrict_WithErrors(b *testing.B) {
type SimpleFilter struct {
Keyword string `query:"keyword,default"`
Title string `query:"title"`
Limit int `query:"limit"`
}
query := "hello world title:foobar limit:invalid unknown:field"
b.ResetTimer()
for i := 0; i < b.N; i++ {
var filter SimpleFilter
UnmarshalStrict(query, &filter)
}
}
// BenchmarkUnmarshal_LongQuery benchmarks long query string performance
func BenchmarkUnmarshal_LongQuery(b *testing.B) {
type LongFilter struct {
Keyword string `query:"keyword,default"`
Tags []string `query:"tags"`
Limit int `query:"limit"`
}
// Generate a long query string
query := "this is a very long search query with many words that should be handled efficiently by the parser " +
"tags:go,programming,web,development,backend,frontend,database,microservices,kubernetes,docker " +
"limit:100 " +
"and even more text here to make it really long and test the performance with extensive content"
b.ResetTimer()
for i := 0; i < b.N; i++ {
var filter LongFilter
Unmarshal(query, &filter)
}
}
// BenchmarkUnmarshal_ManyFields benchmarks many fields struct performance
func BenchmarkUnmarshal_ManyFields(b *testing.B) {
type ManyFieldsFilter struct {
Keyword string `query:"keyword,default"`
Title string `query:"title"`
Author string `query:"author"`
Category string `query:"category"`
Status string `query:"status"`
Priority string `query:"priority"`
Assignee string `query:"assignee"`
Reporter string `query:"reporter"`
Project string `query:"project"`
Version string `query:"version"`
Limit int `query:"limit"`
Offset int `query:"offset"`
Page int `query:"page"`
Size int `query:"size"`
Score float64 `query:"score"`
Rating float64 `query:"rating"`
Published bool `query:"published"`
Featured bool `query:"featured"`
CreatedAt time.Time `query:"created_at,format=2006-01-02"`
UpdatedAt time.Time `query:"updated_at,format=2006-01-02"`
}
query := `search text title:example author:john category:tech status:active priority:high assignee:alice ` +
`reporter:bob project:webapp version:1.0 limit:50 offset:0 page:1 size:20 score:8.5 rating:4.2 ` +
`published:true featured:false created_at:2024-01-01 updated_at:2024-12-31 additional content`
b.ResetTimer()
for i := 0; i < b.N; i++ {
var filter ManyFieldsFilter
Unmarshal(query, &filter)
}
}