-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix_test.go
357 lines (331 loc) · 7.99 KB
/
radix_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package radix
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"math/rand"
"os"
"reflect"
"runtime"
"sync"
"testing"
"time"
)
func TestInteger1MDense(t *testing.T) {
rand.Seed(0)
N := 1000000
testSliceDatabaseN(t, N, fillIntegers(N))
}
func TestInteger1MSparse(t *testing.T) {
rand.Seed(0)
N := 1000000
testSliceDatabaseN(t, N, generateRandomIntegers(N))
}
func TestInteger10MSparse(t *testing.T) {
rand.Seed(0)
N := 10000000
testSliceDatabaseN(t, N, generateRandomIntegers(N))
}
func TestInteger10MDense(t *testing.T) {
rand.Seed(0)
N := 10000000
testSliceDatabaseN(t, N, fillIntegers(N))
}
func TestRepeatedInsertDelete(t *testing.T) {
db := loadURLDatabase(t)
db = db[:10000]
num := runtime.NumCPU()
trie := NewTree(nil)
for i := 0; i < 100; i++ {
insertAll(trie, db, num, true)
lookupAll(t, trie, db, num)
countAll(t, trie)
deleteAll(t, trie, db, num)
}
}
func testSliceDatabaseN(t *testing.T, N int, db [][]byte) {
num := runtime.NumCPU()
trie := buildTreeFromDB(db, num, true)
trie.Admin(os.Stdout, []string{"info"})
lookupAll(t, trie, db, num)
countAll(t, trie)
iterateAll(t, trie)
testMinMax(t, trie, db)
deleteAll(t, trie, db, num)
trie.Admin(os.Stdout, []string{"info"})
trie.Close()
}
func testMinMax(t *testing.T, trie *Tree, db [][]byte) {
min, ok := trie.MinKey(nil)
assertTrue(t, ok)
max, ok := trie.MaxKey(nil)
assertTrue(t, ok)
fmt.Printf("Min key : %x, Max key : %x\n", db[min], db[max])
assertTrue(t, bytes.Compare(db[min], db[max]) == -1)
}
func iterateAll(t *testing.T, trie *Tree) {
start := time.Now()
i := trie.NewIterator(nil)
count := 0
for i.Next() {
_ = i.Value()
count++
}
stop := time.Since(start)
fmt.Printf("%d items iterated in %v. %.2f M per second\n", count, stop, float64(count)/stop.Seconds()/1000000)
assertTrue(t, count == trie.Len())
}
func countAll(t *testing.T, trie *Tree) {
start := time.Now()
c := NewCounter(trie)
nodes, leaves := c.Count(nil)
stop := time.Since(start)
fmt.Printf("%d leaves and %d nodes counted in %v (nodes with prefix %d, total prefix bytes: %d)\n", leaves, nodes, stop, c.NodesWithPrefix, c.TotalPrefixBytes)
assertTrue(t, leaves == trie.Len())
}
func buildTreeFromDB(database [][]byte, num int, bench bool) *Tree {
trie := NewTree(&Options{
NumAllocators: num,
})
insertAll(trie, database, num, bench)
return trie
}
func insertAll(trie *Tree, database [][]byte, num int, bench bool) {
var (
start = time.Now()
wg sync.WaitGroup
N = len(database)
part = N / num
)
for j := 0; j < num; j++ {
wg.Add(1)
from, to := j*part, (j+1)*part
if j == num-1 {
to = N
}
go func() {
a := trie.GetAllocator()
for i, key := range database[from:to] {
S:
found, op := a.PrepareUpdate(key)
if found {
if bytes.Equal(key, database[op.Value]) {
// duplicate key in database, abort
op.Abort()
fmt.Printf("Duplicate key in test database : %s (it should be unique)\n", key)
continue
} else {
op.Match = false
op.FetchedKey = database[op.Value]
}
}
if !op.Finalize(uint64(from + i)) {
goto S
}
}
a.FlushStats()
trie.ReleaseAllocator(a)
wg.Done()
}()
}
wg.Wait()
stop := time.Since(start)
if bench {
fmt.Printf("%d inserts in %v. %.2f M per second\n", N, stop, float64(N)/stop.Seconds()/1000000)
}
}
func lookupAll(t *testing.T, trie *Tree, database [][]byte, num int) {
var (
start = time.Now()
N = len(database)
part = N / num
wg sync.WaitGroup
)
for j := 0; j < num; j++ {
wg.Add(1)
from, to := j*part, (j+1)*part
if j == num-1 {
to = N
}
go func() {
a := trie.GetAllocator()
for i, key := range database[from:to] {
if result, _ := a.Lookup(key); result != uint64(from+i) {
t.Errorf("Lookup failed! Got %d expected %d, while searching for %s", result, i+1, key)
c := NewCounter(trie)
nodes, leaves := c.Count(nil)
fmt.Printf("nodes %d, leaves %d\n", nodes, leaves)
panic("done")
}
}
trie.ReleaseAllocator(a)
wg.Done()
}()
}
wg.Wait()
stop := time.Since(start)
fmt.Printf("%d lookups in %v. %.2f M per second\n", N, stop, float64(N)/stop.Seconds()/1000000)
}
func deleteAll(t *testing.T, trie *Tree, database [][]byte, num int) {
var (
start = time.Now()
N = len(database)
part = N / num
wg sync.WaitGroup
)
for j := 0; j < num; j++ {
wg.Add(1)
from, to := j*part, (j+1)*part
if j == num-1 {
to = N
}
go func() {
a := trie.GetAllocator()
for i := from; i < to; i++ {
S:
found, op := a.PrepareDelete(database[i])
if found {
if bytes.Equal(database[i], database[op.Value]) {
if !op.Finalize() {
fmt.Printf("Write conflict, restarting\n")
goto S
}
} else {
t.Logf("Delete: Mismatch %s != %s", database[i], database[op.Value])
op.Abort()
}
} else {
t.Logf("Delete: Not found ! %s (length %d)", database[i], len(database[i]))
op.Abort()
}
}
trie.ReleaseAllocator(a)
wg.Done()
}()
}
wg.Wait()
stop := time.Since(start)
fmt.Printf("%d deletes in %v. %.2f M per second\n", N, stop, float64(N)/stop.Seconds()/1000000)
if trie.Len() != 0 {
t.Errorf("Tree was not empty after deleting all keys!")
}
}
var (
testFiles = make(map[string][][]byte)
testFileBytes = make(map[string]int64)
)
func loadTestFile(path string, expectedSize int) (db [][]byte, bytes int64) {
if db, ok := testFiles[path]; ok {
return db, testFileBytes[path]
}
if expectedSize > 0 {
// preallocate to avoid repeated slice capacity growth
db = make([][]byte, 0, expectedSize)
}
f, err := os.Open(path)
if err != nil {
return nil, 0
}
defer f.Close()
reader := bufio.NewReader(f)
for {
row, err := reader.ReadSlice('\n')
if err != nil {
break
}
row = row[:len(row)-1]
if len(row) == 0 {
continue
}
key := make([]byte, len(row)+1)
copy(key, row)
db = append(db, key)
bytes += int64(len(key))
}
testFiles[path] = db
testFileBytes[path] = bytes
return
}
func loadURLDatabase(t *testing.T) [][]byte {
db, _ := loadTestFile("testdata/DOI-2011.txt", 17000000)
if db == nil {
t.Skip("Testfile DOI-2011.txt not found")
}
return db
}
func BenchmarkMinReservation(b *testing.B) {
m := newEpochManager(8)
for i := 0; i < b.N; i++ {
m.minReservation()
}
}
func fillIntegers(N int) [][]byte {
database := make([][]byte, N)
// allocate one big slice to make sure all values are adjacent in memory
buf := make([]byte, N*8)
for i := 0; i < N; i++ {
b := buf[i*8 : i*8+8]
binary.BigEndian.PutUint64(b, uint64(i))
database[i] = b
}
return database
}
func generateRandomIntegers(N int) [][]byte {
database := make([][]byte, N)
// allocate one big slice to make sure all values are adjacent in memory
buf := make([]byte, N*8)
for i := range database {
b := buf[i*8 : i*8+8]
binary.BigEndian.PutUint64(b, rand.Uint64())
database[i] = b
}
return database
}
func generateRandomBytes(N int) [][]byte {
database := make([][]byte, N)
fmt.Printf("Generating %d random byte slices... ", N)
r := rand.New(rand.NewSource(0))
for i := range database {
l := 8 + rand.Intn(128)
b := make([]byte, l)
r.Read(b)
b[l-1] = 0
database[i] = b
}
fmt.Printf("done\n")
return database
}
func assertTrue(t *testing.T, value bool, comment ...string) {
if !value {
if len(comment) > 0 {
t.Errorf("Failed: %s", comment[0])
} else {
t.Errorf("Failed: got false, expected true")
}
}
}
func assertFalse(t *testing.T, value bool) {
if value {
t.Errorf("Failed: got true, expected false")
}
}
func assertEqual(t *testing.T, value interface{}, expected interface{}) {
var equal bool
switch value.(type) {
case []byte:
equal = string(value.([]byte)) == string(expected.([]byte))
case int:
equal = value.(int) == expected.(int)
case uint64:
equal = value.(uint64) == expected.(uint64)
case bool:
equal = value.(bool) == expected.(bool)
default:
equal = reflect.DeepEqual(value, expected)
}
if !equal {
_, file, line, _ := runtime.Caller(1)
t.Errorf("Failed: got %v, expected %v (%s:%d)", value, expected, file, line)
}
}