Skip to content

Commit 058395c

Browse files
committed
chore: add benchmarks
1 parent 6f325bf commit 058395c

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

pkg/storer/sample_test.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package storer_test
66

77
import (
88
"context"
9+
"fmt"
910
"math/rand"
1011
"testing"
1112
"time"
@@ -113,7 +114,6 @@ func TestReserveSampler(t *testing.T) {
113114

114115
assertSampleNoErrors(t, sample)
115116
})
116-
117117
}
118118

119119
t.Run("disk", func(t *testing.T) {
@@ -234,7 +234,6 @@ func TestReserveSamplerSisterNeighborhood(t *testing.T) {
234234
t.Fatalf("sample should not have ignored chunks")
235235
}
236236
})
237-
238237
}
239238

240239
t.Run("disk", func(t *testing.T) {
@@ -383,3 +382,49 @@ func assertSampleNoErrors(t *testing.T, sample storer.Sample) {
383382
t.Fatalf("got unexpected invalid stamps")
384383
}
385384
}
385+
386+
// BenchmarkSampleHashing measures the time taken by MakeSampleUsingChunks to
387+
// hash a fixed set of CAC chunks. Results:
388+
// goos: linux
389+
// goarch: amd64
390+
// pkg: github.com/ethersphere/bee/v2/pkg/storer
391+
// cpu: Intel(R) Core(TM) Ultra 7 165U
392+
// BenchmarkSampleHashing/chunks=1000-14 10 129751940 ns/op 31.57 MB/s 69386112 B/op 814005 allocs/op
393+
// BenchmarkSampleHashing/chunks=10000-14 10 1396326665 ns/op 29.33 MB/s 693843300 B/op 8140007 allocs/op
394+
func BenchmarkSampleHashing(b *testing.B) {
395+
anchor := []byte("swarm-test-anchor-deterministic!")
396+
397+
// Shared zero-value stamp: its contents don't affect hash computation.
398+
stamp := postage.NewStamp(make([]byte, 32), make([]byte, 8), make([]byte, 8), make([]byte, 65))
399+
400+
for _, count := range []int{1_000, 10_000} {
401+
count := count
402+
b.Run(fmt.Sprintf("chunks=%d", count), func(b *testing.B) {
403+
// Build chunks once outside the measured loop.
404+
// Content is derived deterministically from the chunk index so
405+
// that every run produces the same set of chunk addresses.
406+
chunks := make([]swarm.Chunk, count)
407+
content := make([]byte, swarm.ChunkSize)
408+
for i := range chunks {
409+
for j := range content {
410+
content[j] = byte(i + j)
411+
}
412+
ch, err := cac.New(content)
413+
if err != nil {
414+
b.Fatal(err)
415+
}
416+
chunks[i] = ch.WithStamp(stamp)
417+
}
418+
419+
// Report throughput so the output shows MB/s as well as ns/op.
420+
b.SetBytes(int64(count) * swarm.ChunkSize)
421+
b.ResetTimer()
422+
423+
for range b.N {
424+
if _, err := storer.MakeSampleUsingChunks(chunks, anchor); err != nil {
425+
b.Fatal(err)
426+
}
427+
}
428+
})
429+
}
430+
}

0 commit comments

Comments
 (0)