| 1 |
package trie |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/rand" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
var stringKeys [1000]string |
| 9 |
const bytesPerKey = 30 |
| 10 |
|
| 11 |
var pathKeys [1000]string |
| 12 |
const partsPerKey = 3 |
| 13 |
const bytesPerPart = 10 |
| 14 |
|
| 15 |
func init() { |
| 16 |
|
| 17 |
for i := 0; i < len(stringKeys); i++ { |
| 18 |
key := make([]byte, bytesPerKey) |
| 19 |
if _, err := rand.Read(key); err != nil { |
| 20 |
panic("error generating random byte slice") |
| 21 |
} |
| 22 |
stringKeys[i] = string(key) |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
for i := 0; i < len(pathKeys); i++ { |
| 27 |
var key string |
| 28 |
for j := 0; j < partsPerKey; j++ { |
| 29 |
key += "/" |
| 30 |
part := make([]byte, bytesPerPart) |
| 31 |
if _, err := rand.Read(part); err != nil { |
| 32 |
panic("error generating random byte slice") |
| 33 |
} |
| 34 |
key += string(part) |
| 35 |
} |
| 36 |
pathKeys[i] = key |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func BenchmarkRuneTriePutStringKey(b *testing.B) { |
| 46 |
trie := NewRuneTrie() |
| 47 |
b.ResetTimer() |
| 48 |
b.ReportAllocs() |
| 49 |
for i := 0; i < b.N; i++ { |
| 50 |
trie.Put(stringKeys[i%len(stringKeys)], i) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func BenchmarkRuneTrieGetStringKey(b *testing.B) { |
| 55 |
trie := NewRuneTrie() |
| 56 |
for i := 0; i < b.N; i++ { |
| 57 |
trie.Put(stringKeys[i%len(stringKeys)], i) |
| 58 |
} |
| 59 |
b.ResetTimer() |
| 60 |
b.ReportAllocs() |
| 61 |
for i := 0; i < b.N; i++ { |
| 62 |
trie.Get(stringKeys[i%len(stringKeys)]) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func BenchmarkRuneTriePutPathKey(b *testing.B) { |
| 69 |
trie := NewRuneTrie() |
| 70 |
b.ResetTimer() |
| 71 |
b.ReportAllocs() |
| 72 |
for i := 0; i < b.N; i++ { |
| 73 |
trie.Put(pathKeys[i%len(pathKeys)], i) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func BenchmarkRuneTrieGetPathKey(b *testing.B) { |
| 78 |
trie := NewRuneTrie() |
| 79 |
for i := 0; i < b.N; i++ { |
| 80 |
trie.Put(pathKeys[i%len(pathKeys)], i) |
| 81 |
} |
| 82 |
b.ResetTimer() |
| 83 |
b.ReportAllocs() |
| 84 |
for i := 0; i < b.N; i++ { |
| 85 |
trie.Get(pathKeys[i%len(pathKeys)]) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
func BenchmarkPathTriePutStringKey(b *testing.B) { |
| 95 |
trie := NewPathTrie() |
| 96 |
b.ResetTimer() |
| 97 |
b.ReportAllocs() |
| 98 |
for i := 0; i < b.N; i++ { |
| 99 |
trie.Put(stringKeys[i%len(stringKeys)], i) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func BenchmarkPathTrieGetStringKey(b *testing.B) { |
| 104 |
trie := NewPathTrie() |
| 105 |
for i := 0; i < b.N; i++ { |
| 106 |
trie.Put(stringKeys[i%len(stringKeys)], i) |
| 107 |
} |
| 108 |
b.ResetTimer() |
| 109 |
b.ReportAllocs() |
| 110 |
for i := 0; i < b.N; i++ { |
| 111 |
trie.Get(stringKeys[i%len(stringKeys)]) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func BenchmarkPathTriePutPathKey(b *testing.B) { |
| 118 |
trie := NewPathTrie() |
| 119 |
b.ResetTimer() |
| 120 |
b.ReportAllocs() |
| 121 |
for i := 0; i < b.N; i++ { |
| 122 |
trie.Put(pathKeys[i%len(pathKeys)], i) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func BenchmarkPathTrieGetPathKey(b *testing.B) { |
| 127 |
trie := NewPathTrie() |
| 128 |
for i := 0; i < b.N; i++ { |
| 129 |
trie.Put(pathKeys[i%len(pathKeys)], i) |
| 130 |
} |
| 131 |
b.ResetTimer() |
| 132 |
b.ReportAllocs() |
| 133 |
for i := 0; i < b.N; i++ { |
| 134 |
trie.Get(pathKeys[i%len(pathKeys)]) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
func BenchmarkPathSegmenter(b *testing.B) { |
| 141 |
b.ResetTimer() |
| 142 |
b.ReportAllocs() |
| 143 |
for j := 0; j < b.N; j++ { |
| 144 |
for part, i := PathSegmenter(pathKeys[j%len(pathKeys)], 0); ; part, i = PathSegmenter(pathKeys[j%len(pathKeys)], i) { |
| 145 |
var _ = part |
| 146 |
if i == -1 { |
| 147 |
break |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |