-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnearest_test.go
132 lines (106 loc) · 4.09 KB
/
nearest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package nearest
import (
// "fmt"
"math/rand"
"strconv"
"testing"
)
// func TestNearest(t *testing.T) {
// var latitude, longitude float64 = 39.92324, 116.3906
// var precision int = 5
// curCoordNode := NewCoordNode(latitude, longitude, precision)
// near := NewNearest()
// near.SetPrecision(5)
// nodes := make([]*CoordNode, 6)
// nodes[0] = NewCoordNode(39.92424, 116.3906, precision)
// nodes[1] = NewCoordNode(39.91324, 116.3907, precision)
// nodes[2] = NewCoordNode(39.92364, 116.3806, precision)
// nodes[3] = NewCoordNode(39.92384, 116.3916, precision)
// nodes[4] = NewCoordNode(39.96324, 116.3106, precision)
// nodes[5] = NewCoordNode(39.92329, 116.3936, precision)
// for i, _ := range nodes {
// near.AddCoordNode("key["+strconv.Itoa(i)+"]", nodes[i])
// }
// // query 1
// fmt.Println("query 1")
// for k, v := range near.GetAllCoordNodes() {
// fmt.Printf("curCoordNode -- %v: %v\n", k, DistanceCoordNode(curCoordNode, v))
// }
// keys := near.QueryNearestSquare(latitude, longitude)
// for _, key := range keys {
// coordNode, _ := near.GetCoordNode(key)
// fmt.Printf("%v -- %v -- %v\n", key, coordNode, DistanceCoordNode(curCoordNode, coordNode))
// }
// fmt.Println()
// near.UpdateCoord("key[0]", 39.97424, 116.4906)
// near.UpdateCoord("key[4]", 39.91424, 116.3306)
// near.DeleteCoordNode("key[3]")
// // query 2
// fmt.Println("query 2")
// for k, v := range near.GetAllCoordNodes() {
// fmt.Printf("curCoordNode -- %v: %v\n", k, DistanceCoordNode(curCoordNode, v))
// }
// keys = near.QueryNearestSquare(latitude, longitude)
// for _, key := range keys {
// coordNode, _ := near.GetCoordNode(key)
// fmt.Printf("%v -- %v -- %v\n", key, coordNode, DistanceCoordNode(curCoordNode, coordNode))
// }
// fmt.Println()
// // query 3
// fmt.Println("query 3")
// for k, v := range near.GetAllCoordNodes() {
// fmt.Printf("nodes[4] -- %v: %v\n", k, DistanceCoordNode(nodes[4], v))
// }
// keys = near.QueryNearestSquareFromKey("key[4]")
// for _, key := range keys {
// coordNode, _ := near.GetCoordNode(key)
// fmt.Printf("%v -- %v -- %v\n", key, nodes[4], DistanceCoordNode(nodes[4], coordNode))
// }
// fmt.Println()
// }
// func TestDistance(t *testing.T) {
// latitude1, longitude1, latitude2, longitude2 := 39.92324, 116.3906, 39.92324, 116.3907
// fmt.Println(Distance(latitude1, longitude1, latitude2, longitude2))
// latitude1, longitude1, latitude2, longitude2 = 39.941, 116.45, 39.94, 116.451
// fmt.Println(Distance(latitude1, longitude1, latitude2, longitude2))
// latitude1, longitude1, latitude2, longitude2 = 39.96, 116.45, 39.94, 116.40
// fmt.Println(Distance(latitude1, longitude1, latitude2, longitude2))
// latitude1, longitude1, latitude2, longitude2 = 39.96, 116.45, 39.94, 117.30
// fmt.Println(Distance(latitude1, longitude1, latitude2, longitude2))
// latitude1, longitude1, latitude2, longitude2 = 39.26, 115.25, 41.04, 117.30
// fmt.Println(Distance(latitude1, longitude1, latitude2, longitude2))
// }
func BenchmarkDelete(b *testing.B) {
near := NewNearest()
var latitude, longitude float64 = 39.92324, 116.3906
for i := 0; i < b.N; i++ {
near.AddCoord("key"+strconv.Itoa(i), latitude, longitude)
near.DeleteCoordNode("key" + strconv.Itoa(i))
}
}
func BenchmarkUpdate(b *testing.B) {
near := NewNearest()
var latitude, longitude float64 = 39.92324, 116.3906
var newLatitude, newLongitude float64 = 49.92324, 120.3906
for i := 0; i < b.N; i++ {
near.AddCoord("key"+strconv.Itoa(i), latitude, longitude)
near.UpdateCoord("key"+strconv.Itoa(i), newLatitude, newLongitude)
}
}
func BenchmarkQuery(b *testing.B) {
b.StopTimer()
near := NewNearest()
var latitude, longitude float64 = 39.92324, 116.3906
var lat, lon float64
for i := 0; i < 1000000; i++ {
lat = float64(rand.Intn(100000)-50000) / 100000
lon = float64(rand.Intn(10000)-5000) / 10000
near.AddCoord("key"+strconv.Itoa(i), latitude+lat, longitude+lon)
}
b.StartTimer()
for i := 0; i < 100; i++ {
lat = float64(rand.Intn(100000)-50000) / 100000
lon = float64(rand.Intn(10000)-5000) / 10000
near.QueryNearestSquare(latitude+lat, longitude+lon)
}
}