-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconditional_benchmark_test.go
135 lines (125 loc) · 2.88 KB
/
conditional_benchmark_test.go
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
package tests_test
import (
"testing"
"github.com/Trendyol/es-query-builder/benchmarks/tests/marshal"
"github.com/Trendyol/es-query-builder/es"
"github.com/Trendyol/es-query-builder/es/enums/sort/order"
"github.com/Trendyol/es-query-builder/test/assert"
)
func createConditionalQuery(items []int) map[string]any {
query := es.NewQuery(
es.Bool().
Filter(
es.Range("indexedAt").
GreaterThan("2021-01-01").
LessThanOrEqual("now"),
es.Term("type", "File"),
es.Terms("sector", 1, 2, 3),
es.TermsFunc("id", items, func(key string, values []int) bool {
for _, value := range values {
if value == 21 {
return false
}
}
return true
}),
).
MustNot(
es.Exists("blocks.reason.id"),
)).
Size(100).
Sort(
es.Sort("modifiedDate").Order(order.Desc),
).
SourceIncludes("id", "type", "indexedAt", "chapters").
SourceExcludes("private.key")
return query
}
func createConditionalQueryVanilla(items []int) map[string]any {
var flag bool
for _, item := range items {
if item == 21 {
flag = false
break
}
flag = true
}
filter := []map[string]interface{}{
{
"range": map[string]interface{}{
"indexedAt": map[string]interface{}{
"gt": "2021-01-01",
"lte": "now",
},
},
},
{
"term": map[string]interface{}{
"type": map[string]interface{}{
"value": "File",
},
},
},
{
"terms": map[string]interface{}{
"sector": []interface{}{1, 2, 3},
},
},
}
if flag {
filter = append(filter, map[string]interface{}{
"terms": map[string]interface{}{
"id": items,
},
})
}
query := map[string]interface{}{
"_source": map[string]interface{}{
"includes": []interface{}{"id", "type", "indexedAt", "chapters"},
"excludes": []interface{}{"private.key"},
},
"size": 100,
"sort": []map[string]interface{}{
{
"modifiedDate": map[string]interface{}{
"order": "desc",
},
},
},
"query": map[string]interface{}{
"bool": map[string]interface{}{
"filter": filter,
"must_not": []map[string]interface{}{
{
"exists": map[string]interface{}{
"field": "blocks.reason.id",
},
},
},
},
},
}
return query
}
func Benchmark_Conditional_Builder(b *testing.B) {
items := []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
createConditionalQuery(items)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createConditionalQuery(items)
}
}
func Benchmark_Conditional_Vanilla(b *testing.B) {
items := []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
createConditionalQueryVanilla(items)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createConditionalQueryVanilla(items)
}
}
func Test_Conditional_Queries_are_equal(t *testing.T) {
items := []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
build := marshal.String(t, createConditionalQuery(items))
vanilla := marshal.String(t, createConditionalQueryVanilla(items))
assert.Equal(t, vanilla, build)
}