forked from go-kratos/kratos
/
client.go
188 lines (170 loc) · 4.68 KB
/
client.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"flag"
"log"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/zongshuai/kratos/pkg/net/netutil/breaker"
"github.com/zongshuai/kratos/pkg/net/rpc/warden"
"github.com/zongshuai/kratos/pkg/net/rpc/warden/internal/benchmark/bench/proto"
xtime "github.com/zongshuai/kratos/pkg/time"
goproto "github.com/gogo/protobuf/proto"
"github.com/montanaflynn/stats"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
iws = 65535 * 1000
iwsc = 65535 * 10000
readBuffer = 32 * 1024
writeBuffer = 32 * 1024
)
var concurrency = flag.Int("c", 50, "concurrency")
var total = flag.Int("t", 500000, "total requests for all clients")
var host = flag.String("s", "127.0.0.1:8972", "server ip and port")
var isWarden = flag.Bool("w", true, "is warden or grpc client")
var strLen = flag.Int("l", 600, "the length of the str")
func wardenCli() proto.HelloClient {
log.Println("start warden cli")
client := warden.NewClient(&warden.ClientConfig{
Dial: xtime.Duration(time.Second * 10),
Timeout: xtime.Duration(time.Second * 10),
Breaker: &breaker.Config{
Window: xtime.Duration(3 * time.Second),
Bucket: 10,
Request: 20,
K: 1.5,
},
},
grpc.WithInitialWindowSize(iws),
grpc.WithInitialConnWindowSize(iwsc),
grpc.WithReadBufferSize(readBuffer),
grpc.WithWriteBufferSize(writeBuffer))
conn, err := client.Dial(context.Background(), *host)
if err != nil {
log.Fatalf("did not connect: %v", err)
}
cli := proto.NewHelloClient(conn)
return cli
}
func grpcCli() proto.HelloClient {
log.Println("start grpc cli")
conn, err := grpc.Dial(*host, grpc.WithInsecure(),
grpc.WithInitialWindowSize(iws),
grpc.WithInitialConnWindowSize(iwsc),
grpc.WithReadBufferSize(readBuffer),
grpc.WithWriteBufferSize(writeBuffer))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
cli := proto.NewHelloClient(conn)
return cli
}
func main() {
flag.Parse()
c := *concurrency
m := *total / c
var wg sync.WaitGroup
wg.Add(c)
log.Printf("concurrency: %d\nrequests per client: %d\n\n", c, m)
args := prepareArgs()
b, _ := goproto.Marshal(args)
log.Printf("message size: %d bytes\n\n", len(b))
var trans uint64
var transOK uint64
d := make([][]int64, c)
for i := 0; i < c; i++ {
dt := make([]int64, 0, m)
d = append(d, dt)
}
var cli proto.HelloClient
if *isWarden {
cli = wardenCli()
} else {
cli = grpcCli()
}
//warmup
cli.Say(context.Background(), args)
totalT := time.Now().UnixNano()
for i := 0; i < c; i++ {
go func(i int) {
for j := 0; j < m; j++ {
t := time.Now().UnixNano()
reply, err := cli.Say(context.Background(), args)
t = time.Now().UnixNano() - t
d[i] = append(d[i], t)
if err == nil && reply.Field1 == "OK" {
atomic.AddUint64(&transOK, 1)
}
atomic.AddUint64(&trans, 1)
}
wg.Done()
}(i)
}
wg.Wait()
totalT = time.Now().UnixNano() - totalT
totalT = totalT / 1e6
log.Printf("took %d ms for %d requests\n", totalT, *total)
totalD := make([]int64, 0, *total)
for _, k := range d {
totalD = append(totalD, k...)
}
totalD2 := make([]float64, 0, *total)
for _, k := range totalD {
totalD2 = append(totalD2, float64(k))
}
mean, _ := stats.Mean(totalD2)
median, _ := stats.Median(totalD2)
max, _ := stats.Max(totalD2)
min, _ := stats.Min(totalD2)
tp99, _ := stats.Percentile(totalD2, 99)
tp999, _ := stats.Percentile(totalD2, 99.9)
log.Printf("sent requests : %d\n", *total)
log.Printf("received requests_OK : %d\n", atomic.LoadUint64(&transOK))
log.Printf("throughput (TPS) : %d\n", int64(c*m)*1000/totalT)
log.Printf("mean: %v ms, median: %v ms, max: %v ms, min: %v ms, p99: %v ms, p999:%v ms\n", mean/1e6, median/1e6, max/1e6, min/1e6, tp99/1e6, tp999/1e6)
}
func prepareArgs() *proto.BenchmarkMessage {
b := true
var i int32 = 120000
var i64 int64 = 98765432101234
var s = "许多往事在眼前一幕一幕,变的那麼模糊"
repeat := *strLen / (8 * 54)
if repeat == 0 {
repeat = 1
}
var str string
for i := 0; i < repeat; i++ {
str += s
}
var args proto.BenchmarkMessage
v := reflect.ValueOf(&args).Elem()
num := v.NumField()
for k := 0; k < num; k++ {
field := v.Field(k)
if field.Type().Kind() == reflect.Ptr {
switch v.Field(k).Type().Elem().Kind() {
case reflect.Int, reflect.Int32:
field.Set(reflect.ValueOf(&i))
case reflect.Int64:
field.Set(reflect.ValueOf(&i64))
case reflect.Bool:
field.Set(reflect.ValueOf(&b))
case reflect.String:
field.Set(reflect.ValueOf(&str))
}
} else {
switch field.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
field.SetInt(9876543)
case reflect.Bool:
field.SetBool(true)
case reflect.String:
field.SetString(str)
}
}
}
return &args
}