Skip to content

Commit

Permalink
Add a benchmark for streaming calls
Browse files Browse the repository at this point in the history
  • Loading branch information
zenhack committed Nov 24, 2022
1 parent 447444b commit 8949924
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions rpc/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,50 @@ import (
"capnproto.org/go/capnp/v3"
"capnproto.org/go/capnp/v3/rpc"
testcp "capnproto.org/go/capnp/v3/rpc/internal/testcapnp"
"capnproto.org/go/capnp/v3/std/capnp/stream"
)

func BenchmarkStreaming(b *testing.B) {
ctx := context.Background()
p1, p2 := net.Pipe()
srv := testcp.StreamTest_ServerToClient(nullStream{})
conn1 := rpc.NewConn(rpc.NewStreamTransport(p1), &rpc.Options{
BootstrapClient: capnp.Client(srv),
})
defer conn1.Close()
conn2 := rpc.NewConn(rpc.NewStreamTransport(p2), nil)
defer conn2.Close()
bootstrap := testcp.StreamTest(conn2.Bootstrap(ctx))
defer bootstrap.Release()
var (
futures []stream.StreamResult_Future
releaseFuncs []capnp.ReleaseFunc
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
fut, rel := bootstrap.Push(ctx, nil)
futures = append(futures, fut)
releaseFuncs = append(releaseFuncs, rel)
}
for i, fut := range futures {
_, err := fut.Struct()
if err != nil {
b.Errorf("Error waiting on future #%v: %v", i, err)
}
}
for _, rel := range releaseFuncs {
rel()
}
}

// nullStream implements testcp.StreamTest, ignoring the data it is sent.
type nullStream struct {
}

func (nullStream) Push(context.Context, testcp.StreamTest_push) error {
return nil
}

func BenchmarkPingPong(b *testing.B) {
p1, p2 := net.Pipe()
srv := testcp.PingPong_ServerToClient(pingPongServer{})
Expand Down

0 comments on commit 8949924

Please sign in to comment.