Skip to content

Commit

Permalink
feat: benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
zarttic committed Feb 23, 2024
1 parent 49c28b9 commit ab057b4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions benchmark/bench_test.go
Original file line number Diff line number Diff line change
@@ -3,9 +3,12 @@ package benchmark
import (
"bitcask"
"bitcask/utils"
"errors"
"github.com/stretchr/testify/assert"
"math/rand"
"os"
"testing"
"time"
)

var db *bitcask.DB
@@ -30,3 +33,30 @@ func Benchmark_Put(b *testing.B) {
assert.Nil(b, err)
}
}
func Benchmark_Get(b *testing.B) {
for i := 0; i < 10000; i++ {
err := db.Put(utils.GetTestKey(i), utils.GetTestValue(1024))
assert.Nil(b, err)
}

rand.Seed(time.Now().UnixNano())
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := db.Get(utils.GetTestKey(rand.Int()))
if err != nil && !errors.Is(err, bitcask.ErrKeyNotFound) {
b.Fatal(err)
}
}
}

func Benchmark_Delete(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()

rand.Seed(time.Now().UnixNano())
for i := 0; i < b.N; i++ {
err := db.Delete(utils.GetTestKey(rand.Int()))
assert.Nil(b, err)
}
}

0 comments on commit ab057b4

Please sign in to comment.