Skip to content

Commit

Permalink
test: add benchmarks to share encoding and decoding (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters authored Jun 24, 2024
1 parent 6576955 commit c078e37
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/test/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ func GenerateBlobTxs(numTxs, blobsPerPfb, blobSize int) [][]byte {
return txs
}

func GenerateBlobs(blobSizes ...int) []*shares.Blob {
blobs := make([]*shares.Blob, len(blobSizes))
var err error
for i, size := range blobSizes {
blobs[i], err = shares.NewBlob(namespace.RandomBlobNamespace(), RandomBytes(size), shares.ShareVersionZero, nil)
if err != nil {
panic(err)
}
}
return blobs
}

const mockPFBExtraBytes = 329

func MockPFB(blobSizes []uint32) []byte {
Expand Down
57 changes: 57 additions & 0 deletions shares/share_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package shares_test

import (
"fmt"
"testing"

"github.com/celestiaorg/go-square/internal/test"
"github.com/celestiaorg/go-square/shares"
)

func BenchmarkBlobsToShares(b *testing.B) {
sizes := []int{256, 256 * 8, 256 * 64}
numBlobs := []int{1, 8, 64}
for _, size := range sizes {
for _, numBlobs := range numBlobs {
b.Run(fmt.Sprintf("ShareEncoding%dBlobs%dBytes", numBlobs, size), func(b *testing.B) {
b.ReportAllocs()
blobs := test.GenerateBlobs(test.Repeat(size, numBlobs)...)

b.ResetTimer()
for i := 0; i < b.N; i++ {
// Convert blob to shares
_, err := shares.SplitBlobs(blobs...)
if err != nil {
b.Fatal("Failed to split blob into shares:", err)
}
}
})
}
}
}

func BenchmarkSharesToBlobs(b *testing.B) {
sizes := []int{256, 256 * 8, 256 * 64}
numBlobs := []int{1, 8, 64}
for _, size := range sizes {
for _, numBlobs := range numBlobs {
b.Run(fmt.Sprintf("ShareDecoding%dBlobs%dBytes", numBlobs, size), func(b *testing.B) {
b.ReportAllocs()
blobs := test.GenerateBlobs(test.Repeat(size, numBlobs)...)
s, err := shares.SplitBlobs(blobs...)
if err != nil {
b.Fatal("Failed to split blob into shares:", err)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
// Convert shares back to blob
_, err := shares.ParseBlobs(s)
if err != nil {
b.Fatal("Failed to reconstruct blob from shares:", err)
}
}
})
}
}
}

0 comments on commit c078e37

Please sign in to comment.