-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmaglev_test.go
97 lines (82 loc) · 1.77 KB
/
maglev_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
package maglev
import (
"fmt"
"math/rand"
"reflect"
"testing"
)
// TestLookup tests the lookup field in table
func TestLookup(t *testing.T) {
table := getTestingMaglevTable()
if !reflect.DeepEqual(table.lookup, []int{
1, 2, 0, 2, 0, 1, 0,
}) {
t.Errorf("table lookup field not the same")
}
}
func TestPopulate(t *testing.T) {
table := getTestingMaglevTable()
var tests = []struct {
dead []int
want []int
}{
{nil, []int{1, 2, 0, 2, 0, 1, 0}},
{[]int{1}, []int{0, 2, 0, 2, 0, 2, 0}},
}
permutations := [][]uint64{
{2, 6, 3, 0, 4, 1, 5},
{0, 5, 3, 1, 6, 4, 2},
{1, 3, 5, 0, 2, 4, 6},
}
newPermutations := [][]uint64{
make([]uint64, 7),
make([]uint64, 7),
make([]uint64, 7),
}
table.resetOffsets()
for i := 0; i < 3; i++ {
for j := 0; j < 7; j++ {
table.nextOffset(i, &newPermutations[i][j])
}
}
if !reflect.DeepEqual(permutations, newPermutations) {
t.Errorf("permutations=%v, want %v", newPermutations, permutations)
t.Errorf("1")
}
for _, tt := range tests {
if got := table.populate(7, tt.dead); !reflect.DeepEqual(got, tt.want) {
t.Errorf("populate(...,%v)=%v, want %v", tt.dead, got, tt.want)
}
}
}
func TestDistribution(t *testing.T) {
const size = 125
var names []string
for i := 0; i < size; i++ {
names = append(names, fmt.Sprintf("backend-%d", i))
}
table := New(names, SmallM)
r := make([]int, size)
rand.Seed(0)
for i := 0; i < 1e6; i++ {
idx := table.Lookup(uint64(rand.Int63()))
r[idx]++
}
var total int
var max = 0
for _, v := range r {
total += v
if v > max {
max = v
}
}
mean := float64(total) / size
t.Logf("max=%v, mean=%v, peak-to-mean=%v", max, mean, float64(max)/mean)
}
func getTestingMaglevTable() *Table {
return New([]string{
"backend-0",
"backend-1",
"backend-2",
}, 7)
}