package rpc
import (
+ "flag"
"fmt"
"http/httptest"
"log"
"net"
"os"
"runtime"
+ "runtime/pprof"
"strings"
"sync"
"testing"
once, newOnce, httpOnce sync.Once
)
+var memprofile = flag.String("memprofile", "", "write the memory profile in TestCountMallocs to the named file")
+
const (
second = 1e9
newHttpPath = "/foo"
}
func TestCountMallocs(t *testing.T) {
+ runtime.MemProfileRate = 1
once.Do(startServer)
client, err := Dial("tcp", serverAddr)
if err != nil {
args := &Args{7, 8}
reply := new(Reply)
mallocs := 0 - runtime.MemStats.Mallocs
- const count = 100
+ const count = 10000
for i := 0; i < count; i++ {
err = client.Call("Arith.Add", args, reply)
if err != nil {
}
}
mallocs += runtime.MemStats.Mallocs
+ if *memprofile != "" {
+ if fd, err := os.Open(*memprofile, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666); err != nil {
+ t.Errorf("can't open %s: %s", *memprofile, err)
+ } else {
+ if err = pprof.WriteHeapProfile(fd); err != nil {
+ t.Errorf("can't write %s: %s", *memprofile, err)
+ }
+ fd.Close()
+ }
+ }
fmt.Printf("mallocs per rpc round trip: %d\n", mallocs/count)
}