Skip to content

Commit

Permalink
fix benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
roseduan committed Sep 10, 2023
1 parent fb42623 commit f8f60f4
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions benchmark/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package benchmark

import (
"fmt"
"github.com/spaolacci/murmur3"
"math/rand"
"os"
"strings"
"testing"
Expand All @@ -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)
Expand Down

0 comments on commit f8f60f4

Please sign in to comment.