Skip to content

Commit 2e998e2

Browse files
committed
CityLots benchmark
Not replacing existing test since it’s measured events per second and I couldn’t get it done with go benchmark. But the benchmark allows a better (and stable way) for comparing regression and improvements.
1 parent 31b199b commit 2e998e2

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

benchmarks_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var (
2020
cityLotsLineCount int
2121
)
2222

23-
func getCityLotsLines(t *testing.T) [][]byte {
24-
t.Helper()
23+
func getCityLotsLines(tb testing.TB) [][]byte {
24+
tb.Helper()
2525

2626
cityLotsLock.Lock()
2727
defer cityLotsLock.Unlock()
@@ -30,14 +30,14 @@ func getCityLotsLines(t *testing.T) [][]byte {
3030
}
3131
file, err := os.Open("testdata/citylots.jlines.gz")
3232
if err != nil {
33-
t.Error("Can't open citlots.jlines.gz: " + err.Error())
33+
tb.Error("Can't open citlots.jlines.gz: " + err.Error())
3434
}
3535
defer func(file *os.File) {
3636
_ = file.Close()
3737
}(file)
3838
zr, err := gzip.NewReader(file)
3939
if err != nil {
40-
t.Error("Can't open zip reader: " + err.Error())
40+
tb.Error("Can't open zip reader: " + err.Error())
4141
}
4242

4343
scanner := bufio.NewScanner(zr)

citylots_bench_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package quamina
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func BenchmarkCityLots(b *testing.B) {
9+
var localMatches []X
10+
11+
patterns := []string{
12+
`{ "properties": { "STREET": [ "CRANLEIGH" ] } }`,
13+
`{ "properties": { "STREET": [ "17TH" ], "ODD_EVEN": [ "E"] } }`,
14+
`{ "geometry": { "coordinates": [ 37.807807921694092 ] } }`,
15+
`{ "properties": { "MAPBLKLOT": ["0011008"], "BLKLOT": ["0011008"]}, "geometry": { "coordinates": [ 37.807807921694092 ] } } `,
16+
}
17+
names := []string{
18+
"CRANLEIGH",
19+
"17TH Even",
20+
"Geometry",
21+
"0011008",
22+
}
23+
24+
var err error
25+
q, err := New()
26+
if err != nil {
27+
b.Fatalf("New(): %s", err.Error())
28+
}
29+
for i := range names {
30+
err = q.AddPattern(names[i], patterns[i])
31+
if err != nil {
32+
b.Fatalf("AddPattern failed: %s", err.Error())
33+
}
34+
}
35+
b.Log(matcherStats(q.matcher.(*coreMatcher)))
36+
37+
lines := getCityLotsLines(b)
38+
39+
b.ResetTimer()
40+
before := time.Now()
41+
42+
for i := 0; i < b.N; i++ {
43+
lineIndex := i
44+
if i >= len(lines) {
45+
lineIndex = 0
46+
}
47+
48+
matches, err := q.MatchesForEvent(lines[lineIndex])
49+
if err != nil {
50+
b.Errorf("Matches4JSON: %s", err.Error())
51+
}
52+
53+
localMatches = matches
54+
}
55+
56+
topMatches = localMatches
57+
elapsed := float64(time.Since(before).Milliseconds())
58+
perSecond := float64(b.N) / (elapsed / 1000.0)
59+
b.Logf("%.2f matches/second, total lines: %d", perSecond, b.N)
60+
}

0 commit comments

Comments
 (0)