Skip to content

Commit

Permalink
added more benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
JkLondon committed Jun 27, 2024
1 parent a82c2e7 commit dbe7bdc
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 3 deletions.
94 changes: 93 additions & 1 deletion mdbx/cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/rand"
"os"
"reflect"
"runtime"
Expand Down Expand Up @@ -1566,7 +1567,7 @@ func BenchmarkCursor_Renew(b *testing.B) {
})
}

func BenchmarkCursor_SetRange(b *testing.B) {
func BenchmarkCursor_SetRange_OneKey(b *testing.B) {
env, _ := setup(b)

var db DBI
Expand Down Expand Up @@ -1606,3 +1607,94 @@ func BenchmarkCursor_SetRange(b *testing.B) {
b.Errorf("put: %v", err)
}
}

func BenchmarkCursor_SetRange_Sequence(b *testing.B) {
env, _ := setup(b)

var db DBI
keys := make([][]byte, b.N, b.N)
for i := range keys {
keys[i] = make([]byte, 8)
binary.BigEndian.PutUint64(keys[i], uint64(i))
}

if err := env.Update(func(txn *Txn) (err error) {
db, err = txn.OpenRoot(0)
if err != nil {
return err
}
for _, k := range keys {
err = txn.Put(db, k, k, 0)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("dbi: %v", err)
return
}

if err := env.View(func(txn *Txn) (err error) {
c, err := txn.OpenCursor(db)
if err != nil {
return err
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err = c.Get(keys[i], nil, SetRange)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("put: %v", err)
}
}

func BenchmarkCursor_SetRange_Random(b *testing.B) {
env, _ := setup(b)

var db DBI
keys := make([][]byte, b.N, b.N)
for i := range keys {
keys[i] = make([]byte, 8)
binary.BigEndian.PutUint64(keys[i], uint64(rand.Intn(100*b.N)))
}

if err := env.Update(func(txn *Txn) (err error) {
db, err = txn.OpenRoot(0)
if err != nil {
return err
}
for _, k := range keys {
err = txn.Put(db, k, k, 0)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("dbi: %v", err)
return
}

if err := env.View(func(txn *Txn) (err error) {
b.ResetTimer()
c, err := txn.OpenCursor(db)
if err != nil {
return err
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err = c.Get(keys[i], nil, SetRange)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("put: %v", err)
}
}
2 changes: 1 addition & 1 deletion mdbx/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func setupFlags(t testing.TB, flags uint) (env *Env, path string) {
t.Fatalf("setmaxdbs: %v", err)
}
const pageSize = 4096
err = env.SetGeometry(-1, -1, 64*1024*pageSize, -1, -1, pageSize)
err = env.SetGeometry(-1, -1, 256*64*1024*pageSize, -1, -1, pageSize)
if err != nil {
t.Fatalf("setmaxdbs: %v", err)
}
Expand Down
85 changes: 84 additions & 1 deletion mdbx/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"math/rand"
"runtime"
"syscall"
"testing"
Expand Down Expand Up @@ -1263,7 +1264,7 @@ func openDBI(env *Env, key string, flags uint) (DBI, error) {
return db, nil
}

func BenchmarkTxn_Get(b *testing.B) {
func BenchmarkTxn_Get_OneKey(b *testing.B) {
env, _ := setup(b)

var db DBI
Expand Down Expand Up @@ -1299,6 +1300,88 @@ func BenchmarkTxn_Get(b *testing.B) {
}
}

func BenchmarkTxn_Get_Sequence(b *testing.B) {
env, _ := setup(b)

var db DBI
keys := make([][]byte, b.N, b.N)
for i := range keys {
keys[i] = make([]byte, 8)
binary.BigEndian.PutUint64(keys[i], uint64(i))
}

if err := env.Update(func(txn *Txn) (err error) {
db, err = txn.OpenRoot(0)
if err != nil {
return err
}
for _, k := range keys {
err = txn.Put(db, k, k, 0)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("dbi: %v", err)
return
}

if err := env.View(func(txn *Txn) (err error) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := txn.Get(db, keys[i])
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("put: %v", err)
}
}

func BenchmarkTxn_Get_Random(b *testing.B) {
env, _ := setup(b)

var db DBI
keys := make([][]byte, b.N, b.N)
for i := range keys {
keys[i] = make([]byte, 8)
binary.BigEndian.PutUint64(keys[i], uint64(rand.Intn(100*b.N)))
}

if err := env.Update(func(txn *Txn) (err error) {
db, err = txn.OpenRoot(0)
if err != nil {
return err
}
for _, k := range keys {
err = txn.Put(db, k, k, 0)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("dbi: %v", err)
return
}

if err := env.View(func(txn *Txn) (err error) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := txn.Get(db, keys[i])
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("put: %v", err)
}
}

func TestTxnEnvWarmup(t *testing.T) {
env, _ := setup(t)

Expand Down

0 comments on commit dbe7bdc

Please sign in to comment.