-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintermediate_benchmark_test.go
107 lines (99 loc) · 2.31 KB
/
intermediate_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
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 createIntermediateQuery(id int) map[string]any {
query := es.NewQuery(
es.Bool().
Must(
es.Bool().
Should(
es.Term("doc.id", id),
es.Term("file.fileId", id),
),
).
Filter(
es.Terms("type", "DOC", "FILE"),
)).
Size(45).
Sort(es.Sort("name").Order(order.Asc)).
SourceIncludes("id", "type", "indexedAt", "chapters")
return query
}
func createIntermediateQueryVanilla(id int) map[string]any {
query := map[string]interface{}{
"_source": map[string]interface{}{
"includes": []interface{}{"id", "type", "indexedAt", "chapters"},
},
"size": 45,
"sort": []map[string]interface{}{
{
"name": map[string]interface{}{
"order": "asc",
},
},
},
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []interface{}{
map[string]interface{}{
"bool": map[string]interface{}{
"should": []interface{}{
map[string]interface{}{
"term": map[string]interface{}{
"doc.id": map[string]interface{}{
"value": id,
},
},
},
map[string]interface{}{
"term": map[string]interface{}{
"file.fileId": map[string]interface{}{
"value": id,
},
},
},
},
},
},
},
"filter": []interface{}{
map[string]interface{}{
"terms": map[string]interface{}{
"type": []string{
"DOC", "FILE",
},
},
},
},
},
},
}
return query
}
func Benchmark_Intermediate_Builder(b *testing.B) {
id := 42
createIntermediateQuery(id)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createIntermediateQuery(id)
}
}
func Benchmark_Intermediate_Vanilla(b *testing.B) {
id := 42
createIntermediateQueryVanilla(id)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createIntermediateQueryVanilla(id)
}
}
func Test_Intermediate_Queries_are_equal(t *testing.T) {
id := 42
build := marshal.String(t, createIntermediateQuery(id))
vanilla := marshal.String(t, createIntermediateQueryVanilla(id))
assert.Equal(t, vanilla, build)
}