From 1403cdc8d86af066d1097c7489ea9f9acd77fe8f Mon Sep 17 00:00:00 2001 From: Frederic Langlet Date: Fri, 26 Jan 2024 11:46:17 -0800 Subject: [PATCH] Make benchmarks more consistent across invocations --- v2/benchmark/BWT_test.go | 6 +++--- v2/benchmark/Entropy_test.go | 4 +++- v2/benchmark/Transforms_test.go | 4 +++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/v2/benchmark/BWT_test.go b/v2/benchmark/BWT_test.go index bfa66eca..ce057343 100644 --- a/v2/benchmark/BWT_test.go +++ b/v2/benchmark/BWT_test.go @@ -20,7 +20,6 @@ import ( "fmt" "math/rand" "testing" - "time" kanzi "github.com/flanglet/kanzi-go/v2" "github.com/flanglet/kanzi-go/v2/transform" @@ -50,8 +49,9 @@ func testBWTSpeed(isBWT bool, iter, size int) error { buf3 := make([]byte, size) for jj := 0; jj < 3; jj++ { + // Initialize with a fixed seed to get consistent results + r := rand.New(rand.NewSource(int64(jj * 1234567))) var bwt kanzi.ByteTransform - rnd := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < iter; i++ { if isBWT { @@ -61,7 +61,7 @@ func testBWTSpeed(isBWT bool, iter, size int) error { } for i := range buf1 { - buf1[i] = byte(rnd.Intn(255) + 1) + buf1[i] = byte(r.Intn(255) + 1) } _, _, err1 := bwt.Forward(buf1, buf2) diff --git a/v2/benchmark/Entropy_test.go b/v2/benchmark/Entropy_test.go index f22d2b77..407bf30c 100644 --- a/v2/benchmark/Entropy_test.go +++ b/v2/benchmark/Entropy_test.go @@ -105,6 +105,8 @@ func getDecoder(name string, ibs kanzi.InputBitStream) kanzi.EntropyDecoder { } func testEntropySpeed(b *testing.B, name string) error { + // Initialize with a fixed seed to get consistent results + r := rand.New(rand.NewSource(1234567)) repeats := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3} for jj := 0; jj < 3; jj++ { @@ -122,7 +124,7 @@ func testEntropySpeed(b *testing.B, name string) error { length := repeats[idx] idx = (idx + 1) & 0x0F - b := byte(rand.Intn(256)) + b := byte(r.Intn(256)) if i0+length >= size { length = size - i0 - 1 diff --git a/v2/benchmark/Transforms_test.go b/v2/benchmark/Transforms_test.go index 5243c133..f83d803b 100644 --- a/v2/benchmark/Transforms_test.go +++ b/v2/benchmark/Transforms_test.go @@ -153,6 +153,8 @@ func BenchmarkMTFT(b *testing.B) { } func testTransformSpeed(name string, iter int) error { + // Initialize with a fixed seed to get consistent results + r := rand.New(rand.NewSource(1234567)) size := 50000 for jj := 0; jj < 3; jj++ { @@ -165,7 +167,7 @@ func testTransformSpeed(name string, iter int) error { n := iter / 20 for n < len(input) { - val := byte(rand.Intn(64)) + val := byte(r.Intn(64)) if val%7 == 0 { val = 0