-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbench_test.go
170 lines (114 loc) · 5.12 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"crypto/sha1"
"encoding/binary"
"hash/fnv"
"strconv"
"testing"
"blainsmith.com/go/seahash"
xxhash "github.com/OneOfOne/xxhash"
xxhashfast "github.com/cespare/xxhash"
dchestsip "github.com/dchest/siphash"
"github.com/dgryski/go-farm"
cfarmhash "github.com/dgryski/go-farmhash"
"github.com/dgryski/go-highway"
"github.com/dgryski/go-marvin32"
"github.com/dgryski/go-metro"
"github.com/dgryski/go-sip13"
"github.com/dgryski/go-spooky"
"github.com/dgryski/go-stadtx"
"github.com/dgryski/go-t1ha"
"github.com/dgryski/go-wyhash"
"github.com/dgryski/go-xxh3"
"github.com/mmcloughlin/meow"
"github.com/rbastic/go-zaphod64"
"github.com/surge/cityhash"
zxxh3 "github.com/zeebo/xxh3"
"golang.org/x/crypto/blake2b"
tsip "github.com/dgryski/trifles/tsip/go"
)
// Written in 2012 by Dmitry Chestnykh.
// Expanded to include multiple hash functions by Damian Gryski, 2015.
//
// To the extent possible under law, the author have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
// http://creativecommons.org/publicdomain/zero/1.0/
var (
key0, key1 uint64
buf = make([]byte, 8<<10)
)
var hspooky = func(k []byte) uint64 { return spooky.Hash64(k) }
func BenchmarkSpooky(b *testing.B) { benchmarkHash(b, "Spooky", hspooky) }
var hsiphash = func(k []byte) uint64 { return dchestsip.Hash(0, 0, k) }
func BenchmarkSipHash(b *testing.B) { benchmarkHash(b, "SipHash", hsiphash) }
var hfarm = func(k []byte) uint64 { return farm.Fingerprint64(k) }
func BenchmarkFarm(b *testing.B) { benchmarkHash(b, "Farm", hfarm) }
var hcfarmhash = func(k []byte) uint64 { return cfarmhash.Hash64(k) }
func BenchmarkCFarm(b *testing.B) { benchmarkHash(b, "CFarm", hcfarmhash) }
var hcity = func(k []byte) uint64 { return cityhash.CityHash64(k, uint32(len(k))) }
func BenchmarkCity(b *testing.B) { benchmarkHash(b, "City", hcity) }
var hmetro = func(k []byte) uint64 { return metro.Hash64(k, 0) }
func BenchmarkMetro(b *testing.B) { benchmarkHash(b, "Metro", hmetro) }
var hxxhash = func(k []byte) uint64 { return xxhash.Checksum64(k) }
func BenchmarkXXHash(b *testing.B) { benchmarkHash(b, "XXHash", hxxhash) }
var hxxhashfast = func(k []byte) uint64 { return xxhashfast.Sum64(k) }
func BenchmarkXXFast(b *testing.B) { benchmarkHash(b, "XXFast", hxxhashfast) }
var high = func(k []byte) uint64 { return highway.Hash(highway.Lanes{}, k) }
func BenchmarkHighway(b *testing.B) { benchmarkHash(b, "Highway", high) }
var marvin = func(k []byte) uint64 { return uint64(marvin32.Sum32(0, k)) }
func BenchmarkMarvin32(b *testing.B) { benchmarkHash(b, "Marvin32", marvin) }
var sip13hash = func(k []byte) uint64 { return sip13.Sum64(0, 0, k) }
func BenchmarkSip13Hash(b *testing.B) { benchmarkHash(b, "Sip13", sip13hash) }
var fnv64 = func(k []byte) uint64 {
h := fnv.New64a()
h.Write(k)
return h.Sum64()
}
func BenchmarkFNV1(b *testing.B) { benchmarkHash(b, "fnv1a", fnv64) }
var ht1ha = func(k []byte) uint64 { return t1ha.Sum64(k, 0) }
func BenchmarkT1ha(b *testing.B) { benchmarkHash(b, "T1ha", ht1ha) }
var zaphodSeed zaphod64.State
var hzaphod64 = func(k []byte) uint64 { return uint64(zaphod64.HashWithState(&zaphodSeed, k, uint64(len(k)))) }
func BenchmarkZaphod64(b *testing.B) { benchmarkHash(b, "Zaphod64", hzaphod64) }
var stadtxState stadtx.State
var hstadtx = func(k []byte) uint64 { return stadtx.Hash(&stadtxState, k) }
func BenchmarkStadtx(b *testing.B) { benchmarkHash(b, "Stadtx", hstadtx) }
var htsip = func(k []byte) uint64 { return tsip.HashASM(0, 0, k) }
func BenchmarkTsip(b *testing.B) { benchmarkHash(b, "Tsip", htsip) }
var hwyhash = func(k []byte) uint64 { return wyhash.Hash(k, 0) }
func BenchmarkWyhash(b *testing.B) { benchmarkHash(b, "Wyhash", hwyhash) }
var hxxh3 = func(k []byte) uint64 { return xxh3.Hash(k, 0) }
func BenchmarkXXH3(b *testing.B) { benchmarkHash(b, "XXH3", hxxh3) }
var hzxxh3 = func(k []byte) uint64 { return zxxh3.Hash(k) }
func BenchmarkZeeboXXH3(b *testing.B) { benchmarkHash(b, "ZeeboXXH3", hzxxh3) }
var hseahash = func(k []byte) uint64 { return seahash.Sum64(k) }
func BenchmarkSeahash(b *testing.B) { benchmarkHash(b, "Seahash", hseahash) }
var hsha1 = func(k []byte) uint64 {
b := sha1.Sum(k)
return binary.LittleEndian.Uint64(b[:8])
}
func BenchmarkSHA1(b *testing.B) { benchmarkHash(b, "SHA1", hsha1) }
var hblake = func(k []byte) uint64 {
b := blake2b.Sum256(k)
return binary.LittleEndian.Uint64(b[:8])
}
func BenchmarkBlake2b(b *testing.B) { benchmarkHash(b, "Blake", hblake) }
var hmeow = func(k []byte) uint64 {
b := meow.Checksum(0, k)
return binary.LittleEndian.Uint64(b[:8])
}
func BenchmarkMeow(b *testing.B) { benchmarkHash(b, "Meow", hmeow) }
func benchmarkHash(b *testing.B, str string, h func([]byte) uint64) {
var sizes = []int{1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 1024, 8192}
for _, n := range sizes {
b.Run(strconv.Itoa(n), func(b *testing.B) { benchmarkHashn(b, int64(n), h) })
}
}
var total uint64
func benchmarkHashn(b *testing.B, size int64, h func([]byte) uint64) {
b.SetBytes(size)
for i := 0; i < b.N; i++ {
total += h(buf[:size])
}
}