forked from Trial97/rpc-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_test.go
42 lines (36 loc) · 788 Bytes
/
rpc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"log"
"net"
"net/rpc"
"rpc-bench/air"
"testing"
)
func startRPCServer() {
rpc.Register(new(air.Arith))
var l net.Listener
l, serverAddr = listenTCP()
log.Println("Test RPC server listening on", serverAddr)
go rpc.Accept(l)
}
func BenchmarkRPCCall(b *testing.B) {
startRPCServer()
client, err := rpc.Dial("tcp", serverAddr)
if err != nil {
b.Fatal("dialing", err)
}
defer client.Close()
// Synchronous calls
args := &air.Args{7, 8}
reply := new(air.Reply)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = client.Call("Arith.Add", args, reply)
if err != nil {
b.Errorf("Add: expected no error but got string %q", err.Error())
}
if reply.C != args.A+args.B {
b.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
}
}
}