From dab6bcabe469974cc0eae3201036a191d29f132a Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 22 Feb 2022 10:35:53 +0300 Subject: [PATCH] [#1146] engine: Add benchmarks for exists Signed-off-by: Evgenii Stratonikov --- .../engine/engine_test.go | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/pkg/local_object_storage/engine/engine_test.go b/pkg/local_object_storage/engine/engine_test.go index 5dfe0b612..0b424fe2d 100644 --- a/pkg/local_object_storage/engine/engine_test.go +++ b/pkg/local_object_storage/engine/engine_test.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "fmt" "math/rand" + "os" "path/filepath" "sync" "testing" @@ -16,7 +17,9 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/util/test" "github.com/nspcc-dev/neofs-sdk-go/checksum" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" + cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" + objecttest "github.com/nspcc-dev/neofs-sdk-go/object/address/test" oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/nspcc-dev/neofs-sdk-go/owner" ownertest "github.com/nspcc-dev/neofs-sdk-go/owner/test" @@ -28,6 +31,49 @@ import ( "go.uber.org/zap" ) +func BenchmarkExists(b *testing.B) { + b.Run("2 shards", func(b *testing.B) { + benchmarkExists(b, 2) + }) + b.Run("4 shards", func(b *testing.B) { + benchmarkExists(b, 4) + }) + b.Run("8 shards", func(b *testing.B) { + benchmarkExists(b, 8) + }) +} + +func benchmarkExists(b *testing.B, shardNum int) { + shards := make([]*shard.Shard, shardNum) + for i := 0; i < shardNum; i++ { + shards[i] = testNewShard(b, i) + } + + e := testNewEngineWithShards(shards...) + b.Cleanup(func() { + _ = e.Close() + _ = os.RemoveAll(b.Name()) + }) + + addr := objecttest.Address() + for i := 0; i < 100; i++ { + obj := generateRawObjectWithCID(b, cidtest.ID()) + err := Put(e, obj.Object()) + if err != nil { + b.Fatal(err) + } + } + + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + ok, err := e.exists(addr) + if err != nil || ok { + b.Fatalf("%t %v", ok, err) + } + } +} + func testNewEngineWithShards(shards ...*shard.Shard) *StorageEngine { engine := &StorageEngine{ cfg: &cfg{ @@ -54,7 +100,7 @@ func testNewEngineWithShards(shards ...*shard.Shard) *StorageEngine { return engine } -func testNewShard(t *testing.T, id int) *shard.Shard { +func testNewShard(t testing.TB, id int) *shard.Shard { sid, err := generateShardID() require.NoError(t, err) @@ -88,7 +134,7 @@ func testOID() *oidSDK.ID { return id } -func generateRawObjectWithCID(t *testing.T, cid *cid.ID) *object.RawObject { +func generateRawObjectWithCID(t testing.TB, cid *cid.ID) *object.RawObject { version := version.New() version.SetMajor(2) version.SetMinor(1)