Skip to content

Commit

Permalink
Merge pull request #7 from zarttic/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
zarttic authored Feb 23, 2024
2 parents f6a5edf + ab057b4 commit e3bd868
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:

# - name: Build
# run: go build -v ./...

- name: Test
run: go test -v ./...
- name: Benchmark
run: cd benchmark && go test -v -bench=.
62 changes: 62 additions & 0 deletions benchmark/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package benchmark

import (
"bitcask"
"bitcask/utils"
"errors"
"github.com/stretchr/testify/assert"
"math/rand"
"os"
"testing"
"time"
)

var db *bitcask.DB

func init() {
cfg := bitcask.DefaultConfig
dir, err := os.MkdirTemp("", "bitcask-bench-test")
cfg.DirPath = dir
if err != nil {
panic(err)
}
db, err = bitcask.Open(cfg)
if err != nil {
panic(err)
}
}
func Benchmark_Put(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := db.Put(utils.GetTestKey(i), utils.GetTestValue(1024))
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 e3bd868

Please sign in to comment.