[#2061] node: Unify meta.Get benchmarks

Make them get exactly one (different) object per a bench iteration.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-11-30 15:17:02 +03:00 committed by fyrchik
parent bc905f169d
commit 3d0768a1d3

View file

@ -4,7 +4,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"os" "os"
"runtime"
"testing" "testing"
"time"
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase" meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
@ -171,6 +173,10 @@ func BenchmarkGet(b *testing.B) {
100, 100,
} }
defer func() {
_ = os.RemoveAll(b.Name())
}()
for _, num := range numOfObjects { for _, num := range numOfObjects {
b.Run(fmt.Sprintf("%d_objects", num), func(b *testing.B) { b.Run(fmt.Sprintf("%d_objects", num), func(b *testing.B) {
benchmarkGet(b, num) benchmarkGet(b, num)
@ -181,31 +187,37 @@ func BenchmarkGet(b *testing.B) {
var obj *objectSDK.Object var obj *objectSDK.Object
func benchmarkGet(b *testing.B, numOfObj int) { func benchmarkGet(b *testing.B, numOfObj int) {
db := newDB(b) prepareDb := func(batchSize int) (*meta.DB, []oid.Address) {
addrs := make([]oid.Address, 0, numOfObj) db := newDB(b,
meta.WithMaxBatchSize(batchSize),
meta.WithMaxBatchDelay(10*time.Millisecond),
)
addrs := make([]oid.Address, 0, numOfObj)
for i := 0; i < numOfObj; i++ { for i := 0; i < numOfObj; i++ {
raw := generateObject(b) raw := generateObject(b)
addrs = append(addrs, object.AddressOf(raw)) addrs = append(addrs, object.AddressOf(raw))
err := putBig(db, raw) err := putBig(db, raw)
require.NoError(b, err) require.NoError(b, err)
}
return db, addrs
} }
b.Cleanup(func() { db, addrs := prepareDb(runtime.NumCPU())
_ = db.Close()
_ = os.RemoveAll(b.Name())
})
var err error
var getPrm meta.GetPrm
getPrm.SetAddress(addrs[len(addrs)/2])
b.Run("parallel", func(b *testing.B) { b.Run("parallel", func(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
var counter int
for pb.Next() { for pb.Next() {
_, err = db.Get(getPrm) var getPrm meta.GetPrm
getPrm.SetAddress(addrs[counter%len(addrs)])
counter++
_, err := db.Get(getPrm)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
@ -213,16 +225,20 @@ func benchmarkGet(b *testing.B, numOfObj int) {
}) })
}) })
require.NoError(b, db.Close())
require.NoError(b, os.RemoveAll(b.Name()))
db, addrs = prepareDb(1)
b.Run("serial", func(b *testing.B) { b.Run("serial", func(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for _, addr := range addrs { var getPrm meta.GetPrm
getPrm.SetAddress(addr) getPrm.SetAddress(addrs[i%len(addrs)])
_, err := db.Get(getPrm) _, err := db.Get(getPrm)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
}
} }
} }
}) })