diff --git a/v2/bench_test.go b/v2/bench_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c1fe5b624270a171cc08df375e70c8b629453fc9 --- /dev/null +++ b/v2/bench_test.go @@ -0,0 +1,48 @@ +package car_test + +import ( + "io" + "os" + "testing" + + carv2 "github.com/ipld/go-car/v2" +) + +// Open a reader, get the roots, and iterate over all blocks. +// Essentially looking at the contents of any CARv1 or CARv2 file. +// Note that this also uses ReadVersion underneath. + +func BenchmarkReadBlocks(b *testing.B) { + path := "testdata/sample-wrapped-v2.car" + + info, err := os.Stat(path) + if err != nil { + b.Fatal(err) + } + b.SetBytes(info.Size()) + b.ReportAllocs() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + cr, err := carv2.OpenReader(path) + if err != nil { + b.Fatal(err) + } + _, err = cr.Roots() + if err != nil { + b.Fatal(err) + } + for { + _, err := cr.Next() + if err == io.EOF { + break + } + if err != nil { + b.Fatal(err) + } + } + + cr.Close() + } + }) +} diff --git a/v2/blockstore/bench_test.go b/v2/blockstore/bench_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c97dc3526767e59339bda26f306ce0bc4c29c897 --- /dev/null +++ b/v2/blockstore/bench_test.go @@ -0,0 +1,71 @@ +package blockstore_test + +import ( + "io" + mathrand "math/rand" + "os" + "testing" + + "github.com/ipfs/go-cid" + carv2 "github.com/ipld/go-car/v2" + "github.com/ipld/go-car/v2/blockstore" +) + +// Open a read-only blockstore, +// and retrieve all blocks in a shuffled order. +// Note that this benchmark includes generating an index, +// since the input file is a CARv1. + +func BenchmarkOpenReadOnlyV1(b *testing.B) { + path := "../testdata/sample-v1.car" + + info, err := os.Stat(path) + if err != nil { + b.Fatal(err) + } + b.SetBytes(info.Size()) + b.ReportAllocs() + + var shuffledCIDs []cid.Cid + cr, err := carv2.OpenReader(path) + if err != nil { + b.Fatal(err) + } + for { + block, err := cr.Next() + if err == io.EOF { + break + } + if err != nil { + b.Fatal(err) + } + shuffledCIDs = append(shuffledCIDs, block.Cid()) + } + cr.Close() + + // The shuffling needs to be deterministic, + // for the sake of stable benchmark results. + // Any source number works as long as it's fixed. + rnd := mathrand.New(mathrand.NewSource(123456)) + rnd.Shuffle(len(shuffledCIDs), func(i, j int) { + shuffledCIDs[i], shuffledCIDs[j] = shuffledCIDs[j], shuffledCIDs[i] + }) + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + bs, err := blockstore.OpenReadOnly(path) + if err != nil { + b.Fatal(err) + } + + for _, c := range shuffledCIDs { + _, err := bs.Get(c) + if err != nil { + b.Fatal(err) + } + } + + bs.Close() + } + }) +}