Additional KNN test

This commit is contained in:
tidwall 2021-07-11 14:49:23 -07:00
parent dd4d31ae1b
commit 3c699183e0
1 changed files with 58 additions and 2 deletions

View File

@ -2,13 +2,19 @@ package tests
import ( import (
"fmt" "fmt"
"math"
"math/rand" "math/rand"
"sort" "sort"
"testing" "testing"
"time"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson"
) )
func subTestSearch(t *testing.T, mc *mockServer) { func subTestSearch(t *testing.T, mc *mockServer) {
runStep(t, mc, "KNN", keys_KNN_test) runStep(t, mc, "KNN_BASIC", keys_KNN_basic_test)
runStep(t, mc, "KNN_RANDOM", keys_KNN_random_test)
runStep(t, mc, "KNN_CURSOR", keys_KNN_cursor_test) runStep(t, mc, "KNN_CURSOR", keys_KNN_cursor_test)
runStep(t, mc, "WITHIN_CIRCLE", keys_WITHIN_CIRCLE_test) runStep(t, mc, "WITHIN_CIRCLE", keys_WITHIN_CIRCLE_test)
runStep(t, mc, "INTERSECTS_CIRCLE", keys_INTERSECTS_CIRCLE_test) runStep(t, mc, "INTERSECTS_CIRCLE", keys_INTERSECTS_CIRCLE_test)
@ -24,7 +30,7 @@ func subTestSearch(t *testing.T, mc *mockServer) {
runStep(t, mc, "FIELDS", keys_FIELDS_search_test) runStep(t, mc, "FIELDS", keys_FIELDS_search_test)
} }
func keys_KNN_test(mc *mockServer) error { func keys_KNN_basic_test(mc *mockServer) error {
return mc.DoBatch([][]interface{}{ return mc.DoBatch([][]interface{}{
{"SET", "mykey", "1", "POINT", 5, 5}, {"OK"}, {"SET", "mykey", "1", "POINT", 5, 5}, {"OK"},
{"SET", "mykey", "2", "POINT", 19, 19}, {"OK"}, {"SET", "mykey", "2", "POINT", 19, 19}, {"OK"},
@ -41,6 +47,56 @@ func keys_KNN_test(mc *mockServer) error {
}) })
} }
func keys_KNN_random_test(mc *mockServer) error {
// do random points
mc.Do("OUTPUT", "resp")
mc.Do("DROP", "points")
defer mc.Do("DROP", "points")
seed := time.Now().UnixNano()
// seed = 98123098
rng := rand.New(rand.NewSource(seed))
rpoint := func() [2]float64 {
return [2]float64{
rng.Float64()*360 - 180,
rng.Float64()*180 - 90,
}
}
N := 5000
points := make([][2]float64, N)
for i := 0; i < len(points); i++ {
points[i] = rpoint()
res, err := redis.String(mc.Do("SET", "points", i, "POINT", points[i][1], points[i][0]))
if err != nil {
return err
}
if res != "OK" {
return fmt.Errorf("expected 'OK', got '%s'", res)
}
}
target := rpoint()
mc.Do("OUTPUT", "json")
defer mc.Do("OUTPUT", "resp")
start := time.Now()
res, err := redis.String(mc.Do("NEARBY", "points", "LIMIT", N, "POINT", target[1], target[0]))
println(time.Since(start).String())
if err != nil {
return err
}
ldist := math.Inf(-1)
for _, dist := range gjson.Get(res, "objects.#.distance").Array() {
if ldist > dist.Float() {
return fmt.Errorf("out of order")
}
ldist = dist.Float()
}
return nil
}
func keys_KNN_cursor_test(mc *mockServer) error { func keys_KNN_cursor_test(mc *mockServer) error {
return mc.DoBatch([][]interface{}{ return mc.DoBatch([][]interface{}{
{"SET", "mykey", "1", "FIELD", "foo", 5.5, "POINT", 5, 5}, {"OK"}, {"SET", "mykey", "1", "FIELD", "foo", 5.5, "POINT", 5, 5}, {"OK"},