github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/benchmark/db_bench_test.go (about)

     1  package benchmark
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/flower-corp/rosedb"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  var roseDB *rosedb.RoseDB
    14  
    15  func init() {
    16  	path := filepath.Join("/tmp", "rosedb_bench")
    17  	opts := rosedb.DefaultOptions(path)
    18  	var err error
    19  	roseDB, err = rosedb.Open(opts)
    20  	if err != nil {
    21  		panic(fmt.Sprintf("open rosedb err: %v", err))
    22  	}
    23  	initDataForGet()
    24  }
    25  
    26  func initDataForGet() {
    27  	writeCount := 800000
    28  	for i := 0; i < writeCount; i++ {
    29  		err := roseDB.Set(getKey(i), getValue128B())
    30  		if err != nil {
    31  			panic(err)
    32  		}
    33  	}
    34  }
    35  
    36  func BenchmarkRoseDB_Set(b *testing.B) {
    37  	b.ResetTimer()
    38  	b.ReportAllocs()
    39  
    40  	for i := 0; i < b.N; i++ {
    41  		err := roseDB.Set(getKey(i), getValue128B())
    42  		assert.Nil(b, err)
    43  	}
    44  }
    45  
    46  func BenchmarkRoseDB_Get(b *testing.B) {
    47  	b.ResetTimer()
    48  	b.ReportAllocs()
    49  	for i := 0; i < b.N; i++ {
    50  		_, err := roseDB.Get(getKey(i))
    51  		assert.Nil(b, err)
    52  	}
    53  }
    54  
    55  func BenchmarkRoseDB_LPush(b *testing.B) {
    56  	keys := [][]byte{
    57  		[]byte("my_list-1"),
    58  		[]byte("my_list-2"),
    59  		[]byte("my_list-3"),
    60  		[]byte("my_list-4"),
    61  	}
    62  
    63  	b.ResetTimer()
    64  	b.ReportAllocs()
    65  	for i := 0; i < b.N; i++ {
    66  		k := rand.Int() % len(keys)
    67  		err := roseDB.LPush(keys[k], getValue128B())
    68  		assert.Nil(b, err)
    69  	}
    70  }
    71  
    72  func BenchmarkRoseDB_ZAdd(b *testing.B) {
    73  	keys := [][]byte{
    74  		[]byte("my_zset-1"),
    75  		[]byte("my_zset-2"),
    76  		[]byte("my_zset-3"),
    77  		[]byte("my_zset-4"),
    78  	}
    79  	b.ResetTimer()
    80  	b.ReportAllocs()
    81  	for i := 0; i < b.N; i++ {
    82  		k := rand.Int() % len(keys)
    83  		err := roseDB.ZAdd(keys[k], float64(i+100), getValue128B())
    84  		assert.Nil(b, err)
    85  	}
    86  }