summaryrefslogtreecommitdiff
path: root/src/pkg/rpc/server_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/rpc/server_test.go')
-rw-r--r--src/pkg/rpc/server_test.go62
1 files changed, 55 insertions, 7 deletions
diff --git a/src/pkg/rpc/server_test.go b/src/pkg/rpc/server_test.go
index 05aaebceb..d4041ae70 100644
--- a/src/pkg/rpc/server_test.go
+++ b/src/pkg/rpc/server_test.go
@@ -6,10 +6,11 @@ package rpc
import (
"fmt"
- "http"
+ "http/httptest"
"log"
"net"
"os"
+ "runtime"
"strings"
"sync"
"testing"
@@ -103,11 +104,9 @@ func startNewServer() {
}
func startHttpServer() {
- var l net.Listener
- l, httpServerAddr = listenTCP()
- httpServerAddr = l.Addr().String()
+ server := httptest.NewServer(nil)
+ httpServerAddr = server.Listener.Addr().String()
log.Println("Test HTTP RPC server listening on", httpServerAddr)
- go http.Serve(l, nil)
}
func TestRPC(t *testing.T) {
@@ -313,12 +312,12 @@ func (WriteFailCodec) WriteRequest(*Request, interface{}) os.Error {
}
func (WriteFailCodec) ReadResponseHeader(*Response) os.Error {
- time.Sleep(60e9)
+ time.Sleep(120e9)
panic("unreachable")
}
func (WriteFailCodec) ReadResponseBody(interface{}) os.Error {
- time.Sleep(60e9)
+ time.Sleep(120e9)
panic("unreachable")
}
@@ -351,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
+ }
+ }
+}