Skip to content

Commit 41baf34

Browse files
committed
Making benchmarks and tests deterministic.
By using a fixed seed for the data generated.
1 parent 0e9e5de commit 41baf34

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

cuckoofilter_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package cuckoo
22

33
import (
44
"bufio"
5-
"crypto/rand"
65
"fmt"
7-
"io"
86
"math"
7+
"math/rand"
98
"os"
109
"testing"
1110

@@ -104,14 +103,13 @@ func TestFilter_LookupLarge(t *testing.T) {
104103
}
105104

106105
func TestFilter_Insert(t *testing.T) {
107-
const cap = 10000
108-
filter := NewFilter(cap)
109-
110-
var hash [32]byte
106+
filter := NewFilter(10000)
107+
rng := rand.New(rand.NewSource(int64(42)))
111108

109+
hash := make([]byte, 32)
112110
for i := 0; i < 100; i++ {
113-
io.ReadFull(rand.Reader, hash[:])
114-
filter.Insert(hash[:])
111+
rng.Read(hash)
112+
filter.Insert(hash)
115113
}
116114

117115
if got, want := filter.Count(), uint(100); got != want {
@@ -134,9 +132,10 @@ func BenchmarkFilter_Reset(b *testing.B) {
134132
func benchmarkKeys(b *testing.B, size int) [][]byte {
135133
b.Helper()
136134
keys := make([][]byte, size)
135+
rng := rand.New(rand.NewSource(int64(size)))
137136
for i := range keys {
138137
keys[i] = make([]byte, 32)
139-
if _, err := io.ReadFull(rand.Reader, keys[i]); err != nil {
138+
if _, err := rng.Read(keys[i]); err != nil {
140139
b.Error(err)
141140
}
142141
}

0 commit comments

Comments
 (0)