Skip to content

Commit

Permalink
Narrowing down benchmarks to relevant portion of code.
Browse files Browse the repository at this point in the history
Before, benchmarks were not very sensitive as lots of other code was
executed in the hot section.
  • Loading branch information
panmari committed Jan 23, 2022
1 parent b9d73bc commit 0e9e5de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
52 changes: 32 additions & 20 deletions cuckoofilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"math"
"os"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -131,33 +130,45 @@ func BenchmarkFilter_Reset(b *testing.B) {
}
}

func BenchmarkFilter_Insert(b *testing.B) {
const cap = 10000
filter := NewFilter(cap)
// benchmarkKeys returns a slice of keys for benchmarking with length `size`.
func benchmarkKeys(b *testing.B, size int) [][]byte {
b.Helper()
keys := make([][]byte, size)
for i := range keys {
keys[i] = make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, keys[i]); err != nil {
b.Error(err)
}
}
return keys
}

func BenchmarkFilter_Insert(b *testing.B) {
const size = 10000
keys := benchmarkKeys(b, int(float64(size)*0.8))
b.ResetTimer()

var hash [32]byte
for i := 0; i < b.N; i++ {
io.ReadFull(rand.Reader, hash[:])
filter.Insert(hash[:])
for i := 0; i < b.N; {
b.StopTimer()
filter := NewFilter(size)
b.StartTimer()
for _, k := range keys {
filter.Insert(k)
i++
}
}
}

func BenchmarkFilter_Lookup(b *testing.B) {
const cap = 10000
filter := NewFilter(cap)

var hash [32]byte
for i := 0; i < 10000; i++ {
io.ReadFull(rand.Reader, hash[:])
filter.Insert(hash[:])
}
filter := NewFilter(10000)
keys := benchmarkKeys(b, 10000)

b.ResetTimer()
for i := 0; i < b.N; i++ {
io.ReadFull(rand.Reader, hash[:])
filter.Lookup(hash[:])
for i := 0; i < b.N; {
for _, k := range keys {
filter.Lookup(k)
i++
}
}
}

Expand Down Expand Up @@ -206,6 +217,7 @@ func TestDeleteMultipleSame(t *testing.T) {
{"some_item", true, 0},
{"some_item", false, 0},
}
t.Logf("Filter state full: %v", cf)
for _, tc := range testCases {
t.Run(fmt.Sprintf("cf.Delete(%q)", tc.word), func(t *testing.T) {
if got, gotCount := cf.Delete([]byte(tc.word)), cf.Count(); got != tc.want || gotCount != tc.wantCount {
Expand All @@ -231,7 +243,7 @@ func TestEncodeDecode(t *testing.T) {
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if !reflect.DeepEqual(cf, got) {
if !cmp.Equal(cf, got, cmp.AllowUnexported(Filter{})) {
t.Errorf("Decode = %v, want %v, encoded = %v", got, cf, encoded)
}
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165 h1:BS21ZUJ/B5X2UV
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165/go.mod h1:c9O8+fpSOX1DM8cPNSkX/qsBWdkD4yd2dpciOWQjpBw=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit 0e9e5de

Please sign in to comment.