From 8a7240b2cb35b9a8a86cdb819f5fea4d0127d42e Mon Sep 17 00:00:00 2001 From: "fengyun.rui" Date: Sun, 2 Jul 2023 23:49:35 +0800 Subject: [PATCH] fix: clear db after benchmark (#224) --- benchmark/bench_test.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/benchmark/bench_test.go b/benchmark/bench_test.go index 1b9bdb29..2e55a692 100644 --- a/benchmark/bench_test.go +++ b/benchmark/bench_test.go @@ -2,6 +2,7 @@ package benchmark import ( "math/rand" + "os" "testing" "github.com/rosedblabs/rosedb/v2" @@ -11,7 +12,7 @@ import ( var db *rosedb.DB -func init() { +func openDB() func() { options := rosedb.DefaultOptions options.DirPath = "/tmp/rosedbv2" @@ -20,9 +21,22 @@ func init() { if err != nil { panic(err) } + + return func() { + _ = db.Close() + _ = os.RemoveAll(options.DirPath) + } +} + +func BenchmarkPutGet(b *testing.B) { + closer := openDB() + defer closer() + + b.Run("put", benchmarkPut) + b.Run("get", bencharkGet) } -func Benchmark_Put(b *testing.B) { +func benchmarkPut(b *testing.B) { b.ResetTimer() b.ReportAllocs() @@ -32,7 +46,7 @@ func Benchmark_Put(b *testing.B) { } } -func Benchmark_Get(b *testing.B) { +func bencharkGet(b *testing.B) { for i := 0; i < 10000; i++ { err := db.Put(utils.GetTestKey(i), utils.RandomValue(1024)) assert.Nil(b, err)