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

Benchmark tests for DBs #192

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 19 additions & 90 deletions blockchain/storagev2/leveldb/leveldb_perf_test.go
Original file line number Diff line number Diff line change
@@ -1,119 +1,48 @@
package leveldb

import (
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/0xPolygon/polygon-edge/blockchain/storagev2"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/syndtr/goleveldb/leveldb/opt"
)

func openStorage(t *testing.T, p string) (*storagev2.Storage, func(), string) {
t.Helper()
func openStorage(b *testing.B, p string) (*storagev2.Storage, func(), string) {
b.Helper()

s, err := NewLevelDBStorage(p, hclog.NewNullLogger())
require.NoError(t, err)
require.NoError(b, err)

closeFn := func() {
require.NoError(t, s.Close())
require.NoError(b, s.Close())

if err := s.Close(); err != nil {
t.Fatal(err)
b.Fatal(err)
}

require.NoError(t, os.RemoveAll(p))
require.NoError(b, os.RemoveAll(p))
}

return s, closeFn, p
}

func dbSize(t *testing.T, path string) int64 {
t.Helper()
func Benchmark(b *testing.B) {
b.StopTimer()

var size int64
s, cleanUpFn, path := openStorage(b, "/tmp/leveldbV2-test-perf")
defer func() {
s.Close()
cleanUpFn()
}()

err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
t.Fail()
}

if info != nil && !info.IsDir() && strings.Contains(info.Name(), ".ldb") {
size += info.Size()
}

return err
})
if err != nil {
t.Log(err)
}

return size
}

func TestWriteBlockPerf(t *testing.T) {
t.SkipNow()

s, _, path := openStorage(t, "/tmp/leveldbV2-test")
defer s.Close()

var watchTime int64

count := 10000
b := storagev2.CreateBlock(t)

for i := 1; i <= count; i++ {
storagev2.UpdateBlock(t, uint64(i), b)
batchWriter := storagev2.PrepareBatch(t, s, b)

tn := time.Now().UTC()

require.NoError(t, batchWriter.WriteBatch())
blockCount := 1000
storagev2.BenchmarkStorage(b, blockCount, s, 25, 15) // CI times

d := time.Since(tn)
watchTime += d.Milliseconds()
}

time.Sleep(time.Second)

size := dbSize(t, path)
t.Logf("\tdb size %d MB", size/(1*opt.MiB))
t.Logf("\ttotal WriteBatch %d ms", watchTime)
}

func TestReadBlockPerf(t *testing.T) {
t.SkipNow()

s, _, _ := openStorage(t, "/tmp/leveldbV2-test")
defer s.Close()

var watchTime int64

count := 1000
for i := 1; i <= count; i++ {
n := uint64(1 + rand.Intn(10000))

tn := time.Now().UTC()
h, ok := s.ReadCanonicalHash(n)
_, err1 := s.ReadBody(n, h)
_, err3 := s.ReadHeader(n, h)
_, err4 := s.ReadReceipts(n, h)
b, err5 := s.ReadBlockLookup(h)
d := time.Since(tn)

watchTime += d.Milliseconds()

if !ok || err1 != nil || err3 != nil || err4 != nil || err5 != nil {
t.Logf("\terror")
}

assert.Equal(t, n, b)
}
t.Logf("\ttotal read %d ms", watchTime)
size, err := dbSize(path)
require.NoError(b, err)
b.Logf("\tldb file count: %d", countLdbFilesInPath(path))
b.Logf("\tdb size %d MB", size/(1*opt.MiB))
}
58 changes: 17 additions & 41 deletions blockchain/storagev2/leveldb/leveldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
Expand All @@ -18,10 +19,10 @@ import (
"github.com/syndtr/goleveldb/leveldb/opt"
)

func newStorage(t *testing.T) (*storagev2.Storage, func()) {
func newStorage(t *testing.T) (*storagev2.Storage, func(), string) {
t.Helper()

path, err := os.MkdirTemp("/tmp", "minimal_storage")
path, err := os.MkdirTemp("", "leveldbV2")
if err != nil {
t.Fatal(err)
}
Expand All @@ -41,31 +42,7 @@ func newStorage(t *testing.T) (*storagev2.Storage, func()) {
}
}

return s, closeFn
}

func newStorageP(t *testing.T) (*storagev2.Storage, func(), string) {
t.Helper()

p, err := os.MkdirTemp("", "leveldbV2-test")
require.NoError(t, err)

require.NoError(t, os.MkdirAll(p, 0755))

s, err := NewLevelDBStorage(p, hclog.NewNullLogger())
require.NoError(t, err)

closeFn := func() {
require.NoError(t, s.Close())

if err := s.Close(); err != nil {
t.Fatal(err)
}

require.NoError(t, os.RemoveAll(p))
}

return s, closeFn, p
return s, closeFn, path
}

