forked from Trial97/rpc-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpcx_test.go
48 lines (40 loc) · 951 Bytes
/
rpcx_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
43
44
45
46
47
48
package main
import (
"context"
"log"
"net"
"rpc-bench/airc"
"testing"
"github.com/smallnest/rpcx/client"
"github.com/smallnest/rpcx/server"
)
func startRPCxServer() {
s := server.NewServer()
s.Register(new(airc.Arith), "")
var l net.Listener
l, serverAddr = listenTCP()
log.Println("Test RPC server listening on", serverAddr)
go s.ServeListener("tcp", l)
}
func BenchmarkRPCXCall(b *testing.B) {
startRPCxServer()
c := client.NewClient(client.DefaultOption)
err := c.Connect("tcp", serverAddr)
if err != nil {
b.Fatal("dialing", err)
}
defer c.Close()
ctx := context.Background()
args := &airc.Args{7, 8}
reply := new(airc.Reply)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = c.Call(ctx, "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)
}
}
}