Skip to content

Commit

Permalink
[nspcc-dev#1329] pilorama: Allow to benchmark all tree backends
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
  • Loading branch information
fyrchik authored and aprasolova committed Oct 19, 2022
1 parent 99556b5 commit 41239ee
Showing 1 changed file with 51 additions and 42 deletions.
93 changes: 51 additions & 42 deletions pkg/local_object_storage/pilorama/forest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

var providers = []struct {
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()
require.NoError(t, f.Init())
require.NoError(t, f.Open())
Expand All @@ -25,7 +25,7 @@ var providers = []struct {

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.
tmpDir, err := os.MkdirTemp(os.TempDir(), "*")
require.NoError(t, err)
Expand Down Expand Up @@ -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()
treeID := "version"

Expand Down Expand Up @@ -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()
treeID := "version"
logs := []Move{
Expand Down Expand Up @@ -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)

const (
Expand Down Expand Up @@ -457,58 +457,67 @@ func testForestTreeApplyRandom(t *testing.T, constructor func(t *testing.T) Fore
const benchNodeCount = 1000

func BenchmarkApplySequential(b *testing.B) {
benchmarkApply(b, benchNodeCount, func(nodeCount, opCount int) []Move {
ops := make([]Move, opCount)
for i := range ops {
ops[i] = Move{
Parent: uint64(rand.Intn(nodeCount)),
Meta: Meta{
Time: Timestamp(i),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
},
Child: uint64(rand.Intn(nodeCount)),
}
}
return ops
})
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)
for i := range ops {
ops[i] = Move{
Parent: uint64(rand.Intn(benchNodeCount)),
Meta: Meta{
Time: Timestamp(i),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
},
Child: uint64(rand.Intn(benchNodeCount)),
}
}
return ops
})
})
}
}

func BenchmarkApplyReorderLast(b *testing.B) {
// Group operations in a blocks of 10, order blocks in increasing timestamp order,
// and operations in a single block in reverse.
const blockSize = 10

benchmarkApply(b, benchNodeCount, func(nodeCount, opCount int) []Move {
ops := make([]Move, opCount)
for i := range ops {
ops[i] = Move{
Parent: uint64(rand.Intn(nodeCount)),
Meta: Meta{
Time: Timestamp(i),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
},
Child: uint64(rand.Intn(nodeCount)),
}
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]
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)
for i := range ops {
ops[i] = Move{
Parent: uint64(rand.Intn(benchNodeCount)),
Meta: Meta{
Time: Timestamp(i),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
},
Child: uint64(rand.Intn(benchNodeCount)),
}
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)

s := newState()
ops := genFunc(n, b.N)
ops := genFunc(b.N)
cid := cidtest.ID()
treeID := "version"

b.ResetTimer()
b.ReportAllocs()
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)
}
}
Expand Down

0 comments on commit 41239ee

Please sign in to comment.