forked from couchbaselabs/gojsonsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastMatcher_bench_test.go
110 lines (96 loc) · 2.13 KB
/
fastMatcher_bench_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
// +build perf
package gojsonsm
import (
"testing"
)
func generateRandomData(mbsToGenerate int) ([][]byte, int, error) {
avgBytesOfOneRecord := 1800
rowsToGenerate := mbsToGenerate * 1000000 / avgBytesOfOneRecord
data := make([][]byte, rowsToGenerate)
totalBytes, err := genRandomUsers(32534059803498589, data)
if err != nil {
return nil, 0, err
}
return data, totalBytes, nil
}
func BenchmarkMatcher(b *testing.B) {
data, totalBytes, err := generateRandomData(1)
if err != nil || len(data) == 0 {
b.Fatalf("Data generation error: %s", err)
}
// name["first"]="Brett" OR (age<50 AND isActive=True)
matchJson := []byte(`
["or",
["equals",
["field", "name", "first"],
["value", "Brett"]
],
["and",
["lessthan",
["field", "age"],
["value", 50]
],
["equals",
["field", "isActive"],
["value", true]
]
]
]`)
expr, err := ParseJsonExpression(matchJson)
if err != nil {
b.Errorf("Failed to parse expression: %s", err)
return
}
var trans Transformer
matchDef := trans.Transform([]Expression{expr})
m := NewFastMatcher(matchDef)
b.SetBytes(int64(totalBytes))
b.ResetTimer()
for j := 0; j < b.N; j++ {
for i := 0; i < len(data); i++ {
_, err := m.Match(data[i])
if err != nil {
b.Fatalf("FastMatcher error: %s", err)
}
}
}
}
func BenchmarkSlowMatcher(b *testing.B) {
data, totalBytes, err := generateRandomData(1)
if err != nil || len(data) == 0 {
b.Fatalf("Data generation error: %s", err)
}
matchJson := []byte(`
["or",
["equals",
["field", "name", "first"],
["value", "Brett"]
],
["and",
["lessthan",
["field", "age"],
["value", 50]
],
["equals",
["field", "isActive"],
["value", true]
]
]
]`)
expr, err := ParseJsonExpression(matchJson)
if err != nil {
b.Errorf("Failed to parse expression: %s", err)
return
}
m := NewSlowMatcher([]Expression{expr})
b.SetBytes(int64(totalBytes))
b.ResetTimer()
for j := 0; j < b.N; j++ {
for i := 0; i < len(data); i++ {
_, err := m.Match(data[i])
if err != nil {
b.Fatalf("Slow matcher error: %s", err)
}
}
}
}