Skip to content

Commit

Permalink
add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mjneil committed Jun 10, 2020
1 parent c1a6cb4 commit 6688fb4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ By default, shard mapping is disabled. To use the shard mapping feature, you nee

This package provides a GetShards function `GetKinesisShardsFunc` that uses an AWS client to call the `ListShards` API to get the shard list.

**Note** At the time of writing, using the shard map feature adds significant overhead. Depending on the configuration and your record set, this can be more than 2x slower. Providing an explicit hash key for user records can help reduce this by quite a bit. Take a look at the benchmarks in `producer_test.go` for examples.

#### Example
```go
package main
Expand Down
81 changes: 67 additions & 14 deletions producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
k "github.com/aws/aws-sdk-go/service/kinesis"
"github.com/google/uuid"
)

type responseMock struct {
Expand Down Expand Up @@ -243,30 +244,86 @@ func (c *mockBenchmarkClient) PutRecords(*k.PutRecordsInput) (*k.PutRecordsOutpu
}, nil
}

func simpleUUIDRecords(dataSize int) func(int) ([]UserRecord, error) {
return func(count int) ([]UserRecord, error) {
records := make([]UserRecord, count)
for i := 0; i < count; i++ {
records[i] = newTestUserRecord(uuid.New().String(), "", mockData("foobar", dataSize))
}
return records, nil
}
}

func explicitHashKeyRecords(getShards GetShardsFunc, dataSize int) func(int) ([]UserRecord, error) {
return func(count int) ([]UserRecord, error) {
shards, _, err := getShards(nil)
if err != nil {
return nil, err
}

shardCount := len(shards)
records := make([]UserRecord, count)
for i := 0; i < count; i++ {
bucket := i % shardCount
shard := shards[bucket]
records[i] = newTestUserRecord(
uuid.New().String(),
*shard.HashKeyRange.StartingHashKey,
mockData("foobar", dataSize))
}
return records, nil
}
}

func BenchmarkProducer(b *testing.B) {
testCases := []struct {
name string
config *Config
name string
config *Config
records func(count int) ([]UserRecord, error)
}{
{
name: "default producer",
config: &Config{
StreamName: "default producer",
StreamName: "default producer",
BacklogCount: 10000,
},
records: simpleUUIDRecords(1024),
},
{
name: "10 shard count",
config: &Config{
StreamName: "10 shard count",
GetShards: StaticGetShardsFunc(10),
StreamName: "10 shard count",
GetShards: StaticGetShardsFunc(10),
BacklogCount: 10000,
},
records: simpleUUIDRecords(1024),
},
{
name: "500 shard count",
config: &Config{
StreamName: "500 shard count",
GetShards: StaticGetShardsFunc(500),
StreamName: "500 shard count",
GetShards: StaticGetShardsFunc(500),
BacklogCount: 10000,
},
records: simpleUUIDRecords(1024),
},
{
name: "10 shard count using explicit hash key",
config: &Config{
StreamName: "10 shard count",
GetShards: StaticGetShardsFunc(10),
BacklogCount: 10000,
},
records: explicitHashKeyRecords(StaticGetShardsFunc(10), 1024),
},
{
name: "500 shard count using explicit hash key",
config: &Config{
StreamName: "500 shard count",
GetShards: StaticGetShardsFunc(500),
BacklogCount: 10000,
},
records: explicitHashKeyRecords(StaticGetShardsFunc(500), 1024),
},
}

Expand All @@ -293,13 +350,9 @@ func BenchmarkProducer(b *testing.B) {
each := b.N / workers
workerWG.Add(workers)

records := make([]UserRecord, b.N)
for i := 0; i < b.N; i++ {
r, err := newMyExampleUserRecord("foo", "bar")
if err != nil {
b.Fatal(err)
}
records[i] = r
records, err := tc.records(b.N)
if err != nil {
b.Fatal(err)
}

p.Start()
Expand Down

0 comments on commit 6688fb4

Please sign in to comment.