Skip to content

Commit e3bd868

Browse files
authored
Merge pull request #7 from zarttic/dev
Dev
2 parents f6a5edf + ab057b4 commit e3bd868

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

.github/workflows/go.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323

2424
# - name: Build
2525
# run: go build -v ./...
26-
2726
- name: Test
2827
run: go test -v ./...
28+
- name: Benchmark
29+
run: cd benchmark && go test -v -bench=.

benchmark/bench_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package benchmark
2+
3+
import (
4+
"bitcask"
5+
"bitcask/utils"
6+
"errors"
7+
"github.com/stretchr/testify/assert"
8+
"math/rand"
9+
"os"
10+
"testing"
11+
"time"
12+
)
13+
14+
var db *bitcask.DB
15+
16+
func init() {
17+
cfg := bitcask.DefaultConfig
18+
dir, err := os.MkdirTemp("", "bitcask-bench-test")
19+
cfg.DirPath = dir
20+
if err != nil {
21+
panic(err)
22+
}
23+
db, err = bitcask.Open(cfg)
24+
if err != nil {
25+
panic(err)
26+
}
27+
}
28+
func Benchmark_Put(b *testing.B) {
29+
b.ResetTimer()
30+
b.ReportAllocs()
31+
for i := 0; i < b.N; i++ {
32+
err := db.Put(utils.GetTestKey(i), utils.GetTestValue(1024))
33+
assert.Nil(b, err)
34+
}
35+
}
36+
func Benchmark_Get(b *testing.B) {
37+
for i := 0; i < 10000; i++ {
38+
err := db.Put(utils.GetTestKey(i), utils.GetTestValue(1024))
39+
assert.Nil(b, err)
40+
}
41+
42+
rand.Seed(time.Now().UnixNano())
43+
b.ResetTimer()
44+
b.ReportAllocs()
45+
for i := 0; i < b.N; i++ {
46+
_, err := db.Get(utils.GetTestKey(rand.Int()))
47+
if err != nil && !errors.Is(err, bitcask.ErrKeyNotFound) {
48+
b.Fatal(err)
49+
}
50+
}
51+
}
52+
53+
func Benchmark_Delete(b *testing.B) {
54+
b.ResetTimer()
55+
b.ReportAllocs()
56+
57+
rand.Seed(time.Now().UnixNano())
58+
for i := 0; i < b.N; i++ {
59+
err := db.Delete(utils.GetTestKey(rand.Int()))
60+
assert.Nil(b, err)
61+
}
62+
}

0 commit comments

Comments
 (0)