Skip to content

Commit d66f4f2

Browse files
committed
Improved matcher testing code style and coverage.
Removed non-JSONSM benchmarks and moved them to their own repository. Added some test data and matching tests. Moved benchmarks/tests to more logical locations.
1 parent 74b4787 commit d66f4f2

9 files changed

+900
-638
lines changed
File renamed without changes.

jsonValueEval.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

matcher_bench_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package gojsonsm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func generateRandomData(mbsToGenerate int) ([][]byte, int, error) {
9+
avgBytesOfOneRecord := 1800
10+
rowsToGenerate := mbsToGenerate * 1000000 / avgBytesOfOneRecord
11+
data := make([][]byte, rowsToGenerate)
12+
totalBytes, err := genRandomUsers(32534059803498589, data)
13+
if err != nil {
14+
return nil, 0, err
15+
}
16+
fmt.Printf("MBs To Generate: %v TotalBytes: %v TotalEntries: %v\n", mbsToGenerate, totalBytes, len(data))
17+
return data, totalBytes, nil
18+
}
19+
20+
func BenchmarkMatcher(b *testing.B) {
21+
data, totalBytes, err := generateRandomData(1)
22+
if err != nil || len(data) == 0 {
23+
b.Fatalf("Data generation error: %s", err)
24+
}
25+
26+
// name["first"]="Brett" OR (age<50 AND isActive=True)
27+
matchJson := []byte(`
28+
["or",
29+
["equals",
30+
["field", "name", "first"],
31+
["value", "Brett"]
32+
],
33+
["and",
34+
["lessthan",
35+
["field", "age"],
36+
["value", 50]
37+
],
38+
["equals",
39+
["field", "isActive"],
40+
["value", true]
41+
]
42+
]
43+
]`)
44+
expr, err := ParseJsonExpression(matchJson)
45+
if err != nil {
46+
b.Errorf("Failed to parse expression: %s", err)
47+
return
48+
}
49+
50+
var trans Transformer
51+
matchDef := trans.Transform([]Expression{expr})
52+
m := NewMatcher(matchDef)
53+
54+
b.SetBytes(int64(totalBytes))
55+
b.ResetTimer()
56+
for j := 0; j < b.N; j++ {
57+
for i := 0; i < len(data); i++ {
58+
_, err := m.Match(data[i])
59+
60+
if err != nil {
61+
b.Fatalf("Matcher error: %s", err)
62+
}
63+
}
64+
}
65+
}
66+
67+
func BenchmarkSlowMatcher(b *testing.B) {
68+
data, totalBytes, err := generateRandomData(1)
69+
if err != nil || len(data) == 0 {
70+
b.Fatalf("Data generation error: %s", err)
71+
}
72+
73+
matchJson := []byte(`
74+
["or",
75+
["equals",
76+
["field", "name", "first"],
77+
["value", "Brett"]
78+
],
79+
["and",
80+
["lessthan",
81+
["field", "age"],
82+
["value", 50]
83+
],
84+
["equals",
85+
["field", "isActive"],
86+
["value", true]
87+
]
88+
]
89+
]`)
90+
expr, err := ParseJsonExpression(matchJson)
91+
if err != nil {
92+
b.Errorf("Failed to parse expression: %s", err)
93+
return
94+
}
95+
96+
m := NewSlowMatcher([]Expression{expr})
97+
98+
b.SetBytes(int64(totalBytes))
99+
b.ResetTimer()
100+
101+
for j := 0; j < b.N; j++ {
102+
for i := 0; i < len(data); i++ {
103+
_, err := m.Match(data[i])
104+
105+
if err != nil {
106+
b.Fatalf("Slow matcher error: %s", err)
107+
}
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)