Skip to content

Commit

Permalink
Merge pull request #53 from runreveal/alan/basic-bench
Browse files Browse the repository at this point in the history
perf: add basic benchmark to demonstrate overhead
  • Loading branch information
abraithwaite authored Jul 7, 2024
2 parents c9dae1c + 8046812 commit f6ee4a5
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 9 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
golang.org/x/sys v0.18.0
golang.org/x/sys v0.21.0
)

require (
Expand All @@ -23,7 +23,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
Expand All @@ -57,17 +57,17 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
15 changes: 15 additions & 0 deletions test/bench.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
goos: darwin
goarch: arm64
pkg: github.com/runreveal/kawa/test
BenchmarkMem-10 3 479789639 ns/op
BenchmarkMem-10 3 470265097 ns/op
BenchmarkMem-10 3 468312402 ns/op
BenchmarkMem-10 3 441723403 ns/op
BenchmarkMem-10 3 478304306 ns/op
BenchmarkMem-10 3 481978250 ns/op
BenchmarkMem-10 3 464515333 ns/op
BenchmarkMem-10 3 478364931 ns/op
BenchmarkMem-10 3 478826166 ns/op
BenchmarkMem-10 3 483086972 ns/op
PASS
ok github.com/runreveal/kawa/test 28.571s
15 changes: 15 additions & 0 deletions test/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/runreveal/kawa/x/printer"
"github.com/runreveal/kawa/x/scanner"
"github.com/segmentio/ksuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -25,6 +26,20 @@ func TestMem(t *testing.T) {
SuiteTest(t, src, dst)
}

func BenchmarkMem(b *testing.B) {
schan := make(chan []byte)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
src := memory.NewMemSource((<-chan []byte)(schan))
dst := memory.NewMemDestination[[]byte]((chan<- []byte)(schan))
runner := BuildBench(b, 1000000, src, dst)
b.StartTimer()
err := runner.Run(context.Background())
assert.NoError(b, err)
}
}

func TestIO(t *testing.T) {
reader, writer := io.Pipe()
// Delim should be a string of bytes which is unlikely occur randomly
Expand Down
58 changes: 57 additions & 1 deletion test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ func SuiteTest(t *testing.T, src kawa.Source[[]byte], dst kawa.Destination[[]byt
}
}

func mark(t *testing.T, actual []byte, sent [][]byte, seen []bool) {
type aide interface {
assert.TestingT
Helper()
}

func mark(t aide, actual []byte, sent [][]byte, seen []bool) {
t.Helper()
for i, want := range sent {
if bytes.Equal(actual, want) {
Expand All @@ -97,3 +102,54 @@ func mark(t *testing.T, actual []byte, sent [][]byte, seen []bool) {
}
}
}

func BuildBench(b *testing.B, count int, src kawa.Source[[]byte], dst kawa.Destination[[]byte]) await.Runner {
wait := await.New()
want := make([][]byte, 25)
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range want {
want[i] = make([]byte, 20)
_, err := rng.Read(want[i])
assert.NoError(b, err)
}

if runnner, ok := src.(await.Runner); ok {
wait.Add(runnner)
}
if runnner, ok := dst.(await.Runner); ok {
wait.Add(runnner)
}

wait.Add(await.RunFunc(func(ctx context.Context) error {
seen := 0
for {
_, ack, err := src.Recv(ctx)
if !errors.Is(err, context.Canceled) {
assert.NoError(b, err)
}
if ack != nil {
ack()
}
seen++
if seen == count {
break
}
}
return nil
}))

wait.Add(await.RunFunc(func(ctx context.Context) error {
for i := 0; i < count; i++ {
toSend := want[i%len(want)]
err := dst.Send(ctx, nil, kawa.Message[[]byte]{Value: toSend})
if !errors.Is(err, context.Canceled) {
assert.NoError(b, err)
}
}
// Wait until the source exits.
<-ctx.Done()
return nil
}))

return wait
}

0 comments on commit f6ee4a5

Please sign in to comment.