forked from niven/simhashing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_test.go
92 lines (70 loc) · 2.62 KB
/
sim_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
package simhashing
import "testing"
import "fmt"
import "math/rand"
import "time"
func TestFindClosestKeys(t *testing.T) {
simstore := NewSimStore()
simstore.Insert("It was the best of times, it was the worst of times,", 1)
simstore.Insert("it was the age of wisdom, it was the age of foolishness,", 2)
simstore.Insert("it was the epoch of belief, it was the epoch of incredulity,", 3)
simstore.Insert("it was the season of Light, it was the season of Darkness,", 4)
simstore.Insert("it was the spring of hope, it was the winter of despair,", 5)
simstore.Insert("we had everything before us, we had nothing before us,", 6)
simstore.Insert("we were all going direct to Heaven, we were all going direct the other way", 7)
found := simstore.FindClosest("It was the best of times, it was the worst of times,")
if found != 1 {
t.Error("FindClosest didn't find the perfect match")
}
found = simstore.FindClosest("It was the best of times and it was the worst of times")
if found != 1 {
t.Error("FindClosest didn't find the good match")
}
}
// this is where we insert >> 256 items so we are sure to test the FindClosest codepath that includes searching the nodes
func TestFindClosestNodes(t *testing.T) {
simstore := NewSimStore()
// insert 100K nonsense
r := rand.New(rand.NewSource(1234))
for i := 0; i < 20*1000; i++ {
simstore.Insert(fmt.Sprintf("%016x", r.Int63()), int64(i))
}
// insert the thing we hope to find
simstore.Insert("It was the best of times, it was the worst of times,", -1)
// exact match
found := simstore.FindClosest("It was the best of times, it was the worst of times,")
if found != -1 {
t.Error("FindClosest didn't find the perfect match")
}
// close match
found = simstore.FindClosest("It was the best of times, it was peanut butter jelly time")
if found != -1 {
t.Error("FindClosest didn't find the perfect match")
}
}
// not really testing anytihng, just looking at timing stuff
func TestTiming(t *testing.T) {
simstore := NewSimStore()
// insert stuff
N := 2 * 1000 // * 1000 //* 10
r := rand.New(rand.NewSource(45342))
for i := 0; i < N; i++ {
simstore.Insert(fmt.Sprintf("%016x", r.Int63()), int64(i))
if i%1000*1000 == 0 {
fmt.Printf("Inserted %.6f%%\n", 100*float64(i)/float64(N))
}
}
for i := 0; i < 20; i++ {
t0 := time.Now()
simstore.FindClosest(fmt.Sprintf("%016x", r.Int63()))
t1 := time.Now()
fmt.Printf("FindClosest in store with %d items took %v.\n", N, t1.Sub(t0))
}
}
func BenchmarkInsert(b *testing.B) {
simstore := NewSimStore()
r := rand.New(rand.NewSource(45342))
for i := 0; i < b.N; i++ {
simstore.Insert(fmt.Sprintf("%016x", r.Int63()), int64(i))
}
}