Created
February 2, 2020 00:12
-
-
Save devplayg/6fcf1f7dc69befe33bde5519f38dd3c6 to your computer and use it in GitHub Desktop.
Benchmark test in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test | |
import ( | |
"crypto/md5" | |
"crypto/sha1" | |
"crypto/sha256" | |
"crypto/sha512" | |
"hash" | |
"hash/fnv" | |
"log" | |
"math/rand" | |
"testing" | |
) | |
const ( | |
K = 1024 | |
DataSize = 1 * K | |
) | |
var data = make([]byte, DataSize) | |
func init() { | |
_, err := rand.Read(data) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func cal(t *testing.B, h hash.Hash, n int) { | |
t.ResetTimer() | |
for i := 0; i < t.N; i++ { | |
h.Sum(data) | |
} | |
} | |
func BenchmarkFNV32(t *testing.B) { | |
cal(t, fnv.New32(), DataSize) | |
} | |
func BenchmarkFNV64(t *testing.B) { | |
cal(t, fnv.New64(), DataSize) | |
} | |
func BenchmarkFNV128(t *testing.B) { | |
cal(t, fnv.New128(), DataSize) | |
} | |
func BenchmarkMD5(t *testing.B) { | |
cal(t, md5.New(), DataSize) | |
} | |
func BenchmarkSHA1(t *testing.B) { | |
cal(t, sha1.New(), DataSize) | |
} | |
func BenchmarkSHA224(t *testing.B) { | |
cal(t, sha256.New224(), DataSize) | |
} | |
func BenchmarkSHA256(t *testing.B) { | |
cal(t, sha256.New(), DataSize) | |
} | |
func BenchmarkSHA512(t *testing.B) { | |
cal(t, sha512.New(), DataSize) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment