-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Benchmark existing ways to check for
IDENTITY
CIDs
Add benchmarks that compare two ways of checking for `multihash.IDENTITY` code: 1. `Cid.Prefix().MhType` 2. Decode of `Cid.Hash()` Relates to #133
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cid_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/multiformats/go-multihash" | ||
) | ||
|
||
// BenchmarkIdentityCheck benchmarks checking whether a CIDv1 has multihash.IDENTITY | ||
// code via two methods: 1) Cid.Prefix() and 2) decoding the Cid.Hash(). | ||
func BenchmarkIdentityCheck(b *testing.B) { | ||
rng := rand.New(rand.NewSource(1413)) | ||
|
||
data := make([]byte, rng.Intn(100)+1024) | ||
if _, err := rng.Read(data); err != nil { | ||
b.Fatal(err) | ||
} | ||
mh, err := multihash.Sum(data, multihash.IDENTITY, -1) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
cv1 := cid.NewCidV1(cid.Raw, mh) | ||
|
||
b.SetBytes(int64(cv1.ByteLen())) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
b.Run("Prefix", func(b *testing.B) { | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
if cv1.Prefix().MhType != multihash.IDENTITY { | ||
b.Fatal("expected IDENTITY CID") | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
b.Run("MultihashDecode", func(b *testing.B) { | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
dmh, err := multihash.Decode(cv1.Hash()) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
if dmh.Code != multihash.IDENTITY { | ||
b.Fatal("expected IDENTITY CID") | ||
} | ||
} | ||
}) | ||
}) | ||
} |