[#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 committed by fyrchik
parent 536857ea5a
commit 835170e452

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 {
ops := make([]Move, opCount) b.Run(providers[i].name, func(b *testing.B) {
for i := range ops { benchmarkApply(b, providers[i].construct(b), func(opCount int) []Move {
ops[i] = Move{ ops := make([]Move, opCount)
Parent: uint64(rand.Intn(nodeCount)), for i := range ops {
Meta: Meta{ ops[i] = Move{
Time: Timestamp(i), Parent: uint64(rand.Intn(benchNodeCount)),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}}, Meta: Meta{
}, Time: Timestamp(i),
Child: uint64(rand.Intn(nodeCount)), Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
} },
} Child: uint64(rand.Intn(benchNodeCount)),
return ops }
}) }
return ops
})
})
}
} }
func BenchmarkApplyReorderLast(b *testing.B) { func BenchmarkApplyReorderLast(b *testing.B) {
@ -478,37 +482,42 @@ 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 {
ops := make([]Move, opCount) b.Run(providers[i].name, func(b *testing.B) {
for i := range ops { benchmarkApply(b, providers[i].construct(b), func(opCount int) []Move {
ops[i] = Move{ ops := make([]Move, opCount)
Parent: uint64(rand.Intn(nodeCount)), for i := range ops {
Meta: Meta{ ops[i] = Move{
Time: Timestamp(i), Parent: uint64(rand.Intn(benchNodeCount)),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}}, Meta: Meta{
}, Time: Timestamp(i),
Child: uint64(rand.Intn(nodeCount)), Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
} },
if i != 0 && i%blockSize == 0 { Child: uint64(rand.Intn(benchNodeCount)),
for j := 0; j < blockSize/2; j++ { }
ops[i-j], ops[i+j-blockSize] = ops[i+j-blockSize], ops[i-j] if i != 0 && i%blockSize == 0 {
for j := 0; j < blockSize/2; j++ {
ops[i-j], ops[i+j-blockSize] = ops[i+j-blockSize], ops[i-j]
}
}
} }
} 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)
} }
} }