Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RunParallel for benchmarks #139

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 33 additions & 59 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,94 +18,68 @@ import (
"context"
"net/http"
"net/http/httptest"
"sync"
"testing"

connect "connectrpc.com/connect"
pingv1 "connectrpc.com/otelconnect/internal/gen/observability/ping/v1"
"connectrpc.com/otelconnect/internal/gen/observability/ping/v1/pingv1connect"
)

const (
concurrency = 5
messagesToSend = 10
)

func BenchmarkStreamingServerNoOptions(b *testing.B) {
testStreaming(b, nil, nil)
}

func BenchmarkStreamingServerClientOption(b *testing.B) {
testStreaming(b, []connect.HandlerOption{connect.WithInterceptors(NewInterceptor())}, []connect.ClientOption{connect.WithInterceptors(NewInterceptor())})
}

func BenchmarkStreamingServerOption(b *testing.B) {
testStreaming(b, []connect.HandlerOption{connect.WithInterceptors(NewInterceptor())}, []connect.ClientOption{})
func BenchmarkStreamingBase(b *testing.B) {
benchStreaming(b, nil, nil)
}

func BenchmarkStreamingClientOption(b *testing.B) {
testStreaming(b, []connect.HandlerOption{}, []connect.ClientOption{connect.WithInterceptors(NewInterceptor())})
func BenchmarkStreamingWithInterceptor(b *testing.B) {
benchStreaming(b,
[]connect.HandlerOption{connect.WithInterceptors(NewInterceptor())},
[]connect.ClientOption{connect.WithInterceptors(NewInterceptor())},
)
}

func BenchmarkUnaryOtel(b *testing.B) {
benchUnary(b, []connect.HandlerOption{connect.WithInterceptors(NewInterceptor())}, []connect.ClientOption{connect.WithInterceptors(NewInterceptor())})
func BenchmarkUnaryBase(b *testing.B) {
benchUnary(b, nil, nil)
}

func BenchmarkUnary(b *testing.B) {
benchUnary(b, nil, nil)
func BenchmarkUnaryWithInterceptor(b *testing.B) {
benchUnary(b,
[]connect.HandlerOption{connect.WithInterceptors(NewInterceptor())},
[]connect.ClientOption{connect.WithInterceptors(NewInterceptor())},
)
}

func benchUnary(b *testing.B, handleropts []connect.HandlerOption, clientopts []connect.ClientOption) {
b.Helper()
svr, client := startBenchServer(handleropts, clientopts)
b.Cleanup(svr.Close)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
for j := 0; j < concurrency; j++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := client.Ping(context.Background(), &connect.Request[pingv1.PingRequest]{
Msg: &pingv1.PingRequest{Data: []byte("Sentence")},
})
if err != nil {
b.Log(err)
}
}()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := client.Ping(context.Background(), &connect.Request[pingv1.PingRequest]{
Msg: &pingv1.PingRequest{Data: []byte("Sentence")},
})
if err != nil {
b.Log(err)
}
}
wg.Wait()
}
})
}

func testStreaming(b *testing.B, handleropts []connect.HandlerOption, clientopts []connect.ClientOption) {
func benchStreaming(b *testing.B, handleropts []connect.HandlerOption, clientopts []connect.ClientOption) {
b.Helper()
_, client := startBenchServer(handleropts, clientopts)
req := &pingv1.CumSumRequest{Number: 12}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
for j := 0; j < concurrency; j++ {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
stream := client.CumSum(context.Background())
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < messagesToSend; j++ {
err := stream.Send(req)
if err != nil {
b.Error(err)
}
}
for j := 0; j < messagesToSend; j++ {
_, err := stream.Receive()
if err != nil {
b.Error(err)
}
}
}()
if err := stream.Send(req); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Send triggers 2 response on the sever:

for i := 0; i < messagesPerRequest; i++ {
so actually need to read twice to drain the service.

Will update the protos to be a pure ping service in a followup PR to make it simpler for testing.

b.Error(err)
}
if _, err := stream.Receive(); err != nil {
b.Error(err)
}
}
wg.Wait()
}
})
}

func startBenchServer(handleropts []connect.HandlerOption, clientopts []connect.ClientOption) (*httptest.Server, pingv1connect.PingServiceClient) {
Expand Down