From c85a0bc86601d4820bab4357f1396f7c066f26de Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 11 Apr 2023 20:20:32 +0300 Subject: [PATCH] [#236] blobstor/test: Reduce test descriptions I tried to add 4 more tests and suddenly, it became harder to navigate in code. Move directory creation in a common function. Signed-off-by: Evgenii Stratonikov --- .../blobstor/perf_test.go | 84 ++++++++----------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/pkg/local_object_storage/blobstor/perf_test.go b/pkg/local_object_storage/blobstor/perf_test.go index 0fd3048d3..a593e1bf4 100644 --- a/pkg/local_object_storage/blobstor/perf_test.go +++ b/pkg/local_object_storage/blobstor/perf_test.go @@ -13,60 +13,65 @@ import ( "github.com/stretchr/testify/require" ) +type storage struct { + desc string + create func(string) common.Storage +} + +func (s storage) open(b *testing.B) common.Storage { + dir, err := os.MkdirTemp(os.TempDir(), s.desc) + if err != nil { + b.Fatalf("creating %s root path: %v", s.desc, err) + } + st := s.create(dir) + + require.NoError(b, st.Open(false)) + require.NoError(b, st.Init()) + + b.Cleanup(func() { + require.NoError(b, st.Close()) + require.NoError(b, os.RemoveAll(dir)) + }) + + return st +} + // The storages to benchmark. Each storage has a description and a function which returns the actual // storage along with a cleanup function. -var storages = []struct { - desc string - create func(*testing.B) (common.Storage, func()) -}{ +var storages = []storage{ { desc: "memstore", - create: func(*testing.B) (common.Storage, func()) { - return memstore.New(), func() {} + create: func(string) common.Storage { + return memstore.New() }, }, { desc: "fstree_nosync", - create: func(b *testing.B) (common.Storage, func()) { - dir, err := os.MkdirTemp(os.TempDir(), "fstree_nosync") - if err != nil { - b.Fatalf("creating fstree_nosync root path: %v", err) - } - cleanup := func() { os.RemoveAll(dir) } + create: func(dir string) common.Storage { return fstree.New( fstree.WithPath(dir), fstree.WithDepth(2), fstree.WithDirNameLen(2), fstree.WithNoSync(true), - ), cleanup + ) }, }, { desc: "fstree", - create: func(b *testing.B) (common.Storage, func()) { - dir, err := os.MkdirTemp(os.TempDir(), "fstree") - if err != nil { - b.Fatalf("creating fstree root path: %v", err) - } - cleanup := func() { os.RemoveAll(dir) } + create: func(dir string) common.Storage { return fstree.New( fstree.WithPath(dir), fstree.WithDepth(2), fstree.WithDirNameLen(2), - ), cleanup + ) }, }, { desc: "blobovniczatree", - create: func(b *testing.B) (common.Storage, func()) { - dir, err := os.MkdirTemp(os.TempDir(), "blobovniczatree") - if err != nil { - b.Fatalf("creating blobovniczatree root path: %v", err) - } - cleanup := func() { os.RemoveAll(dir) } + create: func(dir string) common.Storage { return blobovniczatree.NewBlobovniczaTree( blobovniczatree.WithRootPath(dir), - ), cleanup + ) }, }, } @@ -95,10 +100,7 @@ func BenchmarkSubstorageReadPerf(b *testing.B) { for _, stEntry := range storages { b.Run(fmt.Sprintf("%s-%s", stEntry.desc, tt.desc), func(b *testing.B) { objGen := tt.objGen() - st, cleanup := stEntry.create(b) - - require.NoError(b, st.Open(false)) - require.NoError(b, st.Init()) + st := stEntry.open(b) // Fill database for i := 0; i < tt.size; i++ { @@ -123,9 +125,6 @@ func BenchmarkSubstorageReadPerf(b *testing.B) { require.NoError(b, err) } }) - - require.NoError(b, st.Close()) - cleanup() }) } } @@ -150,10 +149,7 @@ func BenchmarkSubstorageWritePerf(b *testing.B) { for _, stEntry := range storages { b.Run(fmt.Sprintf("%s-%s", stEntry.desc, genEntry.desc), func(b *testing.B) { gen := genEntry.create() - st, cleanup := stEntry.create(b) - - require.NoError(b, st.Open(false)) - require.NoError(b, st.Init()) + st := stEntry.open(b) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { @@ -170,9 +166,6 @@ func BenchmarkSubstorageWritePerf(b *testing.B) { } } }) - - require.NoError(b, st.Close()) - cleanup() }) } } @@ -194,10 +187,7 @@ func BenchmarkSubstorageIteratePerf(b *testing.B) { for _, stEntry := range storages { b.Run(fmt.Sprintf("%s-%s", stEntry.desc, tt.desc), func(b *testing.B) { objGen := tt.objGen() - st, cleanup := stEntry.create(b) - - require.NoError(b, st.Open(false)) - require.NoError(b, st.Init()) + st := stEntry.open(b) // Fill database for i := 0; i < tt.size; i++ { @@ -224,10 +214,6 @@ func BenchmarkSubstorageIteratePerf(b *testing.B) { }) require.NoError(b, err) require.Equal(b, tt.size, cnt) - b.StopTimer() - - require.NoError(b, st.Close()) - cleanup() }) } }