diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 00000000..adcdcd2a --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,170 @@ +on: + push: + branches: + - master + - main + paths: + - "**" + - "!**.md" + pull_request: + paths: + - "**" + - "!**.md" + +permissions: + deployments: write + contents: write + +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 + + - 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: 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: Setup Redis + uses: shogo82148/actions-setup-redis@v1 + with: + redis-version: '7.x' + auto-start: 'false' + + - name: Run Redis + run: | + redis-server --port 6379 & + + - name: Run Benchmarks + 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 + 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" + 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' }} + 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 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/.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/arangodb/arangodb_test.go b/arangodb/arangodb_test.go index 9f25b201..ef0ef4f1 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,49 @@ 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_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/azureblob/azureblob_test.go b/azureblob/azureblob_test.go index b1f92a0e..2b5b1902 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,11 +130,48 @@ 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) { + 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) { + 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_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/badger/badger_test.go b/badger/badger_test.go index 267ab4e5..d393d36c 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 ( @@ -118,3 +120,42 @@ 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_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/bbolt/bbolt_test.go b/bbolt/bbolt_test.go index 3a776d95..565dd173 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 ( @@ -96,3 +109,42 @@ 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_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/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 666aba81..009fecd2 100644 --- a/coherence/coherence_test.go +++ b/coherence/coherence_test.go @@ -4,7 +4,8 @@ package coherence * Copyright © 2023, Oracle and/or its affiliates. */ import ( - "github.com/gofiber/utils" + "github.com/stretchr/testify/require" + "os" "testing" "time" ) @@ -17,152 +18,145 @@ 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() + + testStore, err := New(config...) + require.NoError(t, err) + + err = testStore.Reset() + + return testStore, err +} + func Test_Coherence_Set_And_Get(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) - - err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + err := testStore.Set(key1, value1, 0) + require.NoError(t, err) val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, value1, val) - - utils.AssertEqual(t, true, testStore.Conn() != nil) + require.NoError(t, err) + require.Equal(t, value1, val) - utils.AssertEqual(t, testStore.Close(), nil) + require.NotNil(t, testStore.Conn()) } func Test_Coherence_Set_Override(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) - - err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + err := testStore.Set(key1, value1, 0) + 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) - - utils.AssertEqual(t, testStore.Close(), nil) + require.NoError(t, err) + require.Equal(t, value2, val) } func Test_Coherence_Set_With_Reset(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) - - err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + err := testStore.Set(key1, value1, 0) + 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) + testStore2, err := newTestStore(t, Config{Reset: true}) + require.NoError(t, err) - val, err = testStore.Get(key1) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, true, len(val) == 0) + val, err = testStore2.Get(key1) + require.NoError(t, err) + require.True(t, len(val) == 0) - utils.AssertEqual(t, testStore.Close(), nil) + require.Equal(t, nil, testStore2.Close()) } func Test_Coherence_Set_With_Expiry(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) - // set with an expiry of 5 seconds - err = testStore.Set(key1, value1, time.Duration(5)*time.Second) - utils.AssertEqual(t, err, nil) + err := testStore.Set(key1, value1, time.Duration(5)*time.Second) + 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) } func Test_Coherence_Get_Missing(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) - - val, err = testStore.Get(missingKey) - utils.AssertEqual(t, err, nil) - utils.AssertEqual(t, true, len(val) == 0) - - utils.AssertEqual(t, testStore.Close(), nil) + val, err := testStore.Get(missingKey) + require.NoError(t, err) + require.True(t, len(val) == 0) } func Test_Coherence_Reset(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) - - err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + err := testStore.Set(key1, value1, 0) + 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) - - utils.AssertEqual(t, testStore.Close(), nil) + require.NoError(t, err) + require.True(t, len(val) == 0) } func Test_Coherence_Set_And_Delete(t *testing.T) { var val []byte - testStore, err := newTestStore(t) - utils.AssertEqual(t, err, nil) - - err = testStore.Set(key1, value1, 0) - utils.AssertEqual(t, err, nil) + err := testStore.Set(key1, value1, 0) + 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) - - utils.AssertEqual(t, testStore.Close(), nil) + require.NoError(t, err) + require.True(t, len(val) == 0) } // TestCoherenceWithScope ensures we can create multiple session stores with multiple scopes. @@ -171,37 +165,66 @@ 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()) } -// 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) { + b.ReportAllocs() + b.ResetTimer() - err = testStore.Reset() + var err error + for i := 0; i < b.N; i++ { + err = testStore.Set("john", []byte("doe"), 0) + } - return testStore, err + require.NoError(b, err) +} + +func Benchmark_Coherence_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_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") + } + + 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= diff --git a/couchbase/couchbase_test.go b/couchbase/couchbase_test.go index 40d02842..fa8cc6b2 100644 --- a/couchbase/couchbase_test.go +++ b/couchbase/couchbase_test.go @@ -7,123 +7,139 @@ 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 := newTestStore(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_Couchbase_Get(b *testing.B) { + testStore := newTestStore(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_Couchbase_SetAndDelete(b *testing.B) { + testStore := newTestStore(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/dynamodb/dynamodb_test.go b/dynamodb/dynamodb_test.go index 55c7c932..808f1243 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 ( @@ -106,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 d747dd30..7232f864 100644 --- a/etcd/etcd_test.go +++ b/etcd/etcd_test.go @@ -1,110 +1,132 @@ 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, testStore.Conn() != nil) +} + +func Benchmark_Etcd_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_Etcd_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_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") + } - require.True(t, testStorage.Conn() != nil) + require.NoError(b, err) } diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index 98b88e96..479fa954 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 ( @@ -118,3 +130,42 @@ 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_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/memory/memory_test.go b/memory/memory_test.go index 64e59124..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,30 +119,41 @@ 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() +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") } - 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) - } - } - }) + + 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") + } + + require.NoError(b, err) } diff --git a/minio/minio_test.go b/minio/minio_test.go index 07fb0724..9740dfb4 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 ( @@ -182,3 +193,42 @@ 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_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/mongodb/mongodb_test.go b/mongodb/mongodb_test.go index 0bfb28c2..a12d1f50 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 ( @@ -120,3 +130,42 @@ 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_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/mssql/mssql_test.go b/mssql/mssql_test.go index b1c86588..bb209caf 100644 --- a/mssql/mssql_test.go +++ b/mssql/mssql_test.go @@ -173,3 +173,42 @@ 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_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/mysql/mysql_test.go b/mysql/mysql_test.go index 003ec9f1..6bb4eb6c 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{ @@ -189,3 +189,42 @@ 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_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/pebble/pebble_test.go b/pebble/pebble_test.go index 5397d050..2b8a34e6 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) { @@ -105,3 +105,42 @@ 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_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/postgres/postgres_test.go b/postgres/postgres_test.go index 3ad85a1c..0a226522 100644 --- a/postgres/postgres_test.go +++ b/postgres/postgres_test.go @@ -170,3 +170,42 @@ 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_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/redis/redis_test.go b/redis/redis_test.go index 0c2d2488..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, @@ -414,3 +412,42 @@ 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_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/ristretto/ristretto_test.go b/ristretto/ristretto_test.go index 94df2917..f0fcc46c 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 ( @@ -73,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) { @@ -152,3 +163,42 @@ 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_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/rueidis/rueidis_test.go b/rueidis/rueidis_test.go index 933424cb..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, @@ -237,3 +236,42 @@ 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_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/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, }, diff --git a/s3/s3_test.go b/s3/s3_test.go index 3a3e49e5..5ed2cc5f 100644 --- a/s3/s3_test.go +++ b/s3/s3_test.go @@ -94,3 +94,42 @@ 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_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/sqlite3/sqlite3_test.go b/sqlite3/sqlite3_test.go index 08efce1e..c7965ac1 100644 --- a/sqlite3/sqlite3_test.go +++ b/sqlite3/sqlite3_test.go @@ -155,3 +155,42 @@ 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_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) +}