tile38/internal/server/scanner_test.go

55 lines
1.4 KiB
Go
Raw Normal View History

2020-03-25 23:11:05 +03:00
package server
import (
2022-09-20 03:47:38 +03:00
"fmt"
2020-03-25 23:11:05 +03:00
"math"
"math/rand"
"testing"
"time"
"github.com/tidwall/geojson"
"github.com/tidwall/geojson/geometry"
2022-09-20 03:47:38 +03:00
"github.com/tidwall/tile38/internal/field"
2022-09-21 00:20:53 +03:00
"github.com/tidwall/tile38/internal/object"
2020-03-25 23:11:05 +03:00
)
type testPointItem struct {
object geojson.Object
2022-09-20 03:47:38 +03:00
fields field.List
2020-03-25 23:11:05 +03:00
}
func PO(x, y float64) *geojson.Point {
return geojson.NewPoint(geometry.Point{X: x, Y: y})
}
func BenchmarkFieldMatch(t *testing.B) {
rand.Seed(time.Now().UnixNano())
items := make([]testPointItem, t.N)
for i := 0; i < t.N; i++ {
2022-09-20 03:47:38 +03:00
var fields field.List
fields = fields.Set(field.Make("foo", fmt.Sprintf("%f", rand.Float64()*9+1)))
fields = fields.Set(field.Make("bar", fmt.Sprintf("%f", math.Round(rand.Float64()*30)+1)))
2020-03-25 23:11:05 +03:00
items[i] = testPointItem{
PO(rand.Float64()*360-180, rand.Float64()*180-90),
2022-09-20 03:47:38 +03:00
fields,
2020-03-25 23:11:05 +03:00
}
}
sw := &scanWriter{
wheres: []whereT{
2022-09-20 03:47:38 +03:00
{"foo", false, field.ValueOf("1"), false, field.ValueOf("3")},
{"bar", false, field.ValueOf("10"), false, field.ValueOf("30")},
2020-03-25 23:11:05 +03:00
},
whereins: []whereinT{
2022-09-20 03:47:38 +03:00
{"foo", []field.Value{field.ValueOf("1"), field.ValueOf("2")}},
{"bar", []field.Value{field.ValueOf("11"), field.ValueOf("25")}},
2020-03-25 23:11:05 +03:00
},
}
t.ResetTimer()
for i := 0; i < t.N; i++ {
// one call is super fast, measurements are not reliable, let's do 100
for ix := 0; ix < 100; ix++ {
2022-09-21 20:03:53 +03:00
sw.fieldMatch(object.New("", items[i].object, 0, items[i].fields))
2020-03-25 23:11:05 +03:00
}
}
}