Commit bc34cda7 authored by Rob Pike's avatar Rob Pike

rpc: add the ability to write out a memory profile during testing.

R=rsc, dsymonds
CC=golang-dev
https://golang.org/cl/4290047
parent f11f0324
...@@ -5,12 +5,14 @@ ...@@ -5,12 +5,14 @@
package rpc package rpc
import ( import (
"flag"
"fmt" "fmt"
"http/httptest" "http/httptest"
"log" "log"
"net" "net"
"os" "os"
"runtime" "runtime"
"runtime/pprof"
"strings" "strings"
"sync" "sync"
"testing" "testing"
...@@ -23,6 +25,8 @@ var ( ...@@ -23,6 +25,8 @@ var (
once, newOnce, httpOnce sync.Once once, newOnce, httpOnce sync.Once
) )
var memprofile = flag.String("memprofile", "", "write the memory profile in TestCountMallocs to the named file")
const ( const (
second = 1e9 second = 1e9
newHttpPath = "/foo" newHttpPath = "/foo"
...@@ -352,6 +356,7 @@ func testSendDeadlock(client *Client) { ...@@ -352,6 +356,7 @@ func testSendDeadlock(client *Client) {
} }
func TestCountMallocs(t *testing.T) { func TestCountMallocs(t *testing.T) {
runtime.MemProfileRate = 1
once.Do(startServer) once.Do(startServer)
client, err := Dial("tcp", serverAddr) client, err := Dial("tcp", serverAddr)
if err != nil { if err != nil {
...@@ -360,7 +365,7 @@ func TestCountMallocs(t *testing.T) { ...@@ -360,7 +365,7 @@ func TestCountMallocs(t *testing.T) {
args := &Args{7, 8} args := &Args{7, 8}
reply := new(Reply) reply := new(Reply)
mallocs := 0 - runtime.MemStats.Mallocs mallocs := 0 - runtime.MemStats.Mallocs
const count = 100 const count = 10000
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
err = client.Call("Arith.Add", args, reply) err = client.Call("Arith.Add", args, reply)
if err != nil { if err != nil {
...@@ -371,6 +376,16 @@ func TestCountMallocs(t *testing.T) { ...@@ -371,6 +376,16 @@ func TestCountMallocs(t *testing.T) {
} }
} }
mallocs += runtime.MemStats.Mallocs 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) fmt.Printf("mallocs per rpc round trip: %d\n", mallocs/count)
} }
......
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