| 1 |
package xxhashbench |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"hash/crc32" |
| 6 |
"hash/fnv" |
| 7 |
"testing" |
| 8 |
|
| 9 |
OneOfOne "github.com/OneOfOne/xxhash" |
| 10 |
"github.com/cespare/xxhash/v2" |
| 11 |
"github.com/spaolacci/murmur3" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
var sink uint64 |
| 19 |
|
| 20 |
var benchmarks = []struct { |
| 21 |
name string |
| 22 |
directBytes func([]byte) uint64 |
| 23 |
directString func(string) uint64 |
| 24 |
digestBytes func([]byte) uint64 |
| 25 |
digestString func(string) uint64 |
| 26 |
}{ |
| 27 |
{ |
| 28 |
name: "xxhash", |
| 29 |
directBytes: xxhash.Sum64, |
| 30 |
directString: xxhash.Sum64String, |
| 31 |
digestBytes: func(b []byte) uint64 { |
| 32 |
h := xxhash.New() |
| 33 |
h.Write(b) |
| 34 |
return h.Sum64() |
| 35 |
}, |
| 36 |
digestString: func(s string) uint64 { |
| 37 |
h := xxhash.New() |
| 38 |
h.WriteString(s) |
| 39 |
return h.Sum64() |
| 40 |
}, |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "OneOfOne", |
| 44 |
directBytes: OneOfOne.Checksum64, |
| 45 |
directString: OneOfOne.ChecksumString64, |
| 46 |
digestBytes: func(b []byte) uint64 { |
| 47 |
h := OneOfOne.New64() |
| 48 |
h.Write(b) |
| 49 |
return h.Sum64() |
| 50 |
}, |
| 51 |
digestString: func(s string) uint64 { |
| 52 |
h := OneOfOne.New64() |
| 53 |
h.WriteString(s) |
| 54 |
return h.Sum64() |
| 55 |
}, |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "murmur3", |
| 59 |
directBytes: murmur3.Sum64, |
| 60 |
directString: func(s string) uint64 { |
| 61 |
return murmur3.Sum64([]byte(s)) |
| 62 |
}, |
| 63 |
digestBytes: func(b []byte) uint64 { |
| 64 |
h := murmur3.New64() |
| 65 |
h.Write(b) |
| 66 |
return h.Sum64() |
| 67 |
}, |
| 68 |
digestString: func(s string) uint64 { |
| 69 |
h := murmur3.New64() |
| 70 |
h.Write([]byte(s)) |
| 71 |
return h.Sum64() |
| 72 |
}, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "CRC-32", |
| 76 |
directBytes: func(b []byte) uint64 { |
| 77 |
return uint64(crc32.ChecksumIEEE(b)) |
| 78 |
}, |
| 79 |
directString: func(s string) uint64 { |
| 80 |
return uint64(crc32.ChecksumIEEE([]byte(s))) |
| 81 |
}, |
| 82 |
digestBytes: func(b []byte) uint64 { |
| 83 |
h := crc32.NewIEEE() |
| 84 |
h.Write(b) |
| 85 |
return uint64(h.Sum32()) |
| 86 |
}, |
| 87 |
digestString: func(s string) uint64 { |
| 88 |
h := crc32.NewIEEE() |
| 89 |
h.Write([]byte(s)) |
| 90 |
return uint64(h.Sum32()) |
| 91 |
}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "FNV-1a", |
| 95 |
digestBytes: func(b []byte) uint64 { |
| 96 |
h := fnv.New64() |
| 97 |
h.Write(b) |
| 98 |
return h.Sum64() |
| 99 |
}, |
| 100 |
digestString: func(s string) uint64 { |
| 101 |
h := fnv.New64a() |
| 102 |
h.Write([]byte(s)) |
| 103 |
return h.Sum64() |
| 104 |
}, |
| 105 |
}, |
| 106 |
} |
| 107 |
|
| 108 |
func BenchmarkHashes(b *testing.B) { |
| 109 |
for _, bb := range benchmarks { |
| 110 |
for _, benchSize := range []struct { |
| 111 |
name string |
| 112 |
n int |
| 113 |
}{ |
| 114 |
{"5B", 5}, |
| 115 |
{"100B", 100}, |
| 116 |
{"4KB", 4e3}, |
| 117 |
{"10MB", 10e6}, |
| 118 |
} { |
| 119 |
input := make([]byte, benchSize.n) |
| 120 |
for i := range input { |
| 121 |
input[i] = byte(i) |
| 122 |
} |
| 123 |
inputString := string(input) |
| 124 |
if bb.directBytes != nil { |
| 125 |
name := fmt.Sprintf("%s,direct,bytes,n=%s", bb.name, benchSize.name) |
| 126 |
b.Run(name, func(b *testing.B) { |
| 127 |
benchmarkHashBytes(b, input, bb.directBytes) |
| 128 |
}) |
| 129 |
} |
| 130 |
if bb.directString != nil { |
| 131 |
name := fmt.Sprintf("%s,direct,string,n=%s", bb.name, benchSize.name) |
| 132 |
b.Run(name, func(b *testing.B) { |
| 133 |
benchmarkHashString(b, inputString, bb.directString) |
| 134 |
}) |
| 135 |
} |
| 136 |
if bb.digestBytes != nil { |
| 137 |
name := fmt.Sprintf("%s,digest,bytes,n=%s", bb.name, benchSize.name) |
| 138 |
b.Run(name, func(b *testing.B) { |
| 139 |
benchmarkHashBytes(b, input, bb.digestBytes) |
| 140 |
}) |
| 141 |
} |
| 142 |
if bb.digestString != nil { |
| 143 |
name := fmt.Sprintf("%s,digest,string,n=%s", bb.name, benchSize.name) |
| 144 |
b.Run(name, func(b *testing.B) { |
| 145 |
benchmarkHashString(b, inputString, bb.digestString) |
| 146 |
}) |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func benchmarkHashBytes(b *testing.B, input []byte, fn func([]byte) uint64) { |
| 153 |
b.SetBytes(int64(len(input))) |
| 154 |
for i := 0; i < b.N; i++ { |
| 155 |
sink = fn(input) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func benchmarkHashString(b *testing.B, input string, fn func(string) uint64) { |
| 160 |
b.SetBytes(int64(len(input))) |
| 161 |
for i := 0; i < b.N; i++ { |
| 162 |
sink = fn(input) |
| 163 |
} |
| 164 |
} |