-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbench_test.go
More file actions
93 lines (74 loc) · 1.76 KB
/
bench_test.go
File metadata and controls
93 lines (74 loc) · 1.76 KB
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
package farm
import (
"strconv"
"testing"
)
var res32 uint32
var res64 uint64
var res64lo, res64hi uint64
// 256-bytes random string
var buf = make([]byte, 8192)
var sizes = []int{8, 16, 32, 40, 60, 64, 72, 80, 100, 150, 200, 250, 512, 1024, 8192}
func BenchmarkHash32(b *testing.B) {
var r uint32
for _, n := range sizes {
b.Run(strconv.Itoa(n), func(b *testing.B) {
b.SetBytes(int64(n))
for i := 0; i < b.N; i++ {
// record the result to prevent the compiler eliminating the function call
r = Hash32(buf[:n])
}
// store the result to a package level variable so the compiler cannot eliminate the Benchmark itself
res32 = r
})
}
}
func BenchmarkFingerprint32(b *testing.B) {
var r uint32
for _, n := range sizes {
b.Run(strconv.Itoa(n), func(b *testing.B) {
b.SetBytes(int64(n))
for i := 0; i < b.N; i++ {
// record the result to prevent the compiler eliminating the function call
r = Fingerprint32(buf[:n])
}
// store the result to a package level variable so the compiler cannot eliminate the Benchmark itself
res32 = r
})
}
}
func BenchmarkHash64(b *testing.B) {
var r uint64
for _, n := range sizes {
b.Run(strconv.Itoa(n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
r = Hash64(buf[:n])
}
res64 = r
})
}
}
func BenchmarkFingerprint64(b *testing.B) {
var r uint64
for _, n := range sizes {
b.Run(strconv.Itoa(n), func(b *testing.B) {
b.SetBytes(int64(n))
for i := 0; i < b.N; i++ {
r = Fingerprint64(buf[:n])
}
res64 = r
})
}
}
func BenchmarkHash128(b *testing.B) {
var rlo, rhi uint64
for _, n := range sizes {
b.Run(strconv.Itoa(n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
rlo, rhi = Hash128(buf)
}
res64lo = rlo
res64hi = rhi
})
}
}