-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
51 lines (48 loc) · 1.25 KB
/
benchmark_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
49
50
51
package rpcgo
import (
"context"
"fmt"
"testing"
)
func Benchmark_Call(b *testing.B) {
s, _ := newServer("localhost:8110")
s.rpcServer.Register("hello", func(_ context.Context, replyer *Replyer, arg *string) {
replyer.Reply(fmt.Sprintf("hello world:%s", *arg))
})
s.start()
cli, _ := newClient("localhost:8110")
caller := MakeCaller[string, string](cli.rpcClient, "hello", cli.rpcChannel)
b.ResetTimer()
b.StartTimer()
defer b.StopTimer()
for i := 0; i < b.N; i++ {
_, err := caller.Call(CallerOpt{}, MakeArgument("sniperHW"))
if err != nil {
b.Fatal(err.Error())
}
}
cli.rpcChannel.socket.Close(nil)
s.stop()
}
func Benchmark_Call_Concurrency(b *testing.B) {
s, _ := newServer("localhost:8110")
s.rpcServer.Register("hello", func(_ context.Context, replyer *Replyer, arg *string) {
replyer.Reply(fmt.Sprintf("hello world:%s", *arg))
})
s.start()
cli, _ := newClient("localhost:8110")
caller := MakeCaller[string, string](cli.rpcClient, "hello", cli.rpcChannel)
b.ResetTimer()
b.StartTimer()
defer b.StopTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := caller.Call(CallerOpt{}, MakeArgument("sniperHW"))
if err != nil {
b.Fatal(err.Error())
}
}
})
cli.rpcChannel.socket.Close(nil)
s.stop()
}