Commit f11f0324 authored by Rob Pike's avatar Rob Pike

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
parent c3254bbe
......@@ -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
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment