-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsistent_test.go
99 lines (88 loc) · 1.93 KB
/
consistent_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
package consistent
import (
"fmt"
"math/rand"
"strconv"
"testing"
"unsafe"
)
func TestConsistentBasic(t *testing.T) {
c := New()
_, err := c.Hash("abc")
if err != ErrNoHost {
t.Fatalf("should returns ErrNoHost")
}
c.Add("host1")
host, err := c.Hash("anykey")
if host != "host1" {
t.Fatalf("should returns host1")
}
c.Remove("host1")
_, err = c.Hash("abc")
if err != ErrNoHost {
t.Fatalf("should returns ErrNoHost")
}
c1 := New()
c2 := New()
for i := 0; i < 10; i++ {
c1.Add(fmt.Sprintf("host%d", i))
c2.Add(fmt.Sprintf("host%d", 10-i-1))
}
strbuf := make([]byte, 256)
for i := 0; i < 1000; i++ {
keylen := rand.Intn(16) + 16
rand.Read(strbuf[:keylen])
key := string(strbuf[:keylen])
host1, err := c1.Hash(key)
if err != nil {
t.Fatal(err)
}
host2, err := c2.Hash(key)
if err != nil {
t.Fatal(err)
}
if host1 != host2 {
t.FailNow()
}
}
}
func BenchmarkStringToSlice1(b *testing.B) {
s := "some string LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"
tl := 0
for i := 0; i < b.N; i++ {
tl += len([]byte(s))
}
if tl != len(s)*b.N {
b.Fatalf("wrong length")
}
}
func BenchmarkStringToSlice2(b *testing.B) {
s := "some string LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"
tl := 0
for i := 0; i < b.N; i++ {
tl += len(*((*[]byte)(unsafe.Pointer(&s))))
}
if tl != len(s)*b.N {
b.Fatalf("wrong length")
}
}
func BenchmarkConsistent_AddRemove(b *testing.B) {
c := New()
for i := 0; i < 10; i++ {
c.Add(strconv.Itoa(i))
}
for i := 0; i < b.N; i++ {
c.Add("test")
c.Remove("test")
}
}
func BenchmarkConsistent_Hash(b *testing.B) {
c := New()
c.SetReplica(100)
for i := 0; i < 10; i++ {
c.Add(strconv.Itoa(i))
}
for i := 0; i < b.N; i++ {
c.Hash(strconv.Itoa(1000 + i))
}
}