Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdurham committed Oct 30, 2023
1 parent 47fef92 commit e04828b
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions service/kv/kv_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package kv

import (
"github.com/stretchr/testify/require"
"math/rand"
"path/filepath"
"strconv"
"testing"
)

func BenchmarkSequentialPut(b *testing.B) {
dir := b.TempDir()
db, err := NewKVDB(filepath.Join(dir, "bolt.db"))

require.NoError(b, err)
defer db.Close()
data := []byte("data")
for i := 0; i < b.N; i++ {
db.Put("test", strconv.Itoa(i), data)
}
}

func BenchmarkRandomPut(b *testing.B) {
dir := b.TempDir()
db, err := NewKVDB(filepath.Join(dir, "bolt.db"))

require.NoError(b, err)
defer db.Close()
data := []byte("data")
for i := 0; i < b.N; i++ {
val := rand.Int()
db.Put("test", strconv.Itoa(val), data)
}
}

func BenchmarkSequentialPutLargePayload(b *testing.B) {
dir := b.TempDir()
db, err := NewKVDB(filepath.Join(dir, "bolt.db"))

require.NoError(b, err)
defer db.Close()
data := make([]byte, 1024*1024)
for i := 0; i < b.N; i++ {
db.Put("test", strconv.Itoa(i), data)
}
}

func BenchmarkRandomPutLargePayload(b *testing.B) {
dir := b.TempDir()
db, err := NewKVDB(filepath.Join(dir, "bolt.db"))

require.NoError(b, err)
defer db.Close()
data := make([]byte, 1024*1024)
for i := 0; i < b.N; i++ {
val := rand.Int()
db.Put("test", strconv.Itoa(val), data)
}
}

func BenchmarkBatchPut(b *testing.B) {
dir := b.TempDir()
db, err := NewKVDB(filepath.Join(dir, "bolt.db"))

require.NoError(b, err)
defer db.Close()
data := []byte("data")
kvs := make(map[string][]byte)
for i := 0; i < 10_000; i++ {
kvs[strconv.Itoa(i)] = data
}
for i := 0; i < b.N; i++ {
db.PutRange("test", kvs)

Check failure on line 73 in service/kv/kv_bench_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

db.PutRange undefined (type *KVDB has no field or method PutRange)
}
}

func BenchmarkGet(b *testing.B) {
dir := b.TempDir()
db, err := NewKVDB(filepath.Join(dir, "bolt.db"))

require.NoError(b, err)
defer db.Close()
data := []byte("data")
kvs := make(map[string][]byte)
for i := 0; i < 10_000; i++ {
kvs[strconv.Itoa(i)] = data
}
db.PutRange("test", kvs)

Check failure on line 88 in service/kv/kv_bench_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

db.PutRange undefined (type *KVDB has no field or method PutRange)
for i := 0; i < b.N; i++ {
r := rand.Int31n(10_000)
val, found, _ := db.Get("test", strconv.Itoa(int(r)))
require.True(b, found)
require.True(b, string(val) == "data")
}
}

0 comments on commit e04828b

Please sign in to comment.