From 68d59280d82aa478cba285f667a491b7b3f9a003 Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Fri, 15 Sep 2023 00:02:13 +0300 Subject: [PATCH 1/7] add benchmarks for all storages --- arangodb/arangodb_test.go | 62 ++++++++++++++++++++++++++++++------- azureblob/azureblob_test.go | 44 ++++++++++++++++++++++++++ badger/badger_test.go | 40 ++++++++++++++++++++++++ bbolt/bbolt_test.go | 40 ++++++++++++++++++++++++ coherence/coherence_test.go | 62 +++++++++++++++++++++++++++++++++---- couchbase/couchbase_test.go | 61 ++++++++++++++++++++++++++++++++++++ etcd/etcd_test.go | 52 +++++++++++++++++++++++++++++++ memcache/memcache_test.go | 40 ++++++++++++++++++++++++ memory/memory_test.go | 40 ++++++++++++++++++++++++ minio/minio_test.go | 40 ++++++++++++++++++++++++ mongodb/mongodb_test.go | 40 ++++++++++++++++++++++++ mssql/mssql_test.go | 40 ++++++++++++++++++++++++ mysql/mysql_test.go | 40 ++++++++++++++++++++++++ pebble/pebble_test.go | 40 ++++++++++++++++++++++++ postgres/postgres_test.go | 40 ++++++++++++++++++++++++ redis/redis_test.go | 40 ++++++++++++++++++++++++ ristretto/ristretto_test.go | 40 ++++++++++++++++++++++++ rueidis/rueidis_test.go | 40 ++++++++++++++++++++++++ s3/s3_test.go | 40 ++++++++++++++++++++++++ sqlite3/sqlite3_test.go | 40 ++++++++++++++++++++++++ 20 files changed, 864 insertions(+), 17 deletions(-) diff --git a/arangodb/arangodb_test.go b/arangodb/arangodb_test.go index 9f25b201..9ce6ae67 100644 --- a/arangodb/arangodb_test.go +++ b/arangodb/arangodb_test.go @@ -11,7 +11,7 @@ var testStore = New(Config{ Reset: true, }) -func Test_ARANGODB_Set(t *testing.T) { +func Test_ArangoDB_Set(t *testing.T) { var ( key = "john" val = []byte("doe") @@ -21,7 +21,7 @@ func Test_ARANGODB_Set(t *testing.T) { require.NoError(t, err) } -func Test_ARANGODB_Upsert(t *testing.T) { +func Test_ArangoDB_Upsert(t *testing.T) { var ( key = "john" val = []byte("doe") @@ -34,7 +34,7 @@ func Test_ARANGODB_Upsert(t *testing.T) { require.NoError(t, err) } -func Test_ARANGODB_Get(t *testing.T) { +func Test_ArangoDB_Get(t *testing.T) { var ( key = "john" val = []byte("doe") @@ -48,7 +48,7 @@ func Test_ARANGODB_Get(t *testing.T) { require.Equal(t, val, result) } -func Test_ARANGODB_Set_Expiration(t *testing.T) { +func Test_ArangoDB_Set_Expiration(t *testing.T) { var ( key = "john" val = []byte("doe") @@ -61,7 +61,7 @@ func Test_ARANGODB_Set_Expiration(t *testing.T) { time.Sleep(1100 * time.Millisecond) } -func Test_ARANGODB_Get_Expired(t *testing.T) { +func Test_ArangoDB_Get_Expired(t *testing.T) { key := "john" result, err := testStore.Get(key) @@ -69,13 +69,13 @@ func Test_ARANGODB_Get_Expired(t *testing.T) { require.Zero(t, len(result)) } -func Test_ARANGODB_Get_NotExist(t *testing.T) { +func Test_ArangoDB_Get_NotExist(t *testing.T) { result, err := testStore.Get("notexist") require.NoError(t, err) require.Zero(t, len(result)) } -func Test_ARANGODB_Delete(t *testing.T) { +func Test_ArangoDB_Delete(t *testing.T) { var ( key = "john" val = []byte("doe") @@ -92,7 +92,7 @@ func Test_ARANGODB_Delete(t *testing.T) { require.Zero(t, len(result)) } -func Test_ARANGODB_Reset(t *testing.T) { +func Test_ArangoDB_Reset(t *testing.T) { val := []byte("doe") err := testStore.Set("john1", val, 0) @@ -113,7 +113,7 @@ func Test_ARANGODB_Reset(t *testing.T) { require.Zero(t, len(result)) } -func Test_ARANGODB_Non_UTF8(t *testing.T) { +func Test_ArangoDB_Non_UTF8(t *testing.T) { val := []byte("0xF5") err := testStore.Set("0xF6", val, 0) @@ -124,10 +124,50 @@ func Test_ARANGODB_Non_UTF8(t *testing.T) { require.Equal(t, val, result) } -func Test_ARANGODB_Close(t *testing.T) { +func Test_ArangoDB_Close(t *testing.T) { require.Nil(t, testStore.Close()) } -func Test_ARANGODB_Conn(t *testing.T) { +func Test_ArangoDB_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_ArangoDB_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_ArangoDB_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_ArangoDB_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/azureblob/azureblob_test.go b/azureblob/azureblob_test.go index b1f92a0e..9138cb6c 100644 --- a/azureblob/azureblob_test.go +++ b/azureblob/azureblob_test.go @@ -135,3 +135,47 @@ func Test_AzureBlob_Close(t *testing.T) { testStore := newStore() require.Nil(t, testStore.Close()) } + +func Benchmark_AzureBlob_Set(b *testing.B) { + testStore := newStore() + + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_AzureBlob_Get(b *testing.B) { + testStore := newStore() + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_AzureBlob_Delete(b *testing.B) { + testStore := newStore() + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/badger/badger_test.go b/badger/badger_test.go index 267ab4e5..8d06861f 100644 --- a/badger/badger_test.go +++ b/badger/badger_test.go @@ -118,3 +118,43 @@ func Test_Badger_Close(t *testing.T) { func Test_Badger_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_Badger_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Badger_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Badger_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/bbolt/bbolt_test.go b/bbolt/bbolt_test.go index 3a776d95..8a66027a 100644 --- a/bbolt/bbolt_test.go +++ b/bbolt/bbolt_test.go @@ -96,3 +96,43 @@ func Test_Bbolt_Close(t *testing.T) { func Test_Bbolt_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_Bbolt_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Bbolt_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Bbolt_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/coherence/coherence_test.go b/coherence/coherence_test.go index 666aba81..aa8a24b0 100644 --- a/coherence/coherence_test.go +++ b/coherence/coherence_test.go @@ -17,6 +17,18 @@ var ( value2 = []byte("value2") ) +// newTestStore returns a new Coherence Store and ensures it is reset. +func newTestStore(t testing.TB, config ...Config) (*Storage, error) { + t.Helper() + + testStore, err := New(config...) + utils.AssertEqual(t, err, nil) + + err = testStore.Reset() + + return testStore, err +} + func Test_Coherence_Set_And_Get(t *testing.T) { var val []byte @@ -196,12 +208,50 @@ func Test_Coherence_With_Scope(t *testing.T) { utils.AssertEqual(t, testStore2.Close(), nil) } -// newTestStore returns a new Coherence Store and ensures it is reset. -func newTestStore(t *testing.T, config ...Config) (*Storage, error) { - testStore, err := New(config...) - utils.AssertEqual(t, err, nil) +func Benchmark_Coherence_Set(b *testing.B) { + testStore, err := newTestStore(b) + utils.AssertEqual(b, nil, err) - err = testStore.Reset() + b.ReportAllocs() + b.ResetTimer() - return testStore, err + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + utils.AssertEqual(b, nil, err) +} + +func Benchmark_Coherence_Get(b *testing.B) { + testStore, err := newTestStore(b) + utils.AssertEqual(b, nil, err) + + err = testStore.Set("john", []byte("doe"), 0) + utils.AssertEqual(b, nil, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + utils.AssertEqual(b, nil, err) +} + +func Benchmark_Coherence_Delete(b *testing.B) { + testStore, err := newTestStore(b) + utils.AssertEqual(b, nil, err) + + err = testStore.Set("john", []byte("doe"), 0) + utils.AssertEqual(b, nil, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + utils.AssertEqual(b, nil, err) } diff --git a/couchbase/couchbase_test.go b/couchbase/couchbase_test.go index 40d02842..3877ce1c 100644 --- a/couchbase/couchbase_test.go +++ b/couchbase/couchbase_test.go @@ -127,3 +127,64 @@ func TestGetConn_ReturnsNotNill(t *testing.T) { }) require.True(t, testStorage.Conn() != nil) } + +func Benchmark_Couchbase_Set(b *testing.B) { + testStore := New(Config{ + Username: "admin", + Password: "123456", + Host: "127.0.0.1:8091", + Bucket: "fiber_storage", + }) + + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Couchbase_Get(b *testing.B) { + testStore := New(Config{ + Username: "admin", + Password: "123456", + Host: "127.0.0.1:8091", + Bucket: "fiber_storage", + }) + + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Couchbase_Delete(b *testing.B) { + testStore := New(Config{ + Username: "admin", + Password: "123456", + Host: "127.0.0.1:8091", + Bucket: "fiber_storage", + }) + + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/etcd/etcd_test.go b/etcd/etcd_test.go index d747dd30..1ca5b5e0 100644 --- a/etcd/etcd_test.go +++ b/etcd/etcd_test.go @@ -108,3 +108,55 @@ func TestGetConn_ReturnsNotNill(t *testing.T) { require.True(t, testStorage.Conn() != nil) } + +func Benchmark_Etcd_Set(b *testing.B) { + testStore := New(Config{ + Endpoints: []string{"localhost:2379"}, + }) + + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Etcd_Get(b *testing.B) { + testStore := New(Config{ + Endpoints: []string{"localhost:2379"}, + }) + + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Etcd_Delete(b *testing.B) { + testStore := New(Config{ + Endpoints: []string{"localhost:2379"}, + }) + + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index 98b88e96..08035b1f 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -118,3 +118,43 @@ func Test_Memcache_Close(t *testing.T) { func Test_Memcache_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_Memcache_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Memcache_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Memcache_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/memory/memory_test.go b/memory/memory_test.go index 64e59124..bcb100e1 100644 --- a/memory/memory_test.go +++ b/memory/memory_test.go @@ -147,3 +147,43 @@ func Benchmark_Storage_Memory(b *testing.B) { } }) } + +func Benchmark_Memory_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Memory_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Memory_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/minio/minio_test.go b/minio/minio_test.go index 07fb0724..20848c06 100644 --- a/minio/minio_test.go +++ b/minio/minio_test.go @@ -182,3 +182,43 @@ func Test_Reset(t *testing.T) { func Test_Close(t *testing.T) { require.NoError(t, testStore.Close()) } + +func Benchmark_Minio_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Minio_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Minio_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/mongodb/mongodb_test.go b/mongodb/mongodb_test.go index 0bfb28c2..5b7027e6 100644 --- a/mongodb/mongodb_test.go +++ b/mongodb/mongodb_test.go @@ -120,3 +120,43 @@ func Test_MongoDB_Close(t *testing.T) { func Test_MongoDB_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_MongoDB_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_MongoDB_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_MongoDB_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/mssql/mssql_test.go b/mssql/mssql_test.go index b1c86588..6cdf1783 100644 --- a/mssql/mssql_test.go +++ b/mssql/mssql_test.go @@ -173,3 +173,43 @@ func Test_MSSQL_Close(t *testing.T) { func Test_MSSQL_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_MSSQL_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_MSSQL_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_MSSQL_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index 003ec9f1..57932fb5 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_test.go @@ -189,3 +189,43 @@ func Test_MYSQL_Close(t *testing.T) { func Test_MYSQL_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_MYSQL_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_MYSQL_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_MYSQL_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/pebble/pebble_test.go b/pebble/pebble_test.go index 5397d050..d17ac708 100644 --- a/pebble/pebble_test.go +++ b/pebble/pebble_test.go @@ -105,3 +105,43 @@ func Test_Pebble_Close(t *testing.T) { func Test_Pebble_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_Pebble_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Pebble_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Pebble_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/postgres/postgres_test.go b/postgres/postgres_test.go index 3ad85a1c..6057e721 100644 --- a/postgres/postgres_test.go +++ b/postgres/postgres_test.go @@ -170,3 +170,43 @@ func Test_Postgres_Conn(t *testing.T) { func Test_Postgres_Close(t *testing.T) { require.Nil(t, testStore.Close()) } + +func Benchmark_Postgres_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Postgres_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Postgres_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/redis/redis_test.go b/redis/redis_test.go index 0c2d2488..9ecbb92d 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -414,3 +414,43 @@ func Test_Redis_Cluster(t *testing.T) { require.Nil(t, testStoreUniversal.Close()) } + +func Benchmark_Redis_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Redis_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Redis_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/ristretto/ristretto_test.go b/ristretto/ristretto_test.go index 94df2917..b1a6fc08 100644 --- a/ristretto/ristretto_test.go +++ b/ristretto/ristretto_test.go @@ -152,3 +152,43 @@ func Test_Ristretto_Close(t *testing.T) { func Test_Ristretto_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_Ristretto_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Ristretto_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Ristretto_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/rueidis/rueidis_test.go b/rueidis/rueidis_test.go index 933424cb..b78cd936 100644 --- a/rueidis/rueidis_test.go +++ b/rueidis/rueidis_test.go @@ -237,3 +237,43 @@ func Test_Rueidis_Cluster(t *testing.T) { require.NoError(t, err) require.Nil(t, store.Close()) } + +func Benchmark_Rueidis_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_Rueidis_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_Rueidis_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/s3/s3_test.go b/s3/s3_test.go index 3a3e49e5..e8b0977c 100644 --- a/s3/s3_test.go +++ b/s3/s3_test.go @@ -94,3 +94,43 @@ func Test_S3_Close(t *testing.T) { func Test_S3_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_S3_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_S3_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_S3_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/sqlite3/sqlite3_test.go b/sqlite3/sqlite3_test.go index 08efce1e..6d790628 100644 --- a/sqlite3/sqlite3_test.go +++ b/sqlite3/sqlite3_test.go @@ -155,3 +155,43 @@ func Test_SQLite3_Close(t *testing.T) { func Test_SQLite3_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_SQLite3_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_SQLite3_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_SQLite3_Delete(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err = testStore.Delete("john") + } + + require.NoError(b, err) +} From d17795a9a9dbfb17ea0c067e070389d8dcae791b Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Fri, 15 Sep 2023 00:05:56 +0300 Subject: [PATCH 2/7] add benchmark workflow --- .github/workflows/benchmark.yml | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 .github/workflows/benchmark.yml diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 00000000..6cb09658 --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,59 @@ +on: + push: + branches: + - master + - main + paths: + - "**" + - "!**.md" + pull_request: + paths: + - "**" + - "!**.md" + +permissions: + deployments: write + contents: write + +name: Benchmark +jobs: + Compare: + runs-on: ubuntu-latest + steps: + - name: Fetch Repository + uses: actions/checkout@v4 + + - name: Install Go + uses: actions/setup-go@v4 + with: + # NOTE: Keep this in sync with the version from go.mod + go-version: "1.20.x" + + - name: Run Benchmarks + run: | + set -o pipefail + for d in */ ; do + cd "$d" + go test ./... -benchmem -run=^$ -bench . | tee -a ../output.txt + cd .. + done + shell: bash + + - name: Get Previous Benchmark Results + uses: actions/cache@v3 + with: + path: ./cache + key: ${{ runner.os }}-benchmark + + - name: Save Benchmark Results + uses: benchmark-action/github-action-benchmark@v1.18.0 + with: + tool: "go" + output-file-path: output.txt + github-token: ${{ secrets.GITHUB_TOKEN }} + benchmark-data-dir-path: "benchmarks" + fail-on-alert: true + comment-on-alert: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }} + #summary-always: ${{ github.event_name != 'push' && github.event_name != 'workflow_dispatch' }} + auto-push: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }} + save-data-file: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }} \ No newline at end of file From 55eca1c7b2f7782088943c8c03d250c38cb80bd4 Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Fri, 15 Sep 2023 00:19:11 +0300 Subject: [PATCH 3/7] use testify on coherence storage --- coherence/coherence_test.go | 135 ++++++++++++++++++------------------ coherence/go.mod | 14 +++- coherence/go.sum | 16 ++++- 3 files changed, 92 insertions(+), 73 deletions(-) diff --git a/coherence/coherence_test.go b/coherence/coherence_test.go index aa8a24b0..49b30c23 100644 --- a/coherence/coherence_test.go +++ b/coherence/coherence_test.go @@ -4,7 +4,7 @@ package coherence * Copyright © 2023, Oracle and/or its affiliates. */ import ( - "github.com/gofiber/utils" + "github.com/stretchr/testify/require" "testing" "time" ) @@ -22,7 +22,7 @@ func newTestStore(t testing.TB, config ...Config) (*Storage, error) { t.Helper() testStore, err := New(config...) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Reset() @@ -33,148 +33,147 @@ func Test_Coherence_Set_And_Get(t *testing.T) { var val []byte testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, value1, val) + require.NoError(t, err) + require.Equal(t, value1, val) - utils.AssertEqual(t, true, testStore.Conn() != nil) - - utils.AssertEqual(t, testStore.Close(), nil) + require.NotNil(t, testStore.Conn()) + require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Set_Override(t *testing.T) { var val []byte testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Set(key1, value2, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, value2, val) + require.NoError(t, err) + require.Equal(t, value2, val) - utils.AssertEqual(t, testStore.Close(), nil) + require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Set_With_Reset(t *testing.T) { var val []byte testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, value1, val) + require.NoError(t, err) + require.Equal(t, value1, val) // get a new store but reset it, so the subsequent Get will return nil testStore, err = newTestStore(t, Config{Reset: true}) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, true, len(val) == 0) + require.NoError(t, err) + require.True(t, len(val) == 0) - utils.AssertEqual(t, testStore.Close(), nil) + require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Set_With_Expiry(t *testing.T) { var val []byte testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) // set with an expiry of 5 seconds err = testStore.Set(key1, value1, time.Duration(5)*time.Second) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) time.Sleep(time.Duration(6) * time.Second) val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, true, len(val) == 0) + require.NoError(t, err) + require.True(t, len(val) == 0) - utils.AssertEqual(t, testStore.Close(), nil) + require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Get_Missing(t *testing.T) { var val []byte testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) val, err = testStore.Get(missingKey) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, true, len(val) == 0) + require.NoError(t, err) + require.True(t, len(val) == 0) - utils.AssertEqual(t, testStore.Close(), nil) + require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Reset(t *testing.T) { var val []byte testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Set(key2, value2, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) // check the keys exist val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, value1, val) + require.NoError(t, err) + require.Equal(t, value1, val) val, err = testStore.Get(key2) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, value2, val) + require.NoError(t, err) + require.Equal(t, value2, val) // reset the store, this should remove both entries err = testStore.Reset() // check the keys have expired val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, true, len(val) == 0) + require.NoError(t, err) + require.True(t, len(val) == 0) val, err = testStore.Get(key2) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, true, len(val) == 0) + require.NoError(t, err) + require.True(t, len(val) == 0) - utils.AssertEqual(t, testStore.Close(), nil) + require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Set_And_Delete(t *testing.T) { var val []byte testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore.Delete(key1) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) // ensure the key has gone val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, true, len(val) == 0) + require.NoError(t, err) + require.True(t, len(val) == 0) - utils.AssertEqual(t, testStore.Close(), nil) + require.Equal(t, nil, testStore.Close()) } // TestCoherenceWithScope ensures we can create multiple session stores with multiple scopes. @@ -183,34 +182,34 @@ func Test_Coherence_With_Scope(t *testing.T) { // create two session stores with different scopes testStore1, err := newTestStore(t, Config{ScopeName: "scope1"}) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) testStore2, err := newTestStore(t, Config{ScopeName: "scope2"}) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) // ensure we can put the same key with different values in each scope err = testStore1.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) err = testStore2.Set(key1, value2, 0) - utils.AssertEqual(t, err, nil) + require.NoError(t, err) // ensure the value of "key1" is different for each store val, err = testStore1.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, value1, val) + require.NoError(t, err) + require.Equal(t, value1, val) val, err = testStore2.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, value2, val) + require.NoError(t, err) + require.Equal(t, value2, val) - utils.AssertEqual(t, testStore1.Close(), nil) - utils.AssertEqual(t, testStore2.Close(), nil) + require.NoError(t, testStore1.Close()) + require.NoError(t, testStore2.Close()) } func Benchmark_Coherence_Set(b *testing.B) { testStore, err := newTestStore(b) - utils.AssertEqual(b, nil, err) + require.NoError(b, err) b.ReportAllocs() b.ResetTimer() @@ -219,15 +218,15 @@ func Benchmark_Coherence_Set(b *testing.B) { err = testStore.Set("john", []byte("doe"), 0) } - utils.AssertEqual(b, nil, err) + require.NoError(b, err) } func Benchmark_Coherence_Get(b *testing.B) { testStore, err := newTestStore(b) - utils.AssertEqual(b, nil, err) + require.NoError(b, err) err = testStore.Set("john", []byte("doe"), 0) - utils.AssertEqual(b, nil, err) + require.NoError(b, err) b.ReportAllocs() b.ResetTimer() @@ -236,15 +235,15 @@ func Benchmark_Coherence_Get(b *testing.B) { _, err = testStore.Get("john") } - utils.AssertEqual(b, nil, err) + require.NoError(b, err) } func Benchmark_Coherence_Delete(b *testing.B) { testStore, err := newTestStore(b) - utils.AssertEqual(b, nil, err) + require.NoError(b, err) err = testStore.Set("john", []byte("doe"), 0) - utils.AssertEqual(b, nil, err) + require.NoError(b, err) b.ReportAllocs() b.ResetTimer() @@ -253,5 +252,5 @@ func Benchmark_Coherence_Delete(b *testing.B) { err = testStore.Delete("john") } - utils.AssertEqual(b, nil, err) + require.NoError(b, err) } diff --git a/coherence/go.mod b/coherence/go.mod index af574f28..8a2e7ef3 100644 --- a/coherence/go.mod +++ b/coherence/go.mod @@ -2,12 +2,20 @@ module github.com/gofiber/storage/coherence go 1.19 -require github.com/oracle/coherence-go-client v1.0.1 +require ( + github.com/oracle/coherence-go-client v1.0.1 + github.com/stretchr/testify v1.8.4 +) -require google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) require ( - github.com/gofiber/utils v1.1.0 github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.3.1 // indirect golang.org/x/net v0.14.0 // indirect diff --git a/coherence/go.sum b/coherence/go.sum index 055f2504..7e9ad78c 100644 --- a/coherence/go.sum +++ b/coherence/go.sum @@ -1,5 +1,6 @@ -github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM= -github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= @@ -7,9 +8,16 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/oracle/coherence-go-client v1.0.1 h1:MJ9S46+G8DHg/b3c/XB+mGA58wmTE0HdBCH1t+WrIBs= github.com/oracle/coherence-go-client v1.0.1/go.mod h1:tdIKuX6brsdTCARlbxeRYiOF0TA4tA8ujHO4BCS1mjk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= @@ -25,4 +33,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 36dd674d93fe1004f81cbb1eab38182979e03c29 Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Fri, 15 Sep 2023 10:58:04 +0300 Subject: [PATCH 4/7] add TestMain for a few storages and update benchmark workflow --- .github/workflows/benchmark.yml | 105 ++++++++++++++++++++++++++++++ .github/workflows/test-s3.yml | 2 +- azureblob/azureblob_test.go | 25 ++++--- badger/badger_test.go | 4 +- bbolt/bbolt_test.go | 15 ++++- bbolt/utils.go | 8 ++- coherence/coherence_test.go | 79 ++++++++-------------- couchbase/couchbase_test.go | 112 ++++++++++---------------------- dynamodb/dynamodb_test.go | 17 +++-- etcd/etcd_test.go | 83 ++++++++--------------- memcache/memcache_test.go | 14 +++- minio/minio_test.go | 29 ++++++--- mongodb/mongodb_test.go | 16 ++++- mysql/mysql_test.go | 2 +- pebble/pebble_test.go | 4 +- ristretto/ristretto_test.go | 13 +++- s3/init_test.go | 4 +- 17 files changed, 305 insertions(+), 227 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 6cb09658..69b66340 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -19,6 +19,64 @@ name: Benchmark jobs: Compare: runs-on: ubuntu-latest + services: + arangodb: + image: 'arangodb:latest' + env: + ARANGO_NO_AUTH: 1 + ports: + - '8529:8529' + dynamodb: + image: 'amazon/dynamodb-local:latest' + ports: + - '8000:8000' + memcached: + image: 'memcached:latest' + ports: + - '11211:11211' + mongo: + image: 'mongo:latest' + ports: + - '27017:27017' + mssql: + image: 'mcmoe/mssqldocker:latest' + ports: + - '1433:1433' + env: + ACCEPT_EULA: Y + SA_PASSWORD: MsSql!1234 + MSSQL_DB: master + MSSQL_USER: sa + MSSQL_PASSWORD: MsSql!1234 + options: >- + --health-cmd "/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD -Q 'select 1' -b -o /dev/null" + --health-interval 1s + --health-timeout 30s + --health-start-period 10s + --health-retries 20 + mysql: + image: 'mysql:latest' + env: + MYSQL_DATABASE: fiber + MYSQL_USER: username + MYSQL_PASSWORD: password + MYSQL_ROOT_PASSWORD: password + ports: + - '3306:3306' + options: >- + --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout + 5s --health-retries 5 + postgres: + image: 'postgres:latest' + ports: + - '5432:5432' + env: + POSTGRES_DB: fiber + POSTGRES_USER: username + POSTGRES_PASSWORD: "pass#w%rd" + options: >- + --health-cmd pg_isready --health-interval 10s --health-timeout 5s + --health-retries 5 steps: - name: Fetch Repository uses: actions/checkout@v4 @@ -29,6 +87,53 @@ jobs: # NOTE: Keep this in sync with the version from go.mod go-version: "1.20.x" + - name: Install Azurite + run: | + docker run -d -p 10000:10000 mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0 --blobPort 10000 + - name: Install Coherence + run: | + docker run -d -p 1408:1408 -p 30000:30000 ghcr.io/oracle/coherence-ce:22.06.5 + sleep 30 + - name: Install couchbase + run: | + docker run --name couchbase -d -p 8091-8097:8091-8097 -p 9123:9123 -p 11207:11207 -p 11210:11210 -p 11280:11280 -p 18091-18097:18091-18097 couchbase:enterprise-7.1.1 + sleep 10 + docker exec --tty couchbase couchbase-cli cluster-init -c localhost:8091 --cluster-username admin --cluster-password 123456 --cluster-ramsize 256 --services data + sleep 10 + docker exec --tty couchbase couchbase-cli bucket-create -c localhost:8091 --username admin --password 123456 --bucket fiber_storage --bucket-type couchbase --bucket-ramsize 100 --enable-flush 1 + - name: Install etcd + run: | + docker run -d --name Etcd-server \ + --publish 2379:2379 \ + --publish 2380:2380 \ + --env ALLOW_NONE_AUTHENTICATION=yes \ + --env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 \ + bitnami/etcd:latest + - name: Install MinIO + run: | + docker run -d --restart always -p 9000:9000 --name storage-minio -e MINIO_ROOT_USER='minio-user' -e MINIO_ROOT_PASSWORD='minio-password' minio/minio server /data + - name: Generate TLS certs + run: ./.github/scripts/gen-test-certs.sh + + - name: Add Custom CA cert + run: sudo cp /home/runner/work/storage/storage/tls/ca.crt /usr/local/share/ca-certificates/custom.crt + + - name: Trust Custom CA Cert + run: sudo update-ca-certificates + + - name: Setup Redis + uses: shogo82148/actions-setup-redis@v1 + with: + redis-version: '7.x' + auto-start: 'false' + + - name: Run Redis + run: | + redis-server --tls-port 6380 --port 6379 \ + --tls-cert-file /home/runner/work/storage/storage/tls/redis.crt \ + --tls-key-file /home/runner/work/storage/storage/tls/redis.key \ + --tls-ca-cert-file /home/runner/work/storage/storage/tls/ca.crt & + - name: Run Benchmarks run: | set -o pipefail diff --git a/.github/workflows/test-s3.yml b/.github/workflows/test-s3.yml index 64c68fad..23ff9ce1 100644 --- a/.github/workflows/test-s3.yml +++ b/.github/workflows/test-s3.yml @@ -20,7 +20,7 @@ jobs: - 1.21.x steps: - name: Install MinIO - run: docker run -d -p 9000:9000 --name minio minio/minio server /data + run: docker run -d --restart always -p 9000:9000 --name storage-minio -e MINIO_ROOT_USER='minio-user' -e MINIO_ROOT_PASSWORD='minio-password' minio/minio server /data - name: Fetch Repository uses: actions/checkout@v4 - name: Install Go diff --git a/azureblob/azureblob_test.go b/azureblob/azureblob_test.go index 9138cb6c..59da61b9 100644 --- a/azureblob/azureblob_test.go +++ b/azureblob/azureblob_test.go @@ -1,14 +1,17 @@ package azureblob import ( + "os" "testing" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror" "github.com/stretchr/testify/require" ) -func newStore() *Storage { - return New(Config{ +var testStore *Storage + +func TestMain(m *testing.M) { + testStore = New(Config{ Account: "devstoreaccount1", Container: "test", Endpoint: "http://127.0.0.1:10000/devstoreaccount1", @@ -16,7 +19,13 @@ func newStore() *Storage { Account: "devstoreaccount1", Key: "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==", }, + Reset: true, }) + + code := m.Run() + + _ = testStore.Close() + os.Exit(code) } func Test_AzureBlob_Get(t *testing.T) { @@ -24,7 +33,6 @@ func Test_AzureBlob_Get(t *testing.T) { key = "john" val = []byte("doe") ) - testStore := newStore() err := testStore.Set(key, val, 0) require.NoError(t, err) @@ -40,7 +48,6 @@ func Test_AzureBlob_Set(t *testing.T) { val = []byte("doe") ) - testStore := newStore() err := testStore.Set(key, val, 0) require.NoError(t, err) } @@ -50,7 +57,6 @@ func Test_AzureBlob_Delete(t *testing.T) { key = "john" val = []byte("doe") ) - testStore := newStore() err := testStore.Set(key, val, 0) require.NoError(t, err) @@ -73,7 +79,6 @@ func Test_AzureBlob_Override(t *testing.T) { key = "john" val = []byte("doe") ) - testStore := newStore() err := testStore.Set(key, val, 0) require.NoError(t, err) @@ -83,7 +88,6 @@ func Test_AzureBlob_Override(t *testing.T) { } func Test_AzureBlob_Get_NotExist(t *testing.T) { - testStore := newStore() result, err := testStore.Get("notexist") if err != nil { if bloberror.HasCode(err, bloberror.BlobNotFound) { @@ -96,7 +100,6 @@ func Test_AzureBlob_Get_NotExist(t *testing.T) { func Test_AzureBlob_Reset(t *testing.T) { val := []byte("doe") - testStore := newStore() err := testStore.Set("john1", val, 0) require.NoError(t, err) @@ -127,18 +130,14 @@ func Test_AzureBlob_Reset(t *testing.T) { } func Test_S3_Conn(t *testing.T) { - testStore := newStore() require.True(t, testStore.Conn() != nil) } func Test_AzureBlob_Close(t *testing.T) { - testStore := newStore() require.Nil(t, testStore.Close()) } func Benchmark_AzureBlob_Set(b *testing.B) { - testStore := newStore() - b.ReportAllocs() b.ResetTimer() @@ -151,7 +150,6 @@ func Benchmark_AzureBlob_Set(b *testing.B) { } func Benchmark_AzureBlob_Get(b *testing.B) { - testStore := newStore() err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) @@ -166,7 +164,6 @@ func Benchmark_AzureBlob_Get(b *testing.B) { } func Benchmark_AzureBlob_Delete(b *testing.B) { - testStore := newStore() err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) diff --git a/badger/badger_test.go b/badger/badger_test.go index 8d06861f..2f471a98 100644 --- a/badger/badger_test.go +++ b/badger/badger_test.go @@ -7,7 +7,9 @@ import ( "github.com/stretchr/testify/require" ) -var testStore = New() +var testStore = New(Config{ + Reset: true, +}) func Test_Badger_Set(t *testing.T) { var ( diff --git a/bbolt/bbolt_test.go b/bbolt/bbolt_test.go index 8a66027a..7ac196af 100644 --- a/bbolt/bbolt_test.go +++ b/bbolt/bbolt_test.go @@ -1,12 +1,25 @@ package bbolt import ( + "os" "testing" "github.com/stretchr/testify/require" ) -var testStore = New() +var testStore *Storage + +func TestMain(m *testing.M) { + testStore = New(Config{ + Bucket: "fiber-bucket", + Reset: true, + }) + + code := m.Run() + + _ = testStore.Close() + os.Exit(code) +} func Test_Bbolt_Set(t *testing.T) { var ( diff --git a/bbolt/utils.go b/bbolt/utils.go index d731e529..e3603e33 100644 --- a/bbolt/utils.go +++ b/bbolt/utils.go @@ -1,6 +1,7 @@ package bbolt import ( + "errors" "github.com/gofiber/utils/v2" "go.etcd.io/bbolt" ) @@ -15,6 +16,11 @@ func createBucket(cfg Config, conn *bbolt.DB) error { func removeBucket(cfg Config, conn *bbolt.DB) error { return conn.Update(func(tx *bbolt.Tx) error { - return tx.DeleteBucket(utils.UnsafeBytes(cfg.Bucket)) + err := tx.DeleteBucket(utils.UnsafeBytes(cfg.Bucket)) + if errors.Is(err, bbolt.ErrBucketNotFound) { + return nil + } + + return err }) } diff --git a/coherence/coherence_test.go b/coherence/coherence_test.go index 49b30c23..b093b3a5 100644 --- a/coherence/coherence_test.go +++ b/coherence/coherence_test.go @@ -5,6 +5,7 @@ package coherence */ import ( "github.com/stretchr/testify/require" + "os" "testing" "time" ) @@ -17,6 +18,19 @@ var ( value2 = []byte("value2") ) +var testStore *Storage + +func TestMain(m *testing.M) { + testStore, _ = New(Config{ + Reset: true, + }) + + code := m.Run() + + _ = testStore.Close() + os.Exit(code) +} + // newTestStore returns a new Coherence Store and ensures it is reset. func newTestStore(t testing.TB, config ...Config) (*Storage, error) { t.Helper() @@ -32,10 +46,7 @@ func newTestStore(t testing.TB, config ...Config) (*Storage, error) { func Test_Coherence_Set_And_Get(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - require.NoError(t, err) - - err = testStore.Set(key1, value1, 0) + err := testStore.Set(key1, value1, 0) require.NoError(t, err) val, err = testStore.Get(key1) @@ -43,16 +54,12 @@ func Test_Coherence_Set_And_Get(t *testing.T) { require.Equal(t, value1, val) require.NotNil(t, testStore.Conn()) - require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Set_Override(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - require.NoError(t, err) - - err = testStore.Set(key1, value1, 0) + err := testStore.Set(key1, value1, 0) require.NoError(t, err) err = testStore.Set(key1, value2, 0) @@ -61,17 +68,12 @@ func Test_Coherence_Set_Override(t *testing.T) { val, err = testStore.Get(key1) require.NoError(t, err) require.Equal(t, value2, val) - - require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Set_With_Reset(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - require.NoError(t, err) - - err = testStore.Set(key1, value1, 0) + err := testStore.Set(key1, value1, 0) require.NoError(t, err) val, err = testStore.Get(key1) @@ -79,24 +81,21 @@ func Test_Coherence_Set_With_Reset(t *testing.T) { require.Equal(t, value1, val) // get a new store but reset it, so the subsequent Get will return nil - testStore, err = newTestStore(t, Config{Reset: true}) + testStore2, err := newTestStore(t, Config{Reset: true}) require.NoError(t, err) - val, err = testStore.Get(key1) + val, err = testStore2.Get(key1) require.NoError(t, err) require.True(t, len(val) == 0) - require.Equal(t, nil, testStore.Close()) + require.Equal(t, nil, testStore2.Close()) } func Test_Coherence_Set_With_Expiry(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - require.NoError(t, err) - // set with an expiry of 5 seconds - err = testStore.Set(key1, value1, time.Duration(5)*time.Second) + err := testStore.Set(key1, value1, time.Duration(5)*time.Second) require.NoError(t, err) time.Sleep(time.Duration(6) * time.Second) @@ -104,29 +103,20 @@ func Test_Coherence_Set_With_Expiry(t *testing.T) { require.NoError(t, err) require.True(t, len(val) == 0) - require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Get_Missing(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - require.NoError(t, err) - - val, err = testStore.Get(missingKey) + val, err := testStore.Get(missingKey) require.NoError(t, err) require.True(t, len(val) == 0) - - require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Reset(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - require.NoError(t, err) - - err = testStore.Set(key1, value1, 0) + err := testStore.Set(key1, value1, 0) require.NoError(t, err) err = testStore.Set(key2, value2, 0) @@ -152,17 +142,12 @@ func Test_Coherence_Reset(t *testing.T) { val, err = testStore.Get(key2) require.NoError(t, err) require.True(t, len(val) == 0) - - require.Equal(t, nil, testStore.Close()) } func Test_Coherence_Set_And_Delete(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - require.NoError(t, err) - - err = testStore.Set(key1, value1, 0) + err := testStore.Set(key1, value1, 0) require.NoError(t, err) err = testStore.Delete(key1) @@ -172,8 +157,6 @@ func Test_Coherence_Set_And_Delete(t *testing.T) { val, err = testStore.Get(key1) require.NoError(t, err) require.True(t, len(val) == 0) - - require.Equal(t, nil, testStore.Close()) } // TestCoherenceWithScope ensures we can create multiple session stores with multiple scopes. @@ -208,12 +191,10 @@ func Test_Coherence_With_Scope(t *testing.T) { } func Benchmark_Coherence_Set(b *testing.B) { - testStore, err := newTestStore(b) - require.NoError(b, err) - b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { err = testStore.Set("john", []byte("doe"), 0) } @@ -222,10 +203,7 @@ func Benchmark_Coherence_Set(b *testing.B) { } func Benchmark_Coherence_Get(b *testing.B) { - testStore, err := newTestStore(b) - require.NoError(b, err) - - err = testStore.Set("john", []byte("doe"), 0) + err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) b.ReportAllocs() @@ -239,10 +217,7 @@ func Benchmark_Coherence_Get(b *testing.B) { } func Benchmark_Coherence_Delete(b *testing.B) { - testStore, err := newTestStore(b) - require.NoError(b, err) - - err = testStore.Set("john", []byte("doe"), 0) + err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) b.ReportAllocs() diff --git a/couchbase/couchbase_test.go b/couchbase/couchbase_test.go index 3877ce1c..8af3728c 100644 --- a/couchbase/couchbase_test.go +++ b/couchbase/couchbase_test.go @@ -7,134 +7,100 @@ import ( "github.com/stretchr/testify/require" ) -func TestSetCouchbase_ShouldReturnNoError(t *testing.T) { - testStorage := New(Config{ +func newTestStore(t testing.TB) *Storage { + t.Helper() + return New(Config{ Username: "admin", Password: "123456", Host: "127.0.0.1:8091", Bucket: "fiber_storage", }) +} + +func TestSetCouchbase_ShouldReturnNoError(t *testing.T) { + testStore := newTestStore(t) - err := testStorage.Set("test", []byte("test"), 0) + err := testStore.Set("test", []byte("test"), 0) require.NoError(t, err) } func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) { - testStorage := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) + testStore := newTestStore(t) - val, err := testStorage.Get("not_found_key") + val, err := testStore.Get("not_found_key") require.NoError(t, err) require.Zero(t, len(val)) } -func TestSetAndGet_GetShouldReturn_SettedValueWithoutError(t *testing.T) { - testStorage := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) +func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) { + testStore := newTestStore(t) - err := testStorage.Set("test", []byte("fiber_test_value"), 0) + err := testStore.Set("test", []byte("fiber_test_value"), 0) require.NoError(t, err) - val, err := testStorage.Get("test") + val, err := testStore.Get("test") require.NoError(t, err) require.Equal(t, val, []byte("fiber_test_value")) } func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) { - testStorage := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) + testStore := newTestStore(t) - err := testStorage.Set("test", []byte("fiber_test_value"), 3*time.Second) + err := testStore.Set("test", []byte("fiber_test_value"), 3*time.Second) require.NoError(t, err) time.Sleep(6 * time.Second) - val, err := testStorage.Get("test") + val, err := testStore.Get("test") require.NoError(t, err) require.Zero(t, len(val)) } func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) { - testStorage := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) + testStore := newTestStore(t) - err := testStorage.Set("test", []byte("fiber_test_value"), 0) + err := testStore.Set("test", []byte("fiber_test_value"), 0) require.NoError(t, err) - err = testStorage.Delete("test") + err = testStore.Delete("test") require.NoError(t, err) - _, err = testStorage.Get("test") + _, err = testStore.Get("test") require.NoError(t, err) } func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) { - testStorage := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) + testStore := newTestStore(t) - err := testStorage.Set("test", []byte("fiber_test_value"), 0) + err := testStore.Set("test", []byte("fiber_test_value"), 0) require.NoError(t, err) - err = testStorage.Reset() + err = testStore.Reset() require.NoError(t, err) - _, err = testStorage.Get("test") + _, err = testStore.Get("test") require.NoError(t, err) } func TestClose_CloseShouldReturn_NoError(t *testing.T) { - testStorage := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) + testStore := newTestStore(t) - err := testStorage.Close() + err := testStore.Close() require.NoError(t, err) } -func TestGetConn_ReturnsNotNill(t *testing.T) { - testStorage := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) - require.True(t, testStorage.Conn() != nil) +func TestGetConn_ReturnsNotNil(t *testing.T) { + testStore := newTestStore(t) + + require.True(t, testStore.Conn() != nil) } func Benchmark_Couchbase_Set(b *testing.B) { - testStore := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) + testStore := newTestStore(b) b.ReportAllocs() b.ResetTimer() @@ -148,12 +114,7 @@ func Benchmark_Couchbase_Set(b *testing.B) { } func Benchmark_Couchbase_Get(b *testing.B) { - testStore := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) + testStore := newTestStore(b) err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) @@ -169,12 +130,7 @@ func Benchmark_Couchbase_Get(b *testing.B) { } func Benchmark_Couchbase_Delete(b *testing.B) { - testStore := New(Config{ - Username: "admin", - Password: "123456", - Host: "127.0.0.1:8091", - Bucket: "fiber_storage", - }) + testStore := newTestStore(b) err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) diff --git a/dynamodb/dynamodb_test.go b/dynamodb/dynamodb_test.go index 55c7c932..704b344b 100644 --- a/dynamodb/dynamodb_test.go +++ b/dynamodb/dynamodb_test.go @@ -1,13 +1,16 @@ package dynamodb import ( + "os" "testing" "github.com/stretchr/testify/require" ) -var testStore = New( - Config{ +var testStore *Storage + +func TestMain(m *testing.M) { + testStore = New(Config{ Table: "fiber_storage", Endpoint: "http://localhost:8000/", Region: "us-east-1", @@ -15,8 +18,14 @@ var testStore = New( AccessKey: "dummy", SecretAccessKey: "dummy", }, - }, -) + Reset: true, + }) + + code := m.Run() + + _ = testStore.Close() + os.Exit(code) +} func Test_DynamoDB_Set(t *testing.T) { var ( diff --git a/etcd/etcd_test.go b/etcd/etcd_test.go index 1ca5b5e0..bcbcf20e 100644 --- a/etcd/etcd_test.go +++ b/etcd/etcd_test.go @@ -1,119 +1,98 @@ package etcd import ( + "os" "testing" "time" "github.com/stretchr/testify/require" ) -func TestSetEtcd_ShouldReturnNoError(t *testing.T) { - testStorage := New(Config{ +var testStore *Storage + +func TestMain(m *testing.M) { + testStore = New(Config{ Endpoints: []string{"localhost:2379"}, }) + code := m.Run() + + _ = testStore.Reset() + _ = testStore.Close() + os.Exit(code) +} + +func TestSetEtcd_ShouldReturnNoError(t *testing.T) { var ( key = "john" val = []byte("doe") ) - err := testStorage.Set(key, val, 0) + err := testStore.Set(key, val, 0) require.NoError(t, err) } func TestGetEtcd_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) { - testStorage := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - - val, err := testStorage.Get("not_found_key") + val, err := testStore.Get("not_found_key") require.NoError(t, err) require.Zero(t, len(val)) } func TestSetAndGet_GetShouldReturn_SettedValueWithoutError(t *testing.T) { - testStorage := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - - err := testStorage.Set("test", []byte("fiber_test_value"), 0) + err := testStore.Set("test", []byte("fiber_test_value"), 0) require.NoError(t, err) - val, err := testStorage.Get("test") + val, err := testStore.Get("test") require.NoError(t, err) require.Equal(t, val, []byte("fiber_test_value")) } func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) { - testStorage := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - - err := testStorage.Set("test", []byte("fiber_test_value"), 3*time.Second) + err := testStore.Set("test", []byte("fiber_test_value"), 3*time.Second) require.NoError(t, err) time.Sleep(6 * time.Second) - val, err := testStorage.Get("test") + val, err := testStore.Get("test") require.NoError(t, err) require.Zero(t, len(val)) } func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) { - testStorage := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - - err := testStorage.Set("test", []byte("fiber_test_value"), 0) + err := testStore.Set("test", []byte("fiber_test_value"), 0) require.NoError(t, err) - err = testStorage.Delete("test") + err = testStore.Delete("test") require.NoError(t, err) - _, err = testStorage.Get("test") + _, err = testStore.Get("test") require.NoError(t, err) } func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) { - testStorage := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - - err := testStorage.Set("test", []byte("fiber_test_value"), 0) + err := testStore.Set("test", []byte("fiber_test_value"), 0) require.NoError(t, err) - err = testStorage.Reset() + err = testStore.Reset() require.NoError(t, err) - _, err = testStorage.Get("test") + _, err = testStore.Get("test") require.NoError(t, err) } func TestClose_CloseShouldReturn_NoError(t *testing.T) { - testStorage := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - - err := testStorage.Close() + err := testStore.Close() require.NoError(t, err) } func TestGetConn_ReturnsNotNill(t *testing.T) { - testStorage := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - - require.True(t, testStorage.Conn() != nil) + require.True(t, testStore.Conn() != nil) } func Benchmark_Etcd_Set(b *testing.B) { - testStore := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - b.ReportAllocs() b.ResetTimer() @@ -126,10 +105,6 @@ func Benchmark_Etcd_Set(b *testing.B) { } func Benchmark_Etcd_Get(b *testing.B) { - testStore := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) @@ -144,10 +119,6 @@ func Benchmark_Etcd_Get(b *testing.B) { } func Benchmark_Etcd_Delete(b *testing.B) { - testStore := New(Config{ - Endpoints: []string{"localhost:2379"}, - }) - err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index 08035b1f..5947b0c2 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -1,13 +1,25 @@ package memcache import ( + "os" "testing" "time" "github.com/stretchr/testify/require" ) -var testStore = New() +var testStore *Storage + +func TestMain(m *testing.M) { + testStore = New(Config{ + Reset: true, + }) + + code := m.Run() + + _ = testStore.Close() + os.Exit(code) +} func Test_Memcache_Set(t *testing.T) { var ( diff --git a/minio/minio_test.go b/minio/minio_test.go index 20848c06..683c985e 100644 --- a/minio/minio_test.go +++ b/minio/minio_test.go @@ -1,6 +1,7 @@ package minio import ( + "os" "strconv" "testing" "time" @@ -8,16 +9,26 @@ import ( "github.com/stretchr/testify/require" ) -var testStore = New( - Config{ - Bucket: "fiber-bucket", - Endpoint: "localhost:9000", - Credentials: Credentials{ - AccessKeyID: "minio-user", - SecretAccessKey: "minio-password", +var testStore *Storage + +func TestMain(m *testing.M) { + testStore = New( + Config{ + Bucket: "fiber-bucket", + Endpoint: "localhost:9000", + Credentials: Credentials{ + AccessKeyID: "minio-user", + SecretAccessKey: "minio-password", + }, + Reset: true, }, - }, -) + ) + + code := m.Run() + + _ = testStore.Close() + os.Exit(code) +} func Test_Get(t *testing.T) { var ( diff --git a/mongodb/mongodb_test.go b/mongodb/mongodb_test.go index 5b7027e6..36f3fe13 100644 --- a/mongodb/mongodb_test.go +++ b/mongodb/mongodb_test.go @@ -1,15 +1,25 @@ package mongodb import ( + "os" "testing" "time" "github.com/stretchr/testify/require" ) -var testStore = New(Config{ - Reset: true, -}) +var testStore *Storage + +func TestMain(m *testing.M) { + testStore = New(Config{ + Reset: true, + }) + + code := m.Run() + + _ = testStore.Close() + os.Exit(code) +} func Test_MongoDB_Set(t *testing.T) { var ( diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index 57932fb5..66a74c34 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_test.go @@ -26,7 +26,7 @@ func Test_MYSQL_New(t *testing.T) { }) require.True(t, newConfigStore.db != nil) - newConfigStore.Close() + require.NoError(t, newConfigStore.Close()) dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", os.Getenv("MYSQL_USERNAME"), os.Getenv("MYSQL_PASSWORD"), "127.0.0.1", 3306, os.Getenv("MYSQL_DATABASE")) newConfigStore = New(Config{ diff --git a/pebble/pebble_test.go b/pebble/pebble_test.go index d17ac708..505645ac 100644 --- a/pebble/pebble_test.go +++ b/pebble/pebble_test.go @@ -8,8 +8,8 @@ import ( ) var testStore = New(Config{ - "test.db", - nil, + Path: "test.db", + WriteOptions: nil, }) func Test_Pebble_Set(t *testing.T) { diff --git a/ristretto/ristretto_test.go b/ristretto/ristretto_test.go index b1a6fc08..c7bf4c5e 100644 --- a/ristretto/ristretto_test.go +++ b/ristretto/ristretto_test.go @@ -1,13 +1,24 @@ package ristretto import ( + "os" "testing" "time" "github.com/stretchr/testify/require" ) -var testStore = New() +var testStore *Storage + +func TestMain(m *testing.M) { + testStore = New() + + code := m.Run() + + _ = testStore.Reset() + _ = testStore.Close() + os.Exit(code) +} func Test_Ristretto_Set(t *testing.T) { var ( diff --git a/s3/init_test.go b/s3/init_test.go index 7d8d0c21..56b8cd2c 100644 --- a/s3/init_test.go +++ b/s3/init_test.go @@ -19,8 +19,8 @@ func TestMain(m *testing.M) { Endpoint: "http://127.0.0.1:9000/", Region: "us-east-1", Credentials: Credentials{ - AccessKey: "minioadmin", - SecretAccessKey: "minioadmin", + AccessKey: "minio-user", + SecretAccessKey: "minio-password", }, RequestTimeout: 3 * time.Second, }, From db1dd2bf6a8a4318eb0d17c863ae0738685ebc5d Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Fri, 15 Sep 2023 18:23:02 +0300 Subject: [PATCH 5/7] update delete benchmarks --- arangodb/arangodb_test.go | 7 +++---- azureblob/azureblob_test.go | 7 +++---- badger/badger_test.go | 5 ++--- bbolt/bbolt_test.go | 5 ++--- coherence/coherence_test.go | 7 +++---- couchbase/couchbase_test.go | 7 +++---- dynamodb/dynamodb_test.go | 39 +++++++++++++++++++++++++++++++++++++ etcd/etcd_test.go | 7 +++---- memcache/memcache_test.go | 7 +++---- memory/memory_test.go | 7 +++---- minio/minio_test.go | 7 +++---- mongodb/mongodb_test.go | 7 +++---- mssql/mssql_test.go | 7 +++---- mysql/mysql_test.go | 7 +++---- pebble/pebble_test.go | 7 +++---- postgres/postgres_test.go | 7 +++---- redis/redis_test.go | 25 +++++++++++------------- ristretto/ristretto_test.go | 9 ++++----- rueidis/rueidis_test.go | 16 +++++++-------- s3/s3_test.go | 7 +++---- sqlite3/sqlite3_test.go | 5 ++--- 21 files changed, 109 insertions(+), 93 deletions(-) diff --git a/arangodb/arangodb_test.go b/arangodb/arangodb_test.go index 9ce6ae67..ef0ef4f1 100644 --- a/arangodb/arangodb_test.go +++ b/arangodb/arangodb_test.go @@ -158,14 +158,13 @@ func Benchmark_ArangoDB_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_ArangoDB_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_ArangoDB_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/azureblob/azureblob_test.go b/azureblob/azureblob_test.go index 59da61b9..2b5b1902 100644 --- a/azureblob/azureblob_test.go +++ b/azureblob/azureblob_test.go @@ -163,14 +163,13 @@ func Benchmark_AzureBlob_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_AzureBlob_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_AzureBlob_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/badger/badger_test.go b/badger/badger_test.go index 2f471a98..3e90ae9e 100644 --- a/badger/badger_test.go +++ b/badger/badger_test.go @@ -148,13 +148,12 @@ func Benchmark_Badger_Get(b *testing.B) { } func Benchmark_Badger_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/bbolt/bbolt_test.go b/bbolt/bbolt_test.go index 7ac196af..e53918c9 100644 --- a/bbolt/bbolt_test.go +++ b/bbolt/bbolt_test.go @@ -137,13 +137,12 @@ func Benchmark_Bbolt_Get(b *testing.B) { } func Benchmark_Bbolt_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/coherence/coherence_test.go b/coherence/coherence_test.go index b093b3a5..009fecd2 100644 --- a/coherence/coherence_test.go +++ b/coherence/coherence_test.go @@ -216,14 +216,13 @@ func Benchmark_Coherence_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Coherence_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Coherence_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/couchbase/couchbase_test.go b/couchbase/couchbase_test.go index 8af3728c..fa8cc6b2 100644 --- a/couchbase/couchbase_test.go +++ b/couchbase/couchbase_test.go @@ -129,16 +129,15 @@ func Benchmark_Couchbase_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Couchbase_Delete(b *testing.B) { +func Benchmark_Couchbase_SetAndDelete(b *testing.B) { testStore := newTestStore(b) - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/dynamodb/dynamodb_test.go b/dynamodb/dynamodb_test.go index 704b344b..808f1243 100644 --- a/dynamodb/dynamodb_test.go +++ b/dynamodb/dynamodb_test.go @@ -115,3 +115,42 @@ func Test_DynamoDB_Close(t *testing.T) { func Test_DynamoDB_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } + +func Benchmark_DynamoDB_Set(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } + + require.NoError(b, err) +} + +func Benchmark_DynamoDB_Get(b *testing.B) { + err := testStore.Set("john", []byte("doe"), 0) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err = testStore.Get("john") + } + + require.NoError(b, err) +} + +func Benchmark_DynamoDB_SetAndDelete(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + var err error + for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) + err = testStore.Delete("john") + } + + require.NoError(b, err) +} diff --git a/etcd/etcd_test.go b/etcd/etcd_test.go index bcbcf20e..7232f864 100644 --- a/etcd/etcd_test.go +++ b/etcd/etcd_test.go @@ -118,14 +118,13 @@ func Benchmark_Etcd_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Etcd_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Etcd_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index 5947b0c2..479fa954 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -157,14 +157,13 @@ func Benchmark_Memcache_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Memcache_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Memcache_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/memory/memory_test.go b/memory/memory_test.go index bcb100e1..c5f75a24 100644 --- a/memory/memory_test.go +++ b/memory/memory_test.go @@ -174,14 +174,13 @@ func Benchmark_Memory_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Memory_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Memory_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/minio/minio_test.go b/minio/minio_test.go index 683c985e..9740dfb4 100644 --- a/minio/minio_test.go +++ b/minio/minio_test.go @@ -220,14 +220,13 @@ func Benchmark_Minio_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Minio_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Minio_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/mongodb/mongodb_test.go b/mongodb/mongodb_test.go index 36f3fe13..a12d1f50 100644 --- a/mongodb/mongodb_test.go +++ b/mongodb/mongodb_test.go @@ -157,14 +157,13 @@ func Benchmark_MongoDB_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_MongoDB_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_MongoDB_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/mssql/mssql_test.go b/mssql/mssql_test.go index 6cdf1783..bb209caf 100644 --- a/mssql/mssql_test.go +++ b/mssql/mssql_test.go @@ -200,14 +200,13 @@ func Benchmark_MSSQL_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_MSSQL_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_MSSQL_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index 66a74c34..6bb4eb6c 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_test.go @@ -216,14 +216,13 @@ func Benchmark_MYSQL_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_MYSQL_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_MYSQL_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/pebble/pebble_test.go b/pebble/pebble_test.go index 505645ac..2b8a34e6 100644 --- a/pebble/pebble_test.go +++ b/pebble/pebble_test.go @@ -132,14 +132,13 @@ func Benchmark_Pebble_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Pebble_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Pebble_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/postgres/postgres_test.go b/postgres/postgres_test.go index 6057e721..0a226522 100644 --- a/postgres/postgres_test.go +++ b/postgres/postgres_test.go @@ -197,14 +197,13 @@ func Benchmark_Postgres_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Postgres_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Postgres_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/redis/redis_test.go b/redis/redis_test.go index 9ecbb92d..42aca367 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -152,11 +152,10 @@ func Test_Redis_Initalize_WithURL_TLS(t *testing.T) { return } tlsCfg := &tls.Config{ - MinVersion: tls.VersionTLS12, - CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}, - PreferServerCipherSuites: true, - InsecureSkipVerify: true, - Certificates: []tls.Certificate{cer}, + MinVersion: tls.VersionTLS12, + CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}, + InsecureSkipVerify: true, + Certificates: []tls.Certificate{cer}, CipherSuites: []uint16{ tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, @@ -195,11 +194,10 @@ func Test_Redis_Initalize_WithURL_TLS_Verify(t *testing.T) { return } tlsCfg := &tls.Config{ - MinVersion: tls.VersionTLS12, - CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}, - PreferServerCipherSuites: true, - InsecureSkipVerify: false, - Certificates: []tls.Certificate{cer}, + MinVersion: tls.VersionTLS12, + CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}, + InsecureSkipVerify: false, + Certificates: []tls.Certificate{cer}, CipherSuites: []uint16{ tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, @@ -441,14 +439,13 @@ func Benchmark_Redis_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Redis_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Redis_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/ristretto/ristretto_test.go b/ristretto/ristretto_test.go index c7bf4c5e..f0fcc46c 100644 --- a/ristretto/ristretto_test.go +++ b/ristretto/ristretto_test.go @@ -84,7 +84,7 @@ func Test_Ristretto_Set_Expiration(t *testing.T) { err := testStore.Set(key, val, exp) require.NoError(t, err) - testStore.Reset() + require.NoError(t, testStore.Reset()) } func Test_Ristretto_Get_Expired(t *testing.T) { @@ -190,14 +190,13 @@ func Benchmark_Ristretto_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Ristretto_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Ristretto_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/rueidis/rueidis_test.go b/rueidis/rueidis_test.go index b78cd936..265fb5a7 100644 --- a/rueidis/rueidis_test.go +++ b/rueidis/rueidis_test.go @@ -130,11 +130,10 @@ func Test_Rueidis_WithTLS(t *testing.T) { require.NoError(t, err) } tlsCfg := &tls.Config{ - MinVersion: tls.VersionTLS12, - CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}, - PreferServerCipherSuites: true, - InsecureSkipVerify: true, - Certificates: []tls.Certificate{cer}, + MinVersion: tls.VersionTLS12, + CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}, + InsecureSkipVerify: true, + Certificates: []tls.Certificate{cer}, CipherSuites: []uint16{ tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, @@ -264,14 +263,13 @@ func Benchmark_Rueidis_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Rueidis_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_Rueidis_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/s3/s3_test.go b/s3/s3_test.go index e8b0977c..5ed2cc5f 100644 --- a/s3/s3_test.go +++ b/s3/s3_test.go @@ -121,14 +121,13 @@ func Benchmark_S3_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_S3_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - +func Benchmark_S3_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } diff --git a/sqlite3/sqlite3_test.go b/sqlite3/sqlite3_test.go index 6d790628..c07689be 100644 --- a/sqlite3/sqlite3_test.go +++ b/sqlite3/sqlite3_test.go @@ -183,13 +183,12 @@ func Benchmark_SQLite3_Get(b *testing.B) { } func Benchmark_SQLite3_Delete(b *testing.B) { - err := testStore.Set("john", []byte("doe"), 0) - require.NoError(b, err) - b.ReportAllocs() b.ResetTimer() + var err error for i := 0; i < b.N; i++ { + _ = testStore.Set("john", []byte("doe"), 0) err = testStore.Delete("john") } From f39724354cca3093c48e0a0d02d62decaf1e5bd7 Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Fri, 15 Sep 2023 18:52:15 +0300 Subject: [PATCH 6/7] fix benchmark workflow --- .github/workflows/benchmark.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 69b66340..7276a3e1 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -138,11 +138,23 @@ jobs: run: | set -o pipefail for d in */ ; do + [[ $d == "tls/" ]] && continue + cd "$d" go test ./... -benchmem -run=^$ -bench . | tee -a ../output.txt cd .. done shell: bash + env: + MSSQL_DATABASE: master + MSSQL_USERNAME: sa + MSSQL_PASSWORD: MsSql!1234 + MYSQL_USERNAME: username + MYSQL_PASSWORD: password + MYSQL_DATABASE: fiber + POSTGRES_DATABASE: fiber + POSTGRES_USERNAME: username + POSTGRES_PASSWORD: "pass#w%rd" - name: Get Previous Benchmark Results uses: actions/cache@v3 From f113998db1b15e67832551e6e28b1c3736425713 Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Fri, 15 Sep 2023 19:37:46 +0300 Subject: [PATCH 7/7] fix redis tests --- .github/workflows/benchmark.yml | 18 ++++++------------ .github/workflows/test-redis.yml | 3 ++- .github/workflows/test-rueidis.yml | 3 ++- badger/badger_test.go | 2 +- bbolt/bbolt_test.go | 2 +- memory/memory_test.go | 29 ----------------------------- sqlite3/sqlite3_test.go | 2 +- 7 files changed, 13 insertions(+), 46 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 7276a3e1..adcdcd2a 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -90,10 +90,12 @@ jobs: - name: Install Azurite run: | docker run -d -p 10000:10000 mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0 --blobPort 10000 + - name: Install Coherence run: | docker run -d -p 1408:1408 -p 30000:30000 ghcr.io/oracle/coherence-ce:22.06.5 sleep 30 + - name: Install couchbase run: | docker run --name couchbase -d -p 8091-8097:8091-8097 -p 9123:9123 -p 11207:11207 -p 11210:11210 -p 11280:11280 -p 18091-18097:18091-18097 couchbase:enterprise-7.1.1 @@ -101,6 +103,7 @@ jobs: docker exec --tty couchbase couchbase-cli cluster-init -c localhost:8091 --cluster-username admin --cluster-password 123456 --cluster-ramsize 256 --services data sleep 10 docker exec --tty couchbase couchbase-cli bucket-create -c localhost:8091 --username admin --password 123456 --bucket fiber_storage --bucket-type couchbase --bucket-ramsize 100 --enable-flush 1 + - name: Install etcd run: | docker run -d --name Etcd-server \ @@ -109,17 +112,10 @@ jobs: --env ALLOW_NONE_AUTHENTICATION=yes \ --env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 \ bitnami/etcd:latest + - name: Install MinIO run: | docker run -d --restart always -p 9000:9000 --name storage-minio -e MINIO_ROOT_USER='minio-user' -e MINIO_ROOT_PASSWORD='minio-password' minio/minio server /data - - name: Generate TLS certs - run: ./.github/scripts/gen-test-certs.sh - - - name: Add Custom CA cert - run: sudo cp /home/runner/work/storage/storage/tls/ca.crt /usr/local/share/ca-certificates/custom.crt - - - name: Trust Custom CA Cert - run: sudo update-ca-certificates - name: Setup Redis uses: shogo82148/actions-setup-redis@v1 @@ -129,10 +125,7 @@ jobs: - name: Run Redis run: | - redis-server --tls-port 6380 --port 6379 \ - --tls-cert-file /home/runner/work/storage/storage/tls/redis.crt \ - --tls-key-file /home/runner/work/storage/storage/tls/redis.key \ - --tls-ca-cert-file /home/runner/work/storage/storage/tls/ca.crt & + redis-server --port 6379 & - name: Run Benchmarks run: | @@ -169,6 +162,7 @@ jobs: output-file-path: output.txt github-token: ${{ secrets.GITHUB_TOKEN }} benchmark-data-dir-path: "benchmarks" + alert-threshold: "300%" fail-on-alert: true comment-on-alert: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }} #summary-always: ${{ github.event_name != 'push' && github.event_name != 'workflow_dispatch' }} diff --git a/.github/workflows/test-redis.yml b/.github/workflows/test-redis.yml index 507ea8d6..a4e58b6f 100644 --- a/.github/workflows/test-redis.yml +++ b/.github/workflows/test-redis.yml @@ -56,7 +56,7 @@ jobs: --tls-auth-clients no & - name: Setup Redis Cluster - uses: vishnudxb/redis-cluster@1.0.8 + uses: vishnudxb/redis-cluster@1.0.9 with: master1-port: 7000 master2-port: 7001 @@ -64,6 +64,7 @@ jobs: slave1-port: 7003 slave2-port: 7004 slave3-port: 7005 + sleep-duration: 10 - name: Install Go uses: actions/setup-go@v4 diff --git a/.github/workflows/test-rueidis.yml b/.github/workflows/test-rueidis.yml index bf96d39a..71307b50 100644 --- a/.github/workflows/test-rueidis.yml +++ b/.github/workflows/test-rueidis.yml @@ -43,7 +43,7 @@ jobs: --tls-ca-cert-file /home/runner/work/storage/storage/tls/ca.crt & - name: Setup Redis Cluster - uses: vishnudxb/redis-cluster@1.0.8 + uses: vishnudxb/redis-cluster@1.0.9 with: master1-port: 7000 master2-port: 7001 @@ -51,6 +51,7 @@ jobs: slave1-port: 7003 slave2-port: 7004 slave3-port: 7005 + sleep-duration: 10 - name: Install Go uses: actions/setup-go@v4 diff --git a/badger/badger_test.go b/badger/badger_test.go index 3e90ae9e..d393d36c 100644 --- a/badger/badger_test.go +++ b/badger/badger_test.go @@ -147,7 +147,7 @@ func Benchmark_Badger_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Badger_Delete(b *testing.B) { +func Benchmark_Badger_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() diff --git a/bbolt/bbolt_test.go b/bbolt/bbolt_test.go index e53918c9..565dd173 100644 --- a/bbolt/bbolt_test.go +++ b/bbolt/bbolt_test.go @@ -136,7 +136,7 @@ func Benchmark_Bbolt_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_Bbolt_Delete(b *testing.B) { +func Benchmark_Bbolt_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer() diff --git a/memory/memory_test.go b/memory/memory_test.go index c5f75a24..c80fce93 100644 --- a/memory/memory_test.go +++ b/memory/memory_test.go @@ -4,7 +4,6 @@ import ( "testing" "time" - "github.com/gofiber/utils/v2" "github.com/stretchr/testify/require" ) @@ -120,34 +119,6 @@ func Test_Storage_Memory_Conn(t *testing.T) { require.True(t, testStore.Conn() != nil) } -// go test -v -run=^$ -bench=Benchmark_Storage_Memory -benchmem -count=4 -func Benchmark_Storage_Memory(b *testing.B) { - keyLength := 1000 - keys := make([]string, keyLength) - for i := 0; i < keyLength; i++ { - keys[i] = utils.UUID() - } - value := []byte("joe") - - ttl := 2 * time.Second - b.Run("fiber_memory", func(b *testing.B) { - d := New() - b.ReportAllocs() - b.ResetTimer() - for n := 0; n < b.N; n++ { - for _, key := range keys { - d.Set(key, value, ttl) - } - for _, key := range keys { - _, _ = d.Get(key) - } - for _, key := range keys { - d.Delete(key) - } - } - }) -} - func Benchmark_Memory_Set(b *testing.B) { b.ReportAllocs() b.ResetTimer() diff --git a/sqlite3/sqlite3_test.go b/sqlite3/sqlite3_test.go index c07689be..c7965ac1 100644 --- a/sqlite3/sqlite3_test.go +++ b/sqlite3/sqlite3_test.go @@ -182,7 +182,7 @@ func Benchmark_SQLite3_Get(b *testing.B) { require.NoError(b, err) } -func Benchmark_SQLite3_Delete(b *testing.B) { +func Benchmark_SQLite3_SetAndDelete(b *testing.B) { b.ReportAllocs() b.ResetTimer()