From f8f60f4e00fbc443beb5e5ed90025b5ea487841f Mon Sep 17 00:00:00 2001 From: roseduan Date: Sun, 10 Sep 2023 15:20:28 +0800 Subject: [PATCH] fix benchmark --- benchmark/bench_test.go | 46 ++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/benchmark/bench_test.go b/benchmark/bench_test.go index 749a265..2a48871 100644 --- a/benchmark/bench_test.go +++ b/benchmark/bench_test.go @@ -2,6 +2,8 @@ package benchmark import ( "fmt" + "github.com/spaolacci/murmur3" + "math/rand" "os" "strings" "testing" @@ -10,31 +12,59 @@ import ( "github.com/stretchr/testify/assert" ) -func newDB() (*diskhash.Table, func()) { +func newTable() (*diskhash.Table, func()) { options := diskhash.DefaultOptions - options.SlotValueLength = 16 + options.SlotValueLength = 20 options.DirPath = "/tmp/diskhash-bench" - db, err := diskhash.Open(options) + table, err := diskhash.Open(options) if err != nil { panic(err) } - return db, func() { - _ = db.Close() + return table, func() { + _ = table.Close() _ = os.RemoveAll(options.DirPath) } } func BenchmarkPut(b *testing.B) { - db, destroy := newDB() + table, destroy := newTable() defer destroy() b.ResetTimer() b.ReportAllocs() - value := []byte(strings.Repeat("d", 16)) + value := []byte(strings.Repeat("d", 20)) for i := 0; i < b.N; i++ { - err := db.Put(GetTestKey(i), value, func(slot diskhash.Slot) (bool, error) { + err := table.Put(GetTestKey(i), value, func(slot diskhash.Slot) (bool, error) { + return false, nil + }) + assert.Nil(b, err) + } +} + +func BenchmarkGet(b *testing.B) { + table, destroy := newTable() + defer destroy() + + value := []byte(strings.Repeat("d", 20)) + for i := 0; i < 100000; i++ { + err := table.Put(GetTestKey(i), value, func(slot diskhash.Slot) (bool, error) { + return false, nil + }) + assert.Nil(b, err) + } + + b.ResetTimer() + b.ReportAllocs() + + for i := 0; i < b.N; i++ { + key := GetTestKey(rand.Intn(100000)) + err := table.Get(key, func(slot diskhash.Slot) (bool, error) { + hash := murmur3.Sum32(key) + if hash == slot.Hash { + return true, nil + } return false, nil }) assert.Nil(b, err)