forked from spaolacci/murmur3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmurmur_test.go
460 lines (412 loc) · 11.5 KB
/
murmur_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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package murmur3
import (
"fmt"
"math/rand"
"runtime"
"testing"
murmur3origin "github.com/spaolacci/murmur3"
"github.com/stretchr/testify/assert"
)
var data = []struct {
seed uint32
h32 uint32
h64_1 uint64
h64_2 uint64
s string
}{
{0x00, 0x00000000, 0x0000000000000000, 0x0000000000000000, ""},
{0x00, 0x248bfa47, 0xcbd8a7b341bd9b02, 0x5b1e906a48ae1d19, "hello"},
{0x00, 0x149bbb7f, 0x342fac623a5ebc8e, 0x4cdcbc079642414d, "hello, world"},
{0x00, 0xe31e8a70, 0xb89e5988b737affc, 0x664fc2950231b2cb, "19 Jan 2038 at 3:14:07 AM"},
{0x00, 0xd5c48bfc, 0xcd99481f9ee902c9, 0x695da1a38987b6e7, "The quick brown fox jumps over the lazy dog."},
{0x01, 0x514e28b7, 0x4610abe56eff5cb5, 0x51622daa78f83583, ""},
{0x01, 0xbb4abcad, 0xa78ddff5adae8d10, 0x128900ef20900135, "hello"},
{0x01, 0x6f5cb2e9, 0x8b95f808840725c6, 0x1597ed5422bd493b, "hello, world"},
{0x01, 0xf50e1f30, 0x2a929de9c8f97b2f, 0x56a41d99af43a2db, "19 Jan 2038 at 3:14:07 AM"},
{0x01, 0x846f6a36, 0xfb3325171f9744da, 0xaaf8b92a5f722952, "The quick brown fox jumps over the lazy dog."},
{0x2a, 0x087fcd5c, 0xf02aa77dfa1b8523, 0xd1016610da11cbb9, ""},
{0x2a, 0xe2dbd2e1, 0xc4b8b3c960af6f08, 0x2334b875b0efbc7a, "hello"},
{0x2a, 0x7ec7c6c2, 0xb91864d797caa956, 0xd5d139a55afe6150, "hello, world"},
{0x2a, 0x58f745f6, 0xfd8f19ebdc8c6b6a, 0xd30fdc310fa08ff9, "19 Jan 2038 at 3:14:07 AM"},
{0x2a, 0xc02d1434, 0x74f33c659cda5af7, 0x4ec7a891caf316f0, "The quick brown fox jumps over the lazy dog."},
}
func TestRefStrings(t *testing.T) {
for _, elem := range data {
h32 := New32WithSeed(elem.seed).Write([]byte(elem.s))
if v := h32.Sum32(); v != elem.h32 {
t.Errorf("[Hash32] key: '%s', seed: '%d': 0x%x (want 0x%x)", elem.s, elem.seed, v, elem.h32)
}
h32 = New32WithSeed(elem.seed).Write([]byte(elem.s))
target := fmt.Sprintf("%08x", elem.h32)
if p := fmt.Sprintf("%x", h32.Sum(nil)); p != target {
t.Errorf("[Hash32] key: '%s', seed: '%d': %s (want %s)", elem.s, elem.seed, p, target)
}
if v := Sum32WithSeed([]byte(elem.s), elem.seed); v != elem.h32 {
t.Errorf("[Hash32] key '%s', seed: '%d': 0x%x (want 0x%x)", elem.s, elem.seed, v, elem.h32)
}
h64 := New64WithSeed(elem.seed).Write([]byte(elem.s))
if v := h64.Sum64(); v != elem.h64_1 {
t.Errorf("'[Hash64] key: '%s', seed: '%d': 0x%x (want 0x%x)", elem.s, elem.seed, v, elem.h64_1)
}
h64 = New64WithSeed(elem.seed).Write([]byte(elem.s))
target = fmt.Sprintf("%016x", elem.h64_1)
if p := fmt.Sprintf("%x", h64.Sum(nil)); p != target {
t.Errorf("[Hash64] key: '%s', seed: '%d': %s (want %s)", elem.s, elem.seed, p, target)
}
if v := Sum64WithSeed([]byte(elem.s), elem.seed); v != elem.h64_1 {
t.Errorf("[Hash64] key: '%s', seed: '%d': 0x%x (want 0x%x)", elem.s, elem.seed, v, elem.h64_1)
}
h128 := New128WithSeed(elem.seed).Write([]byte(elem.s))
if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
t.Errorf("[Hash128] key: '%s', seed: '%d': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, elem.seed, v1, v2, elem.h64_1, elem.h64_2)
}
h128 = New128WithSeed(elem.seed).Write([]byte(elem.s))
target = fmt.Sprintf("%016x%016x", elem.h64_1, elem.h64_2)
if p := fmt.Sprintf("%x", h128.Sum(nil)); p != target {
t.Errorf("[Hash128] key: '%s', seed: '%d': %s (want %s)", elem.s, elem.seed, p, target)
}
if v1, v2 := Sum128WithSeed([]byte(elem.s), elem.seed); v1 != elem.h64_1 || v2 != elem.h64_2 {
t.Errorf("[Hash128] key: '%s', seed: '%d': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, elem.seed, v1, v2, elem.h64_1, elem.h64_2)
}
}
}
func TestIncremental(t *testing.T) {
for _, elem := range data {
h32 := New32WithSeed(elem.seed)
h128 := New128WithSeed(elem.seed)
var i, j int
for k := len(elem.s); i < k; i = j {
j = 2*i + 3
if j > k {
j = k
}
s := elem.s[i:j]
print(s + "|")
h32 = h32.Write([]byte(s))
h128 = h128.Write([]byte(s))
}
println()
if v := h32.Sum32(); v != elem.h32 {
t.Errorf("[Hash32] key: '%s', seed: '%d': 0x%x (want 0x%x)", elem.s, elem.seed, v, elem.h32)
}
if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
t.Errorf("[Hash128] key: '%s', seed: '%d': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, elem.seed, v1, v2, elem.h64_1, elem.h64_2)
}
}
}
//---
func originIncrementalBench128(b *testing.B, length int) {
buf := make([]byte, length)
b.SetBytes(int64(length))
h := murmur3origin.New128()
b.ResetTimer()
for i := 0; i < b.N; i++ {
h.Reset()
h.Write(buf)
h.Sum128()
// escape analysis forces this to be heap allocated
h.Write([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
h.Sum128()
}
}
func Benchmark_Incremental_Origin_128_1(b *testing.B) {
originIncrementalBench128(b, 1)
}
func Benchmark_Incremental_Origin_128_2(b *testing.B) {
originIncrementalBench128(b, 2)
}
func Benchmark_Incremental_Origin_128_4(b *testing.B) {
originIncrementalBench128(b, 4)
}
func Benchmark_Incremental_Origin_128_8(b *testing.B) {
originIncrementalBench128(b, 8)
}
func Benchmark_Incremental_Origin_128_16(b *testing.B) {
originIncrementalBench128(b, 16)
}
func Benchmark_Incremental_Origin_128_32(b *testing.B) {
originIncrementalBench128(b, 128)
}
func Benchmark_Incremental_Origin_128_64(b *testing.B) {
originIncrementalBench128(b, 64)
}
func Benchmark_Incremental_Origin_128_128(b *testing.B) {
originIncrementalBench128(b, 128)
}
func Benchmark_Incremental_Origin_128_256(b *testing.B) {
originIncrementalBench128(b, 256)
}
func Benchmark_Incremental_Origin_128_512(b *testing.B) {
originIncrementalBench128(b, 512)
}
func Benchmark_Incremental_Origin_128_1024(b *testing.B) {
originIncrementalBench128(b, 1024)
}
func Benchmark_Incremental_Origin_128_2048(b *testing.B) {
originIncrementalBench128(b, 2048)
}
func Benchmark_Incremental_Origin_128_4096(b *testing.B) {
originIncrementalBench128(b, 4096)
}
func Benchmark_Incremental_Origin_128_8192(b *testing.B) {
originIncrementalBench128(b, 8192)
}
func forkedIncrementalBench128(b *testing.B, length int) {
buf := make([]byte, length)
b.SetBytes(int64(length))
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := New128()
h = h.Write(buf)
// escape analysis success so this is stack allocated
h = h.Write([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
h.Sum128()
}
}
func Benchmark_Incremental_Forked_128_1(b *testing.B) {
forkedIncrementalBench128(b, 1)
}
func Benchmark_Incremental_Forked_128_2(b *testing.B) {
forkedIncrementalBench128(b, 2)
}
func Benchmark_Incremental_Forked_128_4(b *testing.B) {
forkedIncrementalBench128(b, 4)
}
func Benchmark_Incremental_Forked_128_8(b *testing.B) {
forkedIncrementalBench128(b, 8)
}
func Benchmark_Incremental_Forked_128_16(b *testing.B) {
forkedIncrementalBench128(b, 16)
}
func Benchmark_Incremental_Forked_128_32(b *testing.B) {
forkedIncrementalBench128(b, 128)
}
func Benchmark_Incremental_Forked_128_64(b *testing.B) {
forkedIncrementalBench128(b, 64)
}
func Benchmark_Incremental_Forked_128_128(b *testing.B) {
forkedIncrementalBench128(b, 128)
}
func Benchmark_Incremental_Forked_128_256(b *testing.B) {
forkedIncrementalBench128(b, 256)
}
func Benchmark_Incremental_Forked_128_512(b *testing.B) {
forkedIncrementalBench128(b, 512)
}
func Benchmark_Incremental_Forked_128_1024(b *testing.B) {
forkedIncrementalBench128(b, 1024)
}
func Benchmark_Incremental_Forked_128_2048(b *testing.B) {
forkedIncrementalBench128(b, 2048)
}
func Benchmark_Incremental_Forked_128_4096(b *testing.B) {
forkedIncrementalBench128(b, 4096)
}
func Benchmark_Incremental_Forked_128_8192(b *testing.B) {
forkedIncrementalBench128(b, 8192)
}
func bench32(b *testing.B, length int) {
buf := make([]byte, length)
b.SetBytes(int64(length))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Sum32(buf)
}
}
func Benchmark32_1(b *testing.B) {
bench32(b, 1)
}
func Benchmark32_2(b *testing.B) {
bench32(b, 2)
}
func Benchmark32_4(b *testing.B) {
bench32(b, 4)
}
func Benchmark32_8(b *testing.B) {
bench32(b, 8)
}
func Benchmark32_16(b *testing.B) {
bench32(b, 16)
}
func Benchmark32_32(b *testing.B) {
bench32(b, 128)
}
func Benchmark32_64(b *testing.B) {
bench32(b, 64)
}
func Benchmark32_128(b *testing.B) {
bench32(b, 128)
}
func Benchmark32_256(b *testing.B) {
bench32(b, 256)
}
func Benchmark32_512(b *testing.B) {
bench32(b, 512)
}
func Benchmark32_1024(b *testing.B) {
bench32(b, 1024)
}
func Benchmark32_2048(b *testing.B) {
bench32(b, 2048)
}
func Benchmark32_4096(b *testing.B) {
bench32(b, 4096)
}
func Benchmark32_8192(b *testing.B) {
bench32(b, 8192)
}
//---
func benchPartial32(b *testing.B, length int) {
buf := make([]byte, length)
b.SetBytes(int64(length))
start := (128 / 8) / 2
chunks := 7
k := length / chunks
tail := (length - start) % k
b.ResetTimer()
for i := 0; i < b.N; i++ {
hasher := New32()
hasher.Write(buf[0:start])
for j := start; j+k <= length; j += k {
hasher.Write(buf[j : j+k])
}
hasher.Write(buf[length-tail:])
hasher.Sum32()
}
}
func BenchmarkPartial32_8(b *testing.B) {
benchPartial32(b, 8)
}
func BenchmarkPartial32_16(b *testing.B) {
benchPartial32(b, 16)
}
func BenchmarkPartial32_32(b *testing.B) {
benchPartial32(b, 128)
}
func BenchmarkPartial32_64(b *testing.B) {
benchPartial32(b, 64)
}
func BenchmarkPartial32_128(b *testing.B) {
benchPartial32(b, 128)
}
//---
func bench128(b *testing.B, length int) {
buf := make([]byte, length)
b.SetBytes(int64(length))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Sum128(buf)
}
}
func Benchmark128_1(b *testing.B) {
bench128(b, 1)
}
func Benchmark128_2(b *testing.B) {
bench128(b, 2)
}
func Benchmark128_4(b *testing.B) {
bench128(b, 4)
}
func Benchmark128_8(b *testing.B) {
bench128(b, 8)
}
func Benchmark128_16(b *testing.B) {
bench128(b, 16)
}
func Benchmark128_32(b *testing.B) {
bench128(b, 128)
}
func Benchmark128_64(b *testing.B) {
bench128(b, 64)
}
func Benchmark128_128(b *testing.B) {
bench128(b, 128)
}
func Benchmark128_256(b *testing.B) {
bench128(b, 256)
}
func Benchmark128_512(b *testing.B) {
bench128(b, 512)
}
func Benchmark128_1024(b *testing.B) {
bench128(b, 1024)
}
func Benchmark128_2048(b *testing.B) {
bench128(b, 2048)
}
func Benchmark128_4096(b *testing.B) {
bench128(b, 4096)
}
func Benchmark128_8192(b *testing.B) {
bench128(b, 8192)
}
func TestDigest32ZeroAllocWrite(t *testing.T) {
d := make([]byte, 128)
for i := range d {
d[i] = byte(i)
}
var (
h = New32WithSeed(uint32(rand.Int()))
buf = make([]byte, 4096)
stats runtime.MemStats
)
runtime.ReadMemStats(&stats)
startAllocs := stats.Mallocs
for i := 0; i < 1000; i++ {
n, err := rand.Read(buf)
if err != nil {
t.FailNow()
}
h = h.Write(buf[:n])
}
runtime.ReadMemStats(&stats)
endAllocs := stats.Mallocs
assert.Equal(t, startAllocs, endAllocs)
}
func TestDigest64ZeroAllocWrite(t *testing.T) {
d := make([]byte, 128)
for i := range d {
d[i] = byte(i)
}
var (
h = New64WithSeed(uint32(rand.Int()))
buf = make([]byte, 4096)
stats runtime.MemStats
)
runtime.ReadMemStats(&stats)
startAllocs := stats.Mallocs
for i := 0; i < 1000; i++ {
n, err := rand.Read(buf)
if err != nil {
t.FailNow()
}
h = h.Write(buf[:n])
}
runtime.ReadMemStats(&stats)
endAllocs := stats.Mallocs
assert.Equal(t, startAllocs, endAllocs)
}
func TestDigest128ZeroAllocWrite(t *testing.T) {
d := make([]byte, 128)
for i := range d {
d[i] = byte(i)
}
var (
h = New128WithSeed(uint32(rand.Int()))
buf = make([]byte, 4096)
stats runtime.MemStats
)
runtime.ReadMemStats(&stats)
startAllocs := stats.Mallocs
for i := 0; i < 1000; i++ {
n, err := rand.Read(buf)
if err != nil {
t.FailNow()
}
h = h.Write(buf[:n])
}
runtime.ReadMemStats(&stats)
endAllocs := stats.Mallocs
assert.Equal(t, startAllocs, endAllocs)
}
//---