-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
bench_test.go
151 lines (130 loc) · 3.08 KB
/
bench_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
package trie
import (
"crypto/rand"
"testing"
)
var stringKeys [1000]string // random string keys
const bytesPerKey = 30
var pathKeys [1000]string // random /paths/of/parts keys
const partsPerKey = 3 // (e.g. /a/b/c has parts /a, /b, /c)
const bytesPerPart = 10
func init() {
// string keys
for i := 0; i < len(stringKeys); i++ {
key := make([]byte, bytesPerKey)
if _, err := rand.Read(key); err != nil {
panic("error generating random byte slice")
}
stringKeys[i] = string(key)
}
// path keys
for i := 0; i < len(pathKeys); i++ {
var key string
for j := 0; j < partsPerKey; j++ {
key += "/"
part := make([]byte, bytesPerPart)
if _, err := rand.Read(part); err != nil {
panic("error generating random byte slice")
}
key += string(part)
}
pathKeys[i] = key
}
}
// RuneTrie
///////////////////////////////////////////////////////////////////////////////
// string keys
func BenchmarkRuneTriePutStringKey(b *testing.B) {
trie := NewRuneTrie()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
trie.Put(stringKeys[i%len(stringKeys)], i)
}
}
func BenchmarkRuneTrieGetStringKey(b *testing.B) {
trie := NewRuneTrie()
for i := 0; i < b.N; i++ {
trie.Put(stringKeys[i%len(stringKeys)], i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
trie.Get(stringKeys[i%len(stringKeys)])
}
}
// path keys
func BenchmarkRuneTriePutPathKey(b *testing.B) {
trie := NewRuneTrie()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
trie.Put(pathKeys[i%len(pathKeys)], i)
}
}
func BenchmarkRuneTrieGetPathKey(b *testing.B) {
trie := NewRuneTrie()
for i := 0; i < b.N; i++ {
trie.Put(pathKeys[i%len(pathKeys)], i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
trie.Get(pathKeys[i%len(pathKeys)])
}
}
// PathTrie
///////////////////////////////////////////////////////////////////////////////
// string keys
func BenchmarkPathTriePutStringKey(b *testing.B) {
trie := NewPathTrie()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
trie.Put(stringKeys[i%len(stringKeys)], i)
}
}
func BenchmarkPathTrieGetStringKey(b *testing.B) {
trie := NewPathTrie()
for i := 0; i < b.N; i++ {
trie.Put(stringKeys[i%len(stringKeys)], i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
trie.Get(stringKeys[i%len(stringKeys)])
}
}
// path keys
func BenchmarkPathTriePutPathKey(b *testing.B) {
trie := NewPathTrie()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
trie.Put(pathKeys[i%len(pathKeys)], i)
}
}
func BenchmarkPathTrieGetPathKey(b *testing.B) {
trie := NewPathTrie()
for i := 0; i < b.N; i++ {
trie.Put(pathKeys[i%len(pathKeys)], i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
trie.Get(pathKeys[i%len(pathKeys)])
}
}
// benchmark PathSegmenter
func BenchmarkPathSegmenter(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for j := 0; j < b.N; j++ {
for part, i := PathSegmenter(pathKeys[j%len(pathKeys)], 0); ; part, i = PathSegmenter(pathKeys[j%len(pathKeys)], i) {
var _ = part // NoOp 'use' the key part
if i == -1 {
break
}
}
}
}