Skip to content

Commit e6a71ce

Browse files
committed
Initial release
1 parent a666426 commit e6a71ce

File tree

13 files changed

+452
-0
lines changed

13 files changed

+452
-0
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: go
2+
go:
3+
- 1.15.x
4+
- 1.14.x
5+
cache:
6+
directories:
7+
- $GOPATH/pkg/mod

CONTRIBUTING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# How to contribute?
2+
3+
## Do you want to write a benchmark?
4+
5+
* Place your code in its own `./{category}/{name}` sub-directory.
6+
* Make sure it has its own `go.mod` - run `go mod init github.com/bsm/go-benchmark/{category}/{name}`.
7+
8+
## Did you write a benchmark?
9+
10+
* Open a new GitHub pull request with the patch.
11+
* Ensure the PR description clearly describes the intention of the effort.
12+
* Update the README.md, make sure to include your benchmark in the list.
13+
14+
Thanks!

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2020 Black Square Media Ltd
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
GO_MOD_FILES=$(shell find . -name 'go.mod')
2+
3+
default: bench
4+
5+
bench: $(patsubst %/go.mod,bench/%,$(GO_MOD_FILES))
6+
7+
bench/%: %
8+
cd $< && go test ./... -run=NONE -bench=. -benchmem

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Go (Awesome) Benchmarks
2+
3+
[![Build Status](https://travis-ci.org/bsm/go-benchmark.svg?branch=master)](https://travis-ci.org/bsm/go-benchmark)
4+
5+
This is a collection of various benchmarks for [Go](https://golang.org).
6+
7+
## Contributing
8+
9+
Contibutions are very welcome, but please take a quick look at our very short [contribution guidelines](./CONTRIBUTING.md).
10+
11+
## Contents
12+
13+
- http/
14+
- [json](./http/json/) - various methods of generating a JSON HTTP response.
15+
- hash/
16+
- [noncrypto](./hash/noncrypto/) - comparison of non-cryptographic hash methods.
17+
18+
## Results
19+
20+
Latest results can be found on [Travis-CI](https://travis-ci.org/github/bsm/go-benchmark).

hash/noncrypto/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# hash/noncrypto
2+
3+
The benchmark compares various non-cryptographic hash methods.
4+
5+
* [crc64](https://golang.org/pkg/hash/crc64)
6+
* [fnv](https://golang.org/pkg/hash/fnv)
7+
* [cespare/xxhash](https://github.com/cespare/xxhash)
8+
* [creachadair/cityhash](https://github.com/creachadair/cityhash)
9+
* [dgryski/go-farm](https://github.com/dgryski/go-farm)
10+
* [dgryski/go-metro](https://github.com/dgryski/go-metro)
11+
* [dgryski/go-spooky](https://github.com/dgryski/go-spooky)
12+
* [dgryski/go-stadtx](https://github.com/dgryski/go-stadtx)
13+
* [dgryski/go-wyhash](https://github.com/dgryski/go-wyhash)
14+
* [mmcloughlin/meow](https://github.com/mmcloughlin/meow)
15+
* [shivakar/metrohash](https://github.com/shivakar/metrohash)
16+
* [spaolacci/murmur3](https://github.com/spaolacci/murmur3)
17+
18+
## Results
19+
20+
```
21+
Benchmark_64byte/crc64-4 18614912 59.7 ns/op 0 B/op 0 allocs/op
22+
Benchmark_64byte/fnv1-4 17491640 66.2 ns/op 0 B/op 0 allocs/op
23+
Benchmark_64byte/cespare/xxhash-4 44026682 25.6 ns/op 0 B/op 0 allocs/op
24+
Benchmark_64byte/creachadair/cityhash-4 24943398 45.5 ns/op 0 B/op 0 allocs/op
25+
Benchmark_64byte/dgryski/go-farm-4 82406674 14.0 ns/op 0 B/op 0 allocs/op
26+
Benchmark_64byte/dgryski/go-metro-4 78999768 13.7 ns/op 0 B/op 0 allocs/op
27+
Benchmark_64byte/dgryski/go-spooky-4 21959304 52.9 ns/op 0 B/op 0 allocs/op
28+
Benchmark_64byte/dgryski/go-stadtx-4 76584982 15.0 ns/op 0 B/op 0 allocs/op
29+
Benchmark_64byte/dgryski/go-wyhash-4 51034880 21.8 ns/op 0 B/op 0 allocs/op
30+
Benchmark_64byte/mmcloughlin/meow-4 3405470 350 ns/op 600 B/op 3 allocs/op
31+
Benchmark_64byte/shivakar/metrohash-4 49362100 21.9 ns/op 0 B/op 0 allocs/op
32+
Benchmark_64byte/spaolacci/murmur3-4 32521585 35.4 ns/op 0 B/op 0 allocs/op
33+
34+
Benchmark_256byte/crc64-4 6708360 184 ns/op 0 B/op 0 allocs/op
35+
Benchmark_256byte/fnv1-4 3720697 327 ns/op 0 B/op 0 allocs/op
36+
Benchmark_256byte/cespare/xxhash-4 27032524 47.8 ns/op 0 B/op 0 allocs/op
37+
Benchmark_256byte/creachadair/cityhash-4 5348512 206 ns/op 0 B/op 0 allocs/op
38+
Benchmark_256byte/dgryski/go-farm-4 17112612 71.3 ns/op 0 B/op 0 allocs/op
39+
Benchmark_256byte/dgryski/go-metro-4 37797090 35.0 ns/op 0 B/op 0 allocs/op
40+
Benchmark_256byte/dgryski/go-spooky-4 7534816 157 ns/op 0 B/op 0 allocs/op
41+
Benchmark_256byte/dgryski/go-stadtx-4 36302551 33.3 ns/op 0 B/op 0 allocs/op
42+
Benchmark_256byte/dgryski/go-wyhash-4 24865210 48.5 ns/op 0 B/op 0 allocs/op
43+
Benchmark_256byte/mmcloughlin/meow-4 3088322 392 ns/op 600 B/op 3 allocs/op
44+
Benchmark_256byte/shivakar/metrohash-4 25815829 43.3 ns/op 0 B/op 0 allocs/op
45+
Benchmark_256byte/spaolacci/murmur3-4 14044209 80.5 ns/op 0 B/op 0 allocs/op
46+
47+
Benchmark_4096byte/crc64-4 442494 2537 ns/op 0 B/op 0 allocs/op
48+
Benchmark_4096byte/fnv1-4 222055 5329 ns/op 0 B/op 0 allocs/op
49+
Benchmark_4096byte/cespare/xxhash-4 3282175 380 ns/op 0 B/op 0 allocs/op
50+
Benchmark_4096byte/creachadair/cityhash-4 385884 3308 ns/op 0 B/op 0 allocs/op
51+
Benchmark_4096byte/dgryski/go-farm-4 2490466 491 ns/op 0 B/op 0 allocs/op
52+
Benchmark_4096byte/dgryski/go-metro-4 3716091 334 ns/op 0 B/op 0 allocs/op
53+
Benchmark_4096byte/dgryski/go-spooky-4 1000000 1150 ns/op 0 B/op 0 allocs/op
54+
Benchmark_4096byte/dgryski/go-stadtx-4 3300652 365 ns/op 0 B/op 0 allocs/op
55+
Benchmark_4096byte/dgryski/go-wyhash-4 3632041 338 ns/op 0 B/op 0 allocs/op
56+
Benchmark_4096byte/mmcloughlin/meow-4 2539258 506 ns/op 600 B/op 3 allocs/op
57+
Benchmark_4096byte/shivakar/metrohash-4 2337550 479 ns/op 0 B/op 0 allocs/op
58+
Benchmark_4096byte/spaolacci/murmur3-4 1550806 745 ns/op 0 B/op 0 allocs/op
59+
```

hash/noncrypto/bench_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package bench_test
2+
3+
import (
4+
"hash"
5+
"hash/crc64"
6+
"hash/fnv"
7+
"math/rand"
8+
"testing"
9+
10+
"github.com/cespare/xxhash"
11+
"github.com/creachadair/cityhash"
12+
"github.com/dgryski/go-farm"
13+
"github.com/dgryski/go-metro"
14+
"github.com/dgryski/go-spooky"
15+
"github.com/dgryski/go-stadtx"
16+
"github.com/dgryski/go-wyhash"
17+
"github.com/mmcloughlin/meow"
18+
"github.com/shivakar/metrohash"
19+
"github.com/spaolacci/murmur3"
20+
)
21+
22+
var (
23+
p64 = make([]byte, 64)
24+
p256 = make([]byte, 256)
25+
p4096 = make([]byte, 4096)
26+
)
27+
28+
// seed fixtures
29+
func init() {
30+
rnd := rand.New(rand.NewSource(33))
31+
if _, err := rnd.Read(p64); err != nil {
32+
panic(err)
33+
}
34+
if _, err := rnd.Read(p256); err != nil {
35+
panic(err)
36+
}
37+
if _, err := rnd.Read(p4096); err != nil {
38+
panic(err)
39+
}
40+
}
41+
42+
// --------------------------------------------------------------------
43+
44+
func Benchmark_64byte(b *testing.B) {
45+
benchmark(b, p64)
46+
}
47+
48+
func Benchmark_256byte(b *testing.B) {
49+
benchmark(b, p256)
50+
}
51+
52+
func Benchmark_4096byte(b *testing.B) {
53+
benchmark(b, p4096)
54+
}
55+
56+
func benchmark(b *testing.B, p []byte) {
57+
b.Run("crc64", func(b *testing.B) {
58+
h := crc64.New(crc64.MakeTable(crc64.ISO))
59+
benchHash(b, h, p)
60+
})
61+
b.Run("fnv1", func(b *testing.B) {
62+
h := fnv.New64()
63+
benchHash(b, h, p)
64+
})
65+
b.Run("cespare/xxhash", func(b *testing.B) {
66+
h := xxhash.New()
67+
benchHash(b, h, p)
68+
})
69+
b.Run("creachadair/cityhash", func(b *testing.B) {
70+
for i := 0; i < b.N; i++ {
71+
cityhash.Hash64(p)
72+
}
73+
})
74+
b.Run("dgryski/go-farm", func(b *testing.B) {
75+
for i := 0; i < b.N; i++ {
76+
farm.Hash64(p)
77+
}
78+
})
79+
b.Run("dgryski/go-metro", func(b *testing.B) {
80+
for i := 0; i < b.N; i++ {
81+
metro.Hash64(p, 0)
82+
}
83+
})
84+
b.Run("dgryski/go-spooky", func(b *testing.B) {
85+
h := spooky.New(1, 2)
86+
benchHash(b, h, p)
87+
})
88+
b.Run("dgryski/go-stadtx", func(b *testing.B) {
89+
state := stadtx.SeedState([]uint64{1, 2})
90+
for i := 0; i < b.N; i++ {
91+
stadtx.Hash(&state, p)
92+
}
93+
})
94+
b.Run("dgryski/go-wyhash", func(b *testing.B) {
95+
for i := 0; i < b.N; i++ {
96+
wyhash.Hash(p, 0)
97+
}
98+
})
99+
b.Run("mmcloughlin/meow", func(b *testing.B) {
100+
h := meow.New64(0)
101+
benchHash(b, h, p)
102+
})
103+
b.Run("shivakar/metrohash", func(b *testing.B) {
104+
h := metrohash.NewMetroHash64()
105+
benchHash(b, h, p)
106+
})
107+
b.Run("spaolacci/murmur3", func(b *testing.B) {
108+
h := murmur3.New64()
109+
benchHash(b, h, p)
110+
})
111+
}
112+
113+
func benchHash(b *testing.B, h hash.Hash64, p []byte) {
114+
b.ResetTimer()
115+
for i := 0; i < b.N; i++ {
116+
h.Reset()
117+
h.Write(p)
118+
h.Sum64()
119+
}
120+
}

hash/noncrypto/go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/bsm/go-benchmark/hash/noncrypto
2+
3+
go 1.15
4+
5+
require (
6+
github.com/cespare/xxhash v1.1.0
7+
github.com/creachadair/cityhash v0.1.0
8+
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13
9+
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165
10+
github.com/dgryski/go-spooky v0.0.0-20170606183049-ed3d087f40e2
11+
github.com/dgryski/go-stadtx v0.0.0-20181220175827-101ddca05f0b
12+
github.com/dgryski/go-wyhash v0.0.0-20191203203029-c4841ae36371
13+
github.com/mmcloughlin/meow v0.0.0-20200201185800-3501c7c05d21
14+
github.com/shivakar/metrohash v0.0.0-20160821164112-8d1c8b6bed28
15+
github.com/spaolacci/murmur3 v1.1.0
16+
)

hash/noncrypto/go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
2+
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
3+
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
4+
github.com/creachadair/cityhash v0.1.0 h1:rzSfISre5xT/EgbVvXBmsUJ58Hsrp8ccYMZVvc5uBmk=
5+
github.com/creachadair/cityhash v0.1.0/go.mod h1:bYNmpea/cMZgL9Rjt+ZfDfjaT0hScMO6SSGV1DN5lEM=
6+
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
7+
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
8+
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165 h1:BS21ZUJ/B5X2UVUbczfmdWH7GapPWAhxcMsDnjJTU1E=
9+
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165/go.mod h1:c9O8+fpSOX1DM8cPNSkX/qsBWdkD4yd2dpciOWQjpBw=
10+
github.com/dgryski/go-spooky v0.0.0-20170606183049-ed3d087f40e2 h1:lx1ZQgST/imDhmLpYDma1O3Cx9L+4Ie4E8S2RjFPQ30=
11+
github.com/dgryski/go-spooky v0.0.0-20170606183049-ed3d087f40e2/go.mod h1:hgHYKsoIw7S/hlWtP7wD1wZ7SX1jPTtKko5X9jrOgPQ=
12+
github.com/dgryski/go-stadtx v0.0.0-20181220175827-101ddca05f0b h1:YW8r3ofQ8j/EXptc9clMD3D4A/wm9ESkhI1gAcswos0=
13+
github.com/dgryski/go-stadtx v0.0.0-20181220175827-101ddca05f0b/go.mod h1:egA8qbzCRr4cUpP+B1T3lgRGRSfRkcDDc0RId+RMI9I=
14+
github.com/dgryski/go-wyhash v0.0.0-20191203203029-c4841ae36371 h1:bz5ApY1kzFBvw3yckuyRBCtqGvprWrKswYK468nm+Gs=
15+
github.com/dgryski/go-wyhash v0.0.0-20191203203029-c4841ae36371/go.mod h1:/ENMIO1SQeJ5YQeUWWpbX8f+bS8INHrrhFjXgEqi4LA=
16+
github.com/mmcloughlin/meow v0.0.0-20200201185800-3501c7c05d21 h1:2BIiU0QuELctVxpl6FKAsf68ZZvI89I9c8Kt8Guxba8=
17+
github.com/mmcloughlin/meow v0.0.0-20200201185800-3501c7c05d21/go.mod h1:uxCZJI8Z1PD2WRnSJtVJGHCyxC5qWhz5lOsx3Bx1NXo=
18+
github.com/shivakar/metrohash v0.0.0-20160821164112-8d1c8b6bed28 h1:T0pHmIIcYaJ1wFBkZtjKEMJ7mHb7iuFS9jH0hZOaXME=
19+
github.com/shivakar/metrohash v0.0.0-20160821164112-8d1c8b6bed28/go.mod h1:iPY+DBBhMwu94EsrqYzmQYJYZpMAHuBdToFD//nbb00=
20+
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
21+
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
22+
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=

http/json/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# http/json
2+
3+
The benchmark compares methods of generating a JSON response in Go with different payloads.
4+
5+
1. encode directly: `json.NewEncoder(w).Encode(payload)`
6+
2. use a `bufio.Writer` to buffer the output (with and without pooling)
7+
3. first encode to a `*bytes.Buffer` then use `buf.WriteTo(w)` (with and without pooling)
8+
9+
## Results
10+
11+
```
12+
Benchmark/small_payload/direct-4 7532 153040 ns/op 16311 B/op 112 allocs/op
13+
Benchmark/small_payload/bufio-4 19650 60103 ns/op 8229 B/op 51 allocs/op
14+
Benchmark/small_payload/bufio_with_pooling-4 20138 58468 ns/op 4067 B/op 49 allocs/op
15+
Benchmark/small_payload/buffer-4 7131 155010 ns/op 16414 B/op 114 allocs/op
16+
Benchmark/small_payload/buffer_with_pooling-4 7384 155175 ns/op 16307 B/op 112 allocs/op
17+
18+
Benchmark/large_payload/direct-4 4831 262378 ns/op 16658 B/op 116 allocs/op
19+
Benchmark/large_payload/bufio-4 4467 248376 ns/op 21201 B/op 118 allocs/op
20+
Benchmark/large_payload/bufio_with_pooling-4 4842 246278 ns/op 16729 B/op 116 allocs/op
21+
Benchmark/large_payload/buffer-4 4442 265030 ns/op 57771 B/op 119 allocs/op
22+
Benchmark/large_payload/buffer_with_pooling-4 4538 256824 ns/op 16717 B/op 116 allocs/op
23+
```

0 commit comments

Comments
 (0)