237
bench/hash/bench_test.go
Normal file
237
bench/hash/bench_test.go
Normal file
@@ -0,0 +1,237 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"hash/crc32"
|
||||
"testing"
|
||||
|
||||
creachadairCity "bitbucket.org/creachadair/cityhash"
|
||||
jpathyCity "bitbucket.org/jpathy/dmc/cityhash"
|
||||
"github.com/OneOfOne/xxhash"
|
||||
dgryskiSpooky "github.com/dgryski/go-spooky"
|
||||
huichenMurmur "github.com/huichen/murmur"
|
||||
farmhash "github.com/leemcloughlin/gofarmhash"
|
||||
"github.com/minio/blake2b-simd"
|
||||
"github.com/minio/highwayhash"
|
||||
sha256simd "github.com/minio/sha256-simd"
|
||||
reuseeMurmur "github.com/reusee/mmh3"
|
||||
hashlandSpooky "github.com/tildeleb/hashland/spooky"
|
||||
zhangMurmur "github.com/zhangxinngang/murmur"
|
||||
)
|
||||
|
||||
var result interface{}
|
||||
|
||||
func mkinput(n int) []byte {
|
||||
rv := make([]byte, n)
|
||||
rand.Read(rv)
|
||||
return rv
|
||||
}
|
||||
|
||||
func benchmarkHash(num int, fn func(i int)) {
|
||||
for n := 0; n < num; n++ {
|
||||
fn(n)
|
||||
}
|
||||
}
|
||||
|
||||
const benchSize = 32 << 20
|
||||
|
||||
func BenchmarkHighwayhash64(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
key := make([]byte, 32)
|
||||
_, err := rand.Read(key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
h, err := highwayhash.New64(key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = h.Sum(input)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkFarmHashHash32(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = farmhash.Hash32(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkFarmHashHash64(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = farmhash.Hash64(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkHuichenMurmur(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = huichenMurmur.Murmur3(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkReuseeMurmur(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = reuseeMurmur.Sum32(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkZhangMurmur(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = zhangMurmur.Murmur3(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkDgryskiSpooky32(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = dgryskiSpooky.Hash32(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkDgryskiSpooky64(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = dgryskiSpooky.Hash64(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkHashlandSpooky32(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = hashlandSpooky.Hash32(input, 0)
|
||||
})
|
||||
}
|
||||
func BenchmarkHashlandSpooky64(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = hashlandSpooky.Hash64(input, 0)
|
||||
})
|
||||
}
|
||||
func BenchmarkHashlandSpooky128(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_, _ = hashlandSpooky.Hash128(input, 0)
|
||||
})
|
||||
}
|
||||
func BenchmarkJPathyCity32(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = jpathyCity.Hash32(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkCreachadairCity32(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = creachadairCity.Hash32(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkCreachadairCity64(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = creachadairCity.Hash64(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkCreachadairCity128(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_, _ = creachadairCity.Hash128(input)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkXxHash32(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = xxhash.Checksum32(input)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkXxHash64(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = xxhash.Checksum64(input)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkBlake2bSIMD256(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
h := blake2b.New256()
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
h.Reset()
|
||||
_, _ = h.Write(input)
|
||||
h.Sum(nil)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkSHA256(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = sha256.Sum256(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkSHA1(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = sha1.Sum(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkCRC32(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
h := crc32.NewIEEE()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = h.Sum(input)
|
||||
})
|
||||
}
|
||||
func BenchmarkSHA256SIMBD(b *testing.B) {
|
||||
input := mkinput(benchSize)
|
||||
b.SetBytes(int64(len(input)))
|
||||
b.ResetTimer()
|
||||
benchmarkHash(b.N, func(n int) {
|
||||
_ = sha256simd.Sum256(input)
|
||||
})
|
||||
}
|
||||
func main() {
|
||||
|
||||
}
|
42
bench/rand/bench_test.go
Normal file
42
bench/rand/bench_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fastrand"
|
||||
)
|
||||
|
||||
var BenchSink uint32
|
||||
|
||||
func BenchmarkMathRandIntn(b *testing.B) {
|
||||
rand.Seed(time.Now().Unix())
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
s := uint32(0)
|
||||
for pb.Next() {
|
||||
s += uint32(rand.Intn(1e6))
|
||||
}
|
||||
atomic.AddUint32(&BenchSink, s)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkRNGUint32nWithLock(b *testing.B) {
|
||||
var r fastrand.RNG
|
||||
var rMu sync.Mutex
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
s := uint32(0)
|
||||
for pb.Next() {
|
||||
rMu.Lock()
|
||||
s += r.Uint32n(1e6)
|
||||
rMu.Unlock()
|
||||
}
|
||||
atomic.AddUint32(&BenchSink, s)
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
Reference in New Issue
Block a user