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
|
package xxhash_test
import (
"bytes"
"encoding"
"encoding/binary"
"hash"
"hash/adler32"
"hash/crc32"
"hash/crc64"
"hash/fnv"
"os"
"reflect"
"strconv"
"testing"
"github.com/OneOfOne/xxhash"
)
const inS = `Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
`
var (
in = []byte(inS)
)
const (
expected32 uint32 = 0x6101218F
expected64 uint64 = 0xFFAE31BEBFED7652
)
func Test(t *testing.T) {
t.Log("xxhash backend:", xxhash.Backend)
t.Log("Benchmark string len:", len(inS))
}
func TestHash32(t *testing.T) {
h := xxhash.New32()
h.Write(in)
r := h.Sum32()
if r != expected32 {
t.Errorf("expected 0x%x, got 0x%x.", expected32, r)
}
}
func TestHash32Short(t *testing.T) {
r := xxhash.Checksum32(in)
if r != expected32 {
t.Errorf("expected 0x%x, got 0x%x.", expected32, r)
}
}
func TestWriteStringNil(t *testing.T) {
h32, h64 := xxhash.New32(), xxhash.New64()
for i := 0; i < 1e6; i++ {
h32.WriteString("")
h64.WriteString("")
}
_, _ = h32.Sum32(), h64.Sum64()
}
var testsTable = []struct {
input string
want uint64
}{
{"", 0xef46db3751d8e999},
{"a", 0xd24ec4f1a98c6e5b},
{"as", 0x1c330fb2d66be179},
{"asd", 0x631c37ce72a97393},
{"asdf", 0x415872f599cea71e},
{
// Exactly 63 characters, which exercises all code paths.
"Call me Ishmael. Some years ago--never mind how long precisely-",
0x02a2e85470d6fd96,
},
{"The quick brown fox jumps over the lazy dog http://i.imgur.com/VHQXScB.gif", 0x93267f9820452ead},
{string(in), expected64},
}
// Shamelessly copied from https://github.com/cespare/xxhash/blob/5c37fe3735342a2e0d01c87a907579987c8936cc/xxhash_test.go#L28
func TestSum64(t *testing.T) {
for i, tt := range testsTable {
for chunkSize := 1; chunkSize <= len(tt.input); chunkSize++ {
x := xxhash.New64()
for j := 0; j < len(tt.input); j += chunkSize {
end := j + chunkSize
if end > len(tt.input) {
end = len(tt.input)
}
chunk := tt.input[j:end]
n, err := x.WriteString(chunk)
if err != nil || n != len(chunk) {
t.Fatalf("[i=%d,chunkSize=%d] Write: got (%d, %v); want (%d, nil)",
i, chunkSize, n, err, len(chunk))
}
}
if got := x.Sum64(); got != tt.want {
t.Fatalf("[i=%d,chunkSize=%d] got 0x%x; want 0x%x",
i, chunkSize, got, tt.want)
}
if got := x.Sum64(); got != tt.want {
t.Fatalf("[i=%d,chunkSize=%d] got 0x%x; want 0x%x (called .Sum64 twice)",
i, chunkSize, got, tt.want)
}
var b [8]byte
binary.BigEndian.PutUint64(b[:], tt.want)
if got := x.Sum(nil); !bytes.Equal(got, b[:]) {
t.Fatalf("[i=%d,chunkSize=%d] Sum: got %v; want %v",
i, chunkSize, got, b[:])
}
}
if got := xxhash.ChecksumString64(tt.input); got != tt.want {
t.Fatalf("[i=%d] ChecksumString64: got 0x%x; want 0x%x", i, got, tt.want)
}
}
}
func TestBinaryMarshaling(t *testing.T) {
tests := []struct {
name string
h func() hash.Hash
}{
{name: "xxhash32", h: xxhash.NewHash32},
{name: "xxhash64", h: xxhash.NewHash64},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
d := test.h()
d.Write([]byte("abc"))
b, err := d.(encoding.BinaryMarshaler).MarshalBinary()
if err != nil {
t.Fatal(err)
}
d = test.h()
d.Write([]byte("junk"))
if err := d.(encoding.BinaryUnmarshaler).UnmarshalBinary(b); err != nil {
t.Fatal(err)
}
d.Write([]byte("def"))
expected := test.h()
expected.Write([]byte("abcdef"))
if got, want := d.Sum(nil), expected.Sum(nil); !reflect.DeepEqual(got, want) {
t.Fatalf("after MarshalBinary+UnmarshalBinary, got 0x%x; want 0x%x", got, want)
}
d0 := test.h()
d1 := test.h()
for i := 0; i < 64; i++ {
b, err := d0.(encoding.BinaryMarshaler).MarshalBinary()
if err != nil {
t.Fatal(err)
}
d0 = test.h()
if err := d0.(encoding.BinaryUnmarshaler).UnmarshalBinary(b); err != nil {
t.Fatal(err)
}
if got, want := d0.Sum(nil), d1.Sum(nil); !reflect.DeepEqual(got, want) {
t.Fatalf("after %d Writes, unmarshaled hash gave sum 0x%x; want 0x%x", i, got, want)
}
d0.Write([]byte{'a'})
d1.Write([]byte{'a'})
}
})
}
}
func BenchmarkXXChecksum32(b *testing.B) {
for i := 0; i < b.N; i++ {
xxhash.Checksum32(in)
}
}
func BenchmarkXXChecksumString32(b *testing.B) {
for i := 0; i < b.N; i++ {
xxhash.ChecksumString32(inS)
}
}
func BenchmarkXXSum64(b *testing.B) {
b.Run("Func", func(b *testing.B) {
for i := 0; i < b.N; i++ {
xxhash.Checksum64(in)
}
})
b.Run("Struct", func(b *testing.B) {
h := xxhash.New64()
for i := 0; i < b.N; i++ {
h.Write(in)
h.Sum64()
h.Reset()
}
})
}
func BenchmarkXXSum64Short(b *testing.B) {
k := []byte("Test-key-1000")
b.Run("Func", func(b *testing.B) {
for i := 0; i < b.N; i++ {
xxhash.Checksum64(k)
}
})
b.Run("Struct", func(b *testing.B) {
h := xxhash.New64()
for i := 0; i < b.N; i++ {
h.Write(k)
h.Sum64()
h.Reset()
}
})
}
func BenchmarkXXSum64EvenPoint(b *testing.B) {
if os.Getenv("EP") == "" {
b.SkipNow()
}
for i := 256; i < len(in); i += 256 {
block := in[:i]
b.Run("Func/"+strconv.Itoa(i), func(b *testing.B) {
for i := 0; i < b.N; i++ {
xxhash.Checksum64(block)
}
})
b.Run("Struct/"+strconv.Itoa(i), func(b *testing.B) {
h := xxhash.New64()
for i := 0; i < b.N; i++ {
h.Write(block)
h.Sum64()
h.Reset()
}
})
}
}
func BenchmarkFnv32(b *testing.B) {
var bv []byte
h := fnv.New32()
for i := 0; i < b.N; i++ {
h.Write(in)
bv = h.Sum(nil)
h.Reset()
}
_ = bv
}
func BenchmarkAdler32(b *testing.B) {
for i := 0; i < b.N; i++ {
adler32.Checksum(in)
}
}
func BenchmarkCRC32IEEE(b *testing.B) {
for i := 0; i < b.N; i++ {
crc32.ChecksumIEEE(in)
}
}
func BenchmarkCRC32IEEEString(b *testing.B) {
for i := 0; i < b.N; i++ {
crc32.ChecksumIEEE([]byte(inS))
}
}
var crc64ISO = crc64.MakeTable(crc64.ISO)
func BenchmarkCRC64ISO(b *testing.B) {
for i := 0; i < b.N; i++ {
crc64.Checksum(in, crc64ISO)
}
}
func BenchmarkCRC64ISOString(b *testing.B) {
for i := 0; i < b.N; i++ {
crc64.Checksum([]byte(inS), crc64ISO)
}
}
func BenchmarkCRC32IEEEShort(b *testing.B) {
k := []byte("Test-key-100")
for i := 0; i < b.N; i++ {
crc32.ChecksumIEEE(k)
}
}
func BenchmarkCRC64ISOShort(b *testing.B) {
k := []byte("Test-key-100")
for i := 0; i < b.N; i++ {
crc64.Checksum(k, crc64ISO)
}
}
func BenchmarkFnv64(b *testing.B) {
h := fnv.New64()
for i := 0; i < b.N; i++ {
h.Write(in)
h.Sum(nil)
h.Reset()
}
}
func BenchmarkFnv64Short(b *testing.B) {
var bv []byte
k := []byte("Test-key-100")
for i := 0; i < b.N; i++ {
h := fnv.New64()
h.Write(k)
bv = h.Sum(nil)
}
_ = bv
}
|