@@ -6,6 +6,7 @@ package storer_test
66
77import (
88 "context"
9+ "fmt"
910 "math/rand"
1011 "testing"
1112 "time"
@@ -383,3 +384,44 @@ func assertSampleNoErrors(t *testing.T, sample storer.Sample) {
383384 t .Fatalf ("got unexpected invalid stamps" )
384385 }
385386}
387+
388+ // BenchmarkSampleHashing measures the time taken by MakeSampleUsingChunks to
389+ // hash a fixed set of CAC chunks. Sub-benchmarks cover the two sizes of
390+ // practical interest: 1 000 and 10 000 chunks.
391+ func BenchmarkSampleHashing (b * testing.B ) {
392+ anchor := []byte ("swarm-test-anchor-deterministic!" )
393+
394+ // Shared zero-value stamp: its contents don't affect hash computation.
395+ stamp := postage .NewStamp (make ([]byte , 32 ), make ([]byte , 8 ), make ([]byte , 8 ), make ([]byte , 65 ))
396+
397+ for _ , count := range []int {1_000 , 10_000 } {
398+ count := count
399+ b .Run (fmt .Sprintf ("chunks=%d" , count ), func (b * testing.B ) {
400+ // Build chunks once outside the measured loop.
401+ // Content is derived deterministically from the chunk index so
402+ // that every run produces the same set of chunk addresses.
403+ chunks := make ([]swarm.Chunk , count )
404+ content := make ([]byte , swarm .ChunkSize )
405+ for i := range chunks {
406+ for j := range content {
407+ content [j ] = byte (i + j )
408+ }
409+ ch , err := cac .New (content )
410+ if err != nil {
411+ b .Fatal (err )
412+ }
413+ chunks [i ] = ch .WithStamp (stamp )
414+ }
415+
416+ // Report throughput so the output shows MB/s as well as ns/op.
417+ b .SetBytes (int64 (count ) * swarm .ChunkSize )
418+ b .ResetTimer ()
419+
420+ for range b .N {
421+ if _ , err := storer .MakeSampleUsingChunks (chunks , anchor ); err != nil {
422+ b .Fatal (err )
423+ }
424+ }
425+ })
426+ }
427+ }
0 commit comments