func countLdbFilesInPath(path string) int {
Expand All @@ -79,27 +56,22 @@ func countLdbFilesInPath(path string) int {
return len(files)
}

func dirSize(t *testing.T, path string) int64 {
t.Helper()

func dbSize(path string) (int64, error) {
var size int64

err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
t.Fail()
return err
}

if !info.IsDir() {
if info != nil && !info.IsDir() && strings.Contains(info.Name(), ".ldb") {
size += info.Size()
}

return err
})
if err != nil {
t.Log(err)
}

return size
return size, err
}

func writeBlock(t *testing.T, s *storagev2.Storage, b *types.FullBlock) {
Expand Down Expand Up @@ -171,13 +143,16 @@ func TestStorage(t *testing.T) {
}

func TestWriteReadFullBlockInParallel(t *testing.T) {
s, _, path := newStorageP(t)
defer s.Close()
s, cleanUpFn, path := newStorage(t)
defer func() {
s.Close()
cleanUpFn()
}()

var wg sync.WaitGroup

blockCount := 100
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)

signchan := make(chan os.Signal, 1)
signal.Notify(signchan, syscall.SIGINT)
Expand Down Expand Up @@ -208,8 +183,9 @@ insertloop:
}
}

size := dirSize(t, path)
size, err := dbSize(path)
require.NoError(t, err)
t.Logf("\tldb file count: %d", countLdbFilesInPath(path))
t.Logf("\tdir size %d MBs", size/(1*opt.MiB))
t.Logf("\tdb size %d MBs", size/(1*opt.MiB))
wg.Wait()
}
83 changes: 18 additions & 65 deletions blockchain/storagev2/mdbx/mdbx_perf_test.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,46 @@
package mdbx

import (
"math/rand"
"os"
"testing"
"time"

"github.com/0xPolygon/polygon-edge/blockchain/storagev2"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func openStorage(t *testing.T, p string) (*storagev2.Storage, func(), string) {
t.Helper()
func openStorage(b *testing.B, p string) (*storagev2.Storage, func(), string) {
b.Helper()

s, err := NewMdbxStorage(p, hclog.NewNullLogger())
require.NoError(t, err)
require.NoError(b, err)

closeFn := func() {
require.NoError(t, s.Close())
require.NoError(b, s.Close())

if err := s.Close(); err != nil {
t.Fatal(err)
b.Fatal(err)
}

require.NoError(t, os.RemoveAll(p))
require.NoError(b, os.RemoveAll(p))
}

return s, closeFn, p
}

func TestWriteBlockPerf(t *testing.T) {
t.SkipNow()
func Benchmark(b *testing.B) {
b.StopTimer()

s, _, path := openStorage(t, "/tmp/mdbx-test")
defer s.Close()
s, cleanUpFn, path := openStorage(b, "/tmp/mdbx-test-perf")
defer func() {
s.Close()
cleanUpFn()
}()

var watchTime int64
blockCount := 1000
storagev2.BenchmarkStorage(b, blockCount, s, 40, 18) // CI times

count := 10000
b := storagev2.CreateBlock(t)

for i := 1; i <= count; i++ {
storagev2.UpdateBlock(t, uint64(i), b)
batchWriter := storagev2.PrepareBatch(t, s, b)

tn := time.Now().UTC()

require.NoError(t, batchWriter.WriteBatch())

d := time.Since(tn)
watchTime += d.Milliseconds()
}

time.Sleep(time.Second)

size := dbSize(t, path)
t.Logf("\tdb size %d MB", size/(1024*1024))
t.Logf("\ttotal WriteBatch %d ms", watchTime)
}

func TestReadBlockPerf(t *testing.T) {
t.SkipNow()

s, _, _ := openStorage(t, "/tmp/mdbx-test")
defer s.Close()

var watchTime int64

count := 1000
for i := 1; i <= count; i++ {
n := uint64(1 + rand.Intn(10000))

tn := time.Now().UTC()
h, ok := s.ReadCanonicalHash(n)
_, err1 := s.ReadBody(n, h)
_, err3 := s.ReadHeader(n, h)
_, err4 := s.ReadReceipts(n, h)
b, err5 := s.ReadBlockLookup(h)
d := time.Since(tn)

watchTime += d.Milliseconds()

if !ok || err1 != nil || err3 != nil || err4 != nil || err5 != nil {
t.Logf("\terror")
}

assert.Equal(t, n, b)
}
t.Logf("\ttotal read %d ms", watchTime)
size, err := dbSize(path)
require.NoError(b, err)
b.Logf("\tdb size %d MB", size/1048576)
}
Loading
Loading