Skip to content

Commit

Permalink
test: add benchmark for all hash functions Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Aug 17, 2022
1 parent 16974ad commit b4fb2ca
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"runtime"
"sort"
"testing"

"github.com/multiformats/go-multihash"
Expand Down Expand Up @@ -177,3 +180,47 @@ func TestBasicSum(t *testing.T) {
}
}
}

var Sink []byte

type codeNamePair struct {
id uint64
name string
}

func BenchmarkSumAllLarge(b *testing.B) {
var data [1024 * 1024 * 16]byte
src := rand.New(rand.NewSource(0x4242424242424242))
_, err := io.ReadFull(src, data[:])
if err != nil {
b.Fatal(err)
}

// Write to a slice to sort elements
s := make([]codeNamePair, 0, len(multihash.Codes))
for id, name := range multihash.Codes {
s = append(s, codeNamePair{id, name})
}

sort.Slice(s, func(x, y int) bool {
return s[x].id < s[y].id
})

for _, v := range s {
h, e := multihash.GetHasher(v.id)
if h == nil || e != nil {
continue // Don't benchmark unsupported hashing functions
}

b.Run(v.name, func(b *testing.B) {
b.SetBytes(int64(len(data)))
for i := b.N; i > 0; i-- {
var err error
Sink, err = multihash.Sum(data[:], v.id, -1)
if err != nil {
b.Fatal(err)
}
}
})
}
}

0 comments on commit b4fb2ca

Please sign in to comment.