[#1329] pilorama: Allow to benchmark all tree backends

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-11 16:59:35 +03:00
parent 9cbd4271f1
commit 35fa445195

View file

@ -13,9 +13,9 @@ import (
var providers = []struct { var providers = []struct {
name string name string
construct func(t *testing.T) Forest construct func(t testing.TB) Forest
}{ }{
{"inmemory", func(t *testing.T) Forest { {"inmemory", func(t testing.TB) Forest {
f := NewMemoryForest() f := NewMemoryForest()
require.NoError(t, f.Init()) require.NoError(t, f.Init())
require.NoError(t, f.Open()) require.NoError(t, f.Open())
@ -25,7 +25,7 @@ var providers = []struct {
return f return f
}}, }},
{"bbolt", func(t *testing.T) Forest { {"bbolt", func(t testing.TB) Forest {
// Use `os.TempDir` because we construct multiple times in the same test. // Use `os.TempDir` because we construct multiple times in the same test.
tmpDir, err := os.MkdirTemp(os.TempDir(), "*") tmpDir, err := os.MkdirTemp(os.TempDir(), "*")
require.NoError(t, err) require.NoError(t, err)
@ -290,7 +290,7 @@ func TestForest_Apply(t *testing.T) {
} }
} }
func testForestTreeApply(t *testing.T, constructor func(t *testing.T) Forest) { func testForestTreeApply(t *testing.T, constructor func(t testing.TB) Forest) {
cid := cidtest.ID() cid := cidtest.ID()
treeID := "version" treeID := "version"
@ -332,7 +332,7 @@ func TestForest_GetOpLog(t *testing.T) {
} }
} }
func testForestTreeGetOpLog(t *testing.T, constructor func(t *testing.T) Forest) { func testForestTreeGetOpLog(t *testing.T, constructor func(t testing.TB) Forest) {
cid := cidtest.ID() cid := cidtest.ID()
treeID := "version" treeID := "version"
logs := []Move{ logs := []Move{
@ -390,7 +390,7 @@ func TestForest_ApplyRandom(t *testing.T) {
} }
} }
func testForestTreeApplyRandom(t *testing.T, constructor func(t *testing.T) Forest) { func testForestTreeApplyRandom(t *testing.T, constructor func(t testing.TB) Forest) {
rand.Seed(42) rand.Seed(42)
const ( const (
@ -457,20 +457,24 @@ func testForestTreeApplyRandom(t *testing.T, constructor func(t *testing.T) Fore
const benchNodeCount = 1000 const benchNodeCount = 1000
func BenchmarkApplySequential(b *testing.B) { func BenchmarkApplySequential(b *testing.B) {
benchmarkApply(b, benchNodeCount, func(nodeCount, opCount int) []Move { for i := range providers {
b.Run(providers[i].name, func(b *testing.B) {
benchmarkApply(b, providers[i].construct(b), func(opCount int) []Move {
ops := make([]Move, opCount) ops := make([]Move, opCount)
for i := range ops { for i := range ops {
ops[i] = Move{ ops[i] = Move{
Parent: uint64(rand.Intn(nodeCount)), Parent: uint64(rand.Intn(benchNodeCount)),
Meta: Meta{ Meta: Meta{
Time: Timestamp(i), Time: Timestamp(i),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}}, Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
}, },
Child: uint64(rand.Intn(nodeCount)), Child: uint64(rand.Intn(benchNodeCount)),
} }
} }
return ops return ops
}) })
})
}
} }
func BenchmarkApplyReorderLast(b *testing.B) { func BenchmarkApplyReorderLast(b *testing.B) {
@ -478,16 +482,18 @@ func BenchmarkApplyReorderLast(b *testing.B) {
// and operations in a single block in reverse. // and operations in a single block in reverse.
const blockSize = 10 const blockSize = 10
benchmarkApply(b, benchNodeCount, func(nodeCount, opCount int) []Move { for i := range providers {
b.Run(providers[i].name, func(b *testing.B) {
benchmarkApply(b, providers[i].construct(b), func(opCount int) []Move {
ops := make([]Move, opCount) ops := make([]Move, opCount)
for i := range ops { for i := range ops {
ops[i] = Move{ ops[i] = Move{
Parent: uint64(rand.Intn(nodeCount)), Parent: uint64(rand.Intn(benchNodeCount)),
Meta: Meta{ Meta: Meta{
Time: Timestamp(i), Time: Timestamp(i),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}}, Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
}, },
Child: uint64(rand.Intn(nodeCount)), Child: uint64(rand.Intn(benchNodeCount)),
} }
if i != 0 && i%blockSize == 0 { if i != 0 && i%blockSize == 0 {
for j := 0; j < blockSize/2; j++ { for j := 0; j < blockSize/2; j++ {
@ -497,18 +503,21 @@ func BenchmarkApplyReorderLast(b *testing.B) {
} }
return ops return ops
}) })
})
}
} }
func benchmarkApply(b *testing.B, n int, genFunc func(int, int) []Move) { func benchmarkApply(b *testing.B, s Forest, genFunc func(int) []Move) {
rand.Seed(42) rand.Seed(42)
s := newState() ops := genFunc(b.N)
ops := genFunc(n, b.N) cid := cidtest.ID()
treeID := "version"
b.ResetTimer() b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := range ops { for i := range ops {
if err := s.Apply(&ops[i]); err != nil { if err := s.TreeApply(cid, treeID, &ops[i]); err != nil {
b.Fatalf("error in `Apply`: %v", err) b.Fatalf("error in `Apply`: %v", err)
} }
} }