-
Notifications
You must be signed in to change notification settings - Fork 1
/
routing_table_test.go
180 lines (138 loc) · 3.51 KB
/
routing_table_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package dht
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRoutingTableFindNearest(t *testing.T) {
rt := newRoutingTable(&node{
id: randomID(),
})
// generate a random target key we want to look up
target := randomID()
// attempt to search an empty routing table
n := rt.closest(target)
require.Nil(t, n)
// insert 10000 nodes into the routing table
for i := 0; i < 10000; i++ {
rt.insert(randomID(), nil)
}
// search the populated routing table
n = rt.closest(target)
require.NotNil(t, n)
// check all nodes to ensure we actually found the closest node
var nodes []*node
for i := range rt.buckets {
rt.buckets[i].iterate(func(nd *node) {
nodes = append(nodes, nd)
})
}
sort.Slice(nodes, func(i, j int) bool {
d1 := distance(nodes[i].id, target)
d2 := distance(nodes[j].id, target)
// we're sorting for the closest distance,
// which is actually the greatest number of
// matching bits, hence why we compare with >
return d1 > d2
})
assert.Equal(t, distance(n.id, target), distance(nodes[0].id, target))
}
func TestRoutingTableFindNearestN(t *testing.T) {
rt := newRoutingTable(&node{
id: randomID(),
})
// generate a random target key we want to look up
target := randomID()
// try to find nodes on an empty table
ns := rt.closestN(target, 3)
require.Len(t, ns, 0)
// insert 10000 nodes into the routing table
for i := 0; i < 10000; i++ {
rt.insert(randomID(), nil)
}
// try to find closest nodes on a populated table
ns = rt.closestN(target, 3)
require.Len(t, ns, 3)
// check all nodes to ensure we actually found the closest node
var nodes []*node
for i := range rt.buckets {
rt.buckets[i].iterate(func(nd *node) {
nodes = append(nodes, nd)
})
}
sort.Slice(nodes, func(i, j int) bool {
d1 := distance(nodes[i].id, target)
d2 := distance(nodes[j].id, target)
// we're sorting for the closest distance,
// which is actually the greatest number of
// matching bits, hence why we compare with >
return d1 > d2
})
for i := 0; i < 3; i++ {
assert.Equal(t, distance(target, nodes[i].id), distance(target, ns[i].id))
}
}
func BenchmarkRoutingTableFindNearest(b *testing.B) {
rt := newRoutingTable(&node{
id: randomID(),
})
// insert 10000 nodes into the routing table
for i := 0; i < 10000; i++ {
rt.insert(randomID(), nil)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
target := randomID()
rt.closest(target)
}
}
func BenchmarkRoutingTableFindNearestN(b *testing.B) {
rt := newRoutingTable(&node{
id: randomID(),
})
// insert 10000 nodes into the routing table
for i := 0; i < 10000; i++ {
rt.insert(randomID(), nil)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
target := randomID()
rt.closestN(target, 3)
}
}
func BenchmarkRoutingTableInsert(b *testing.B) {
rt := newRoutingTable(&node{
id: randomID(),
})
nodes := make([][]byte, 10000)
// preallocate 10,000 nodes
// should simulate seeing the same
for i := 0; i < 10000; i++ {
nodes[i] = randomID()
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
rt.insert(nodes[i%10000], nil)
}
}
func BenchmarkRoutingTableSeen(b *testing.B) {
rt := newRoutingTable(&node{
id: randomID(),
})
nodes := make([][]byte, 10000)
// preallocate 10,000 nodes
// should simulate seeing the same
for i := 0; i < 10000; i++ {
nodes[i] = randomID()
rt.insert(nodes[i], nil)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
rt.seen(nodes[i%10000])
}
}