Skip to content

Commit

Permalink
Add benchmarks for keyvalue stores.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Mar 16, 2024
1 parent 6f0ce9a commit fceb043
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 40 deletions.
45 changes: 33 additions & 12 deletions driver/aws/dynamokv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,57 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/service/dynamodb"
. "github.com/dogmatiq/persistencekit/driver/aws/dynamokv"
"github.com/dogmatiq/persistencekit/driver/aws/internal/dynamox"
"github.com/dogmatiq/persistencekit/kv"
)

func TestStore(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
client, table := setup(t)
kv.RunTests(
t,
func(t *testing.T) kv.Store {
return &Store{
Client: client,
Table: table,
}
},
)
}

func BenchmarkStore(b *testing.B) {
client, table := setup(b)
kv.RunBenchmarks(
b,
func(b *testing.B) kv.Store {
return &Store{
Client: client,
Table: table,
}
},
)
}

func setup(t testing.TB) (*dynamodb.Client, string) {
client := dynamox.NewTestClient(t)
table := "kvstore"

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

if err := CreateTable(ctx, client, table); err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

if err := dynamox.DeleteTableIfNotExists(ctx, client, table); err != nil {
t.Fatal(err)
}

cancel()
})

kv.RunTests(
t,
func(t *testing.T) kv.Store {
return &Store{
Client: client,
Table: table,
}
},
)
return client, table
}
2 changes: 1 addition & 1 deletion driver/aws/internal/dynamox/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// NewTestClient returns a new DynamoDB client for use in a test.
func NewTestClient(t *testing.T) *dynamodb.Client {
func NewTestClient(t testing.TB) *dynamodb.Client {
endpoint := os.Getenv("DOGMATIQ_TEST_DYNAMODB_ENDPOINT")
if endpoint == "" {
endpoint = "http://localhost:28000"
Expand Down
9 changes: 9 additions & 0 deletions driver/memory/memorykv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ func TestStore(t *testing.T) {
},
)
}

func BenchmarkStore(b *testing.B) {
kv.RunBenchmarks(
b,
func(b *testing.B) kv.Store {
return &Store{}
},
)
}
38 changes: 29 additions & 9 deletions driver/sql/postgres/pgkv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,42 @@ package pgkv_test

import (
"context"
"database/sql"
"testing"
"time"

. "github.com/dogmatiq/persistencekit/driver/sql/postgres/pgkv"
"github.com/dogmatiq/persistencekit/kv"
"github.com/dogmatiq/sqltest"
)

func TestStore(t *testing.T) {
ctx := context.Background()
db := setup(t)
kv.RunTests(
t,
func(t *testing.T) kv.Store {
return &Store{
DB: db,
}
},
)
}

func BenchmarkStore(b *testing.B) {
db := setup(b)
kv.RunBenchmarks(
b,
func(b *testing.B) kv.Store {
return &Store{
DB: db,
}
},
)
}

func setup(t testing.TB) *sql.DB {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

database, err := sqltest.NewDatabase(ctx, sqltest.PGXDriver, sqltest.PostgreSQL)
if err != nil {
Expand All @@ -32,12 +59,5 @@ func TestStore(t *testing.T) {
}
})

kv.RunTests(
t,
func(t *testing.T) kv.Store {
return &Store{
DB: db,
}
},
)
return db
}
16 changes: 8 additions & 8 deletions journal/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func RunTests(
return ctx, deps
}

t.Run("type Store", func(t *testing.T) {
t.Run("Store", func(t *testing.T) {
t.Parallel()

t.Run("func Open()", func(t *testing.T) {
t.Run("Open", func(t *testing.T) {
t.Parallel()

t.Run("allows a journal to be opened multiple times", func(t *testing.T) {
Expand Down Expand Up @@ -94,10 +94,10 @@ func RunTests(
})
})

t.Run("type Journal", func(t *testing.T) {
t.Run("Journal", func(t *testing.T) {
t.Parallel()

t.Run("func Bounds()", func(t *testing.T) {
t.Run("Bounds", func(t *testing.T) {
t.Parallel()

t.Run("it returns the expected bounds", func(t *testing.T) {
Expand Down Expand Up @@ -155,7 +155,7 @@ func RunTests(
})
})

t.Run("func Get()", func(t *testing.T) {
t.Run("Get", func(t *testing.T) {
t.Parallel()

t.Run("it returns ErrNotFound if there is no record at the given position", func(t *testing.T) {
Expand Down Expand Up @@ -225,7 +225,7 @@ func RunTests(
})
})

t.Run("func Range()", func(t *testing.T) {
t.Run("Range", func(t *testing.T) {
t.Parallel()

t.Run("calls the function for each record in the journal", func(t *testing.T) {
Expand Down Expand Up @@ -367,7 +367,7 @@ func RunTests(
})
})

t.Run("func Append()", func(t *testing.T) {
t.Run("Append", func(t *testing.T) {
t.Parallel()

t.Run("it does not return an error if there is no record at the given position", func(t *testing.T) {
Expand Down Expand Up @@ -439,7 +439,7 @@ func RunTests(
})
})

t.Run("func Truncate()", func(t *testing.T) {
t.Run("Truncate", func(t *testing.T) {
t.Parallel()

t.Run("it truncates the journal", func(t *testing.T) {
Expand Down
Loading

0 comments on commit fceb043

Please sign in to comment.