github.com/coyove/common@v0.0.0-20240403014525-f70e643f9de8/quadtree/redis/redis_test.go (about) 1 package redis_adapter 2 3 import ( 4 "fmt" 5 "math/rand" 6 "os" 7 "strconv" 8 "testing" 9 "time" 10 11 "github.com/coyove/common/quadtree" 12 ) 13 14 func TestSimpleRedis(t *testing.T) { 15 rc := New(os.Getenv("QT")) 16 quadtree.MaxElems = 2 17 rand.Seed(time.Now().Unix()) 18 _tr, _ := quadtree.NewQuadTree(rc, quadtree.Pt(-180, 90), quadtree.Pt(180, -90), func(t *quadtree.QuadTree) { 19 t.MinBox = 3 20 }) 21 tr := func() quadtree.QuadTree { 22 tr, _ := _tr.LoadTree(_tr.ID) 23 return tr 24 } 25 26 randPoint := func() quadtree.Point { 27 x := rand.Float64()*360 - 180 28 y := rand.Float64()*180 - 90 29 if rand.Intn(4) == 0 { 30 x = float64(int64(x)) 31 y = float64(int64(y)) 32 } 33 if rand.Intn(10) == 0 { 34 x, y = 0, 0 35 } 36 return quadtree.Pt(x, y) 37 } 38 39 start := time.Now() 40 m := map[quadtree.Point]interface{}{} 41 allpoints := []quadtree.Point{} 42 for i := 0; i < 20; i++ { 43 p := randPoint() 44 m[p] = i 45 tr().Put(p, itob(i)) 46 allpoints = append(allpoints, p) 47 } 48 49 length := float64(len(m)) 50 fmt.Println("size:", len(m), time.Since(start).Seconds()/length) 51 52 start = time.Now() 53 idx := 0 54 for p, v := range m { 55 v2, err := tr().Get(p) 56 if err != nil { 57 t.Fatal(p, "idx=", idx, "expect=", v, "err=", err) 58 } 59 if btoi(v2.Data) != v { 60 t.Fatal(p, idx, "got:", v2.Data, "expect:", v) 61 } 62 idx++ 63 } 64 fmt.Println(time.Since(start).Seconds() / length) 65 fmt.Println(tr()) 66 rp := allpoints[rand.Intn(len(allpoints))] 67 fmt.Print("rand point=", rp, " neighbours=") 68 nn, _ := tr().FindNeig(rp, nil) 69 fmt.Println(len(nn)) 70 } 71 72 func itob(i int) []byte { return []byte(strconv.Itoa(i)) } 73 74 func btoi(b []byte) int { i, _ := strconv.Atoi(string(b)); return i }