]> Cypherpunks repositories - gostls13.git/commitdiff
rpc: add benchmarks
authorRob Pike <r@golang.org>
Tue, 15 Mar 2011 20:09:11 +0000 (13:09 -0700)
committerRob Pike <r@golang.org>
Tue, 15 Mar 2011 20:09:11 +0000 (13:09 -0700)
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

index 344fe2457aced4d4281abd01e717c6d5b5259b6e..9e32b740f1e8b37a4cd54dabbcc55c2f4e7d3161 100644 (file)
@@ -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
+               }
+       }
+}