From f11f032486ada2ee8e3ae9ee8860acdeb2e8db00 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 15 Mar 2011 13:09:11 -0700 Subject: [PATCH] rpc: add benchmarks On my mac: mallocs per rpc round trip: 144 rpc.BenchmarkEndToEnd 10000 228244 ns/op Room for improvement. R=rsc CC=golang-dev https://golang.org/cl/4274058 --- src/pkg/rpc/server_test.go | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/pkg/rpc/server_test.go b/src/pkg/rpc/server_test.go index 344fe2457a..9e32b740f1 100644 --- a/src/pkg/rpc/server_test.go +++ b/src/pkg/rpc/server_test.go @@ -10,6 +10,7 @@ import ( "log" "net" "os" + "runtime" "strings" "sync" "testing" @@ -349,3 +350,52 @@ func testSendDeadlock(client *Client) { reply := new(Reply) client.Call("Arith.Add", args, reply) } + +func TestCountMallocs(t *testing.T) { + once.Do(startServer) + client, err := Dial("tcp", serverAddr) + if err != nil { + t.Error("error dialing", err) + } + args := &Args{7, 8} + reply := new(Reply) + mallocs := 0 - runtime.MemStats.Mallocs + const count = 100 + for i := 0; i < count; i++ { + err = client.Call("Arith.Add", args, reply) + if err != nil { + t.Errorf("Add: expected no error but got string %q", err.String()) + } + if reply.C != args.A+args.B { + t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B) + } + } + mallocs += runtime.MemStats.Mallocs + fmt.Printf("mallocs per rpc round trip: %d\n", mallocs/count) +} + +func BenchmarkEndToEnd(b *testing.B) { + b.StopTimer() + once.Do(startServer) + client, err := Dial("tcp", serverAddr) + if err != nil { + fmt.Println("error dialing", err) + return + } + + // Synchronous calls + args := &Args{7, 8} + reply := new(Reply) + b.StartTimer() + for i := 0; i < b.N; i++ { + err = client.Call("Arith.Add", args, reply) + if err != nil { + fmt.Printf("Add: expected no error but got string %q", err.String()) + break + } + if reply.C != args.A+args.B { + fmt.Printf("Add: expected %d got %d", reply.C, args.A+args.B) + break + } + } +} -- 2.50.0