Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize memory usage #287

Closed
roseduan opened this issue Sep 18, 2023 · 0 comments
Closed

Optimize memory usage #287

roseduan opened this issue Sep 18, 2023 · 0 comments

Comments

@roseduan
Copy link
Collaborator

roseduan commented Sep 18, 2023

package kv

import (
	"fmt"
	"github.com/cockroachdb/pebble"
	"github.com/rosedblabs/rosedb/v2"
	"math"
	"math/rand"
	"sync"
	"testing"
	"time"
)

var pebbledb *pebble.DB
var rdb *rosedb.DB

func init() {
	var err error
	opt := &pebble.Options{BytesPerSync: math.MaxInt, WALBytesPerSync: math.MaxInt}
	pebbledb, err = pebble.Open("/tmp/pebble-bench", opt)
	if err != nil {
		panic(err)
	}

	options := rosedb.DefaultOptions
	options.DirPath = "/tmp/rosedb-bench"
	rdb, err = rosedb.Open(options)
	if err != nil {
		panic(err)
	}
}

func BenchmarkPebble_Get(b *testing.B) {
	for i := 0; i < 1000000; i++ {
		pebbledb.Set(GetTestKey(i), RandomValue(128), &pebble.WriteOptions{Sync: false})
	}

	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, _, err := pebbledb.Get(GetTestKey(i))
		if err != nil && err != pebble.ErrNotFound {
			panic(err)
		}
	}
}

func BenchmarkRoseDB_Get(b *testing.B) {
	for i := 0; i < 1000000; i++ {
		rdb.Put(GetTestKey(i), RandomValue(128))
	}

	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := rdb.Get(GetTestKey(i))
		if err != nil && err != rosedb.ErrKeyNotFound {
			panic(err)
		}
	}
}

var (
	lock    = sync.Mutex{}
	randStr = rand.New(rand.NewSource(time.Now().Unix()))
	letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
)

// GetTestKey get formated key, for test only
func GetTestKey(i int) []byte {
	return []byte(fmt.Sprintf("rosedb-test-key-%09d", i))
}

// RandomValue generate random value, for test only
func RandomValue(n int) []byte {
	b := make([]byte, n)
	for i := range b {
		lock.Lock()
		b[i] = letters[randStr.Intn(len(letters))]
		lock.Unlock()
	}
	return []byte("rosedb-test-value-" + string(b))
}

Run RoseDB:

go test -v -bench=BenchmarkRoseDB_Get -memprofile rosedb.out

Run Pebble:

go test -v -bench=BenchmarkPebble_Get -memprofile pebble.out

Analyze:

go tool pprof rosedb.out
go tool pprof pebble.out
image image
@roseduan roseduan closed this as completed Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant