-
Notifications
You must be signed in to change notification settings - Fork 17
/
hashing_test.go
316 lines (270 loc) · 6.83 KB
/
hashing_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
package benchmark
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"io"
"testing"
xxhashcespare "github.com/cespare/xxhash/v2"
"github.com/dchest/siphash"
blake2bsimd "github.com/minio/blake2b-simd"
"github.com/minio/highwayhash"
xxhash32pier "github.com/pierrec/xxHash/xxHash32"
xxhash64pier "github.com/pierrec/xxHash/xxHash64"
murmur3spaolacci "github.com/spaolacci/murmur3"
murmur3twmb "github.com/twmb/murmur3"
xxhash32vova "github.com/vova616/xxhash"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/sha3"
)
const hashBufferSize = 8
// hash is the common interface implemented by all hash functions.
type hash interface {
io.Writer
Sum(b []byte) []byte
}
// hash32 is the common interface implemented by all 32-bit hash functions.
type hash32 interface {
io.Writer
Sum32() uint32
}
// hash64 is the common interface implemented by all 64-bit hash functions.
type hash64 interface {
io.Writer
Sum64() uint64
}
func benchmarkHash[T hash](b *testing.B, hash func() T, length int64) {
data := make([]byte, length)
b.SetBytes(length)
var c uint64
for i := 0; i < b.N; i++ {
h := hash()
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
b := h.Sum(nil)
c += uint64(b[0])
}
if c == 0 { // unlikely that this will ever fail but it prevents optimizing away the Sum() call
b.Fail()
}
}
func benchmarkHash64[T hash64](b *testing.B, hash func() T, length int64) {
data := make([]byte, length)
b.SetBytes(length)
var c uint64
for i := 0; i < b.N; i++ {
h := hash()
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
c += h.Sum64()
}
if c == 0 { // unlikely that this will ever fail but it prevents optimizing away the Sum() call
b.Fail()
}
}
func benchmarkHash64seed[T hash64](b *testing.B, hash func(uint64) T, length int64) {
data := make([]byte, length)
b.SetBytes(length)
var c uint64
for i := 0; i < b.N; i++ {
h := hash(1471)
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
c += h.Sum64()
}
if c == 0 { // unlikely that this will ever fail but it prevents optimizing away the Sum() call
b.Fail()
}
}
func benchmarkHash32seed[T hash32](b *testing.B, hash func(uint32) T, length int64) {
data := make([]byte, length)
b.SetBytes(length)
for i := 0; i < b.N; i++ {
h := hash(1471)
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
_ = h.Sum32()
}
}
func benchmarkHash64to32[T hash64](b *testing.B, hash func() T, length int64) {
data := make([]byte, length)
b.SetBytes(length)
for i := 0; i < b.N; i++ {
h := hash()
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
var b []byte
s := h.Sum64()
_ = append(
b,
byte(s>>56),
byte(s>>48),
byte(s>>40),
byte(s>>32),
)
}
}
func benchmarkHash64to16[T hash64](b *testing.B, hash func() T, length int64) {
data := make([]byte, length)
b.SetBytes(length)
for i := 0; i < b.N; i++ {
h := hash()
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
var b []byte
s := h.Sum64()
_ = append(
b,
byte(s>>56),
byte(s>>48),
)
}
}
func benchmarkHash64to8[T hash64](b *testing.B, hash func() T, length int64) {
data := make([]byte, length)
b.SetBytes(length)
for i := 0; i < b.N; i++ {
h := hash()
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
s := h.Sum64()
_ = byte(s >> 56)
}
}
func benchmarkHashKeyError[T hash](b *testing.B, hash func([]byte) (T, error), length int64) {
data := make([]byte, length)
b.SetBytes(length)
key := make([]byte, 16)
var c uint64
for i := 0; i < b.N; i++ {
h, _ := hash(key)
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
b := h.Sum(nil)
c += uint64(b[0])
}
if c == 0 { // unlikely that this will ever fail but it prevents optimizing away the Sum() call
b.Fail()
}
}
func benchmarkHashKey64[T hash64](b *testing.B, hash func([]byte) T, length int64) {
data := make([]byte, length)
b.SetBytes(length)
key := make([]byte, 16)
var c uint64
for i := 0; i < b.N; i++ {
h := hash(key)
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
c += h.Sum64()
}
if c == 0 { // unlikely that this will ever fail but it prevents optimizing away the Sum() call
b.Fail()
}
}
func benchmarkHashKey64Error[T hash64](b *testing.B, hash func([]byte) (T, error), length int64) {
data := make([]byte, length)
b.SetBytes(length)
key := make([]byte, 32)
var c uint64
for i := 0; i < b.N; i++ {
h, _ := hash(key)
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
c += h.Sum64()
}
if c == 0 { // unlikely that this will ever fail but it prevents optimizing away the Sum() call
b.Fail()
}
}
func benchmarkHashKey32[T hash32](b *testing.B, hash func([]byte) hash32, length int64) {
data := make([]byte, length)
b.SetBytes(length)
key := make([]byte, 8)
for i := 0; i < b.N; i++ {
h := hash(key)
_, err := h.Write(data[:])
if err != nil {
panic(err)
}
_ = h.Sum32()
}
}
func BenchmarkHashing64MD5(b *testing.B) {
benchmarkHash(b, md5.New, hashBufferSize)
}
func BenchmarkHashing64SHA1(b *testing.B) {
benchmarkHash(b, sha1.New, hashBufferSize)
}
func BenchmarkHashing64SHA256(b *testing.B) {
benchmarkHash(b, sha256.New, hashBufferSize)
}
func BenchmarkHashing64SHA3B224(b *testing.B) {
benchmarkHash(b, sha3.New224, hashBufferSize)
}
func BenchmarkHashing64SHA3B256(b *testing.B) {
benchmarkHash(b, sha3.New256, hashBufferSize)
}
func BenchmarkHashing64RIPEMD160(b *testing.B) {
benchmarkHash(b, ripemd160.New, hashBufferSize)
}
func BenchmarkHashing64Blake2B(b *testing.B) {
benchmarkHashKeyError(b, blake2b.New256, hashBufferSize)
}
func BenchmarkHashing64Blake2BSimd(b *testing.B) {
benchmarkHash(b, blake2bsimd.New256, hashBufferSize)
}
func BenchmarkHashing64Murmur3(b *testing.B) {
benchmarkHash64(b, murmur3spaolacci.New64, hashBufferSize)
}
func BenchmarkHashing64Murmur3Twmb(b *testing.B) {
benchmarkHash64(b, murmur3twmb.New64, hashBufferSize)
}
func BenchmarkHashing64SipHash(b *testing.B) {
benchmarkHashKey64(b, siphash.New, hashBufferSize)
}
func BenchmarkHashing64XXHash(b *testing.B) {
benchmarkHash64(b, xxhashcespare.New, hashBufferSize)
}
func BenchmarkHashing64XXHashpier(b *testing.B) {
benchmarkHash64seed(b, xxhash64pier.New, hashBufferSize)
}
func BenchmarkHashing64HighwayHash(b *testing.B) {
benchmarkHashKey64Error(b, highwayhash.New64, hashBufferSize)
}
func BenchmarkHashing32XXHashvova(b *testing.B) {
benchmarkHash32seed(b, xxhash32vova.New, hashBufferSize)
}
func BenchmarkHashing32XXHashpier(b *testing.B) {
benchmarkHash32seed(b, xxhash32pier.New, hashBufferSize)
}
func BenchmarkHashing32XXHash(b *testing.B) {
benchmarkHash64to32(b, xxhashcespare.New, hashBufferSize)
}
func BenchmarkHashing16XXHash(b *testing.B) {
benchmarkHash64to16(b, xxhashcespare.New, hashBufferSize)
}
func BenchmarkHashing8XXHash(b *testing.B) {
benchmarkHash64to8(b, xxhashcespare.New, hashBufferSize)
}