-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
222 lines (206 loc) · 5.66 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package loaderbot
import (
"bytes"
"context"
"encoding/gob"
"io"
"log"
"sync/atomic"
"time"
"github.com/jinzhu/copier"
"google.golang.org/grpc"
)
type NodeClient struct {
conn *grpc.ClientConn
LoaderClient
stream Loader_RunClient
}
func NewNodeClient(addr string) *NodeClient {
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("failed to connect node %s: %v", addr, err)
}
return &NodeClient{
conn: conn,
LoaderClient: NewLoaderClient(conn),
stream: nil,
}
}
func (m *NodeClient) StartRunner(cluster *ClusterClient) {
var err error
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cluster.testCfg.TestTimeSec)*time.Second)
defer cancel()
nodeCfg := cluster.configToNodes()
m.stream, err = m.Run(ctx, &RunConfigRequest{
Config: MarshalConfigGob(nodeCfg),
AttackerName: "http",
})
if err != nil {
cluster.L.Fatal(err)
}
m.receive(ctx, cluster)
}
func (m *NodeClient) Shutdown() {
if _, err := m.LoaderClient.ShutdownNode(context.Background(), &ShutdownNodeRequest{}); err != nil {
log.Fatal(err)
}
}
func (m *NodeClient) Close() {
m.conn.Close()
}
func (m *NodeClient) receive(_ context.Context, cluster *ClusterClient) {
for {
res, err := m.stream.Recv()
if err == io.EOF || res == nil {
atomic.AddInt32(&cluster.activeClients, -1)
return
}
if err != nil {
atomic.AddInt32(&cluster.activeClients, -1)
cluster.L.Error(err)
}
b := bytes.NewBuffer(res.ResultsChunk)
dec := gob.NewDecoder(b)
var results []AttackResult
if err := dec.Decode(&results); err != nil {
cluster.L.Fatal(err)
}
cluster.Results <- results
}
}
type ClusterClient struct {
TimeoutCtx context.Context
CancelFunc context.CancelFunc
failed bool
testCfg *RunnerConfig
activeClients int32
clients []*NodeClient
Results chan []AttackResult
clusterTickMetrics map[int]*ClusterTickMetrics
Report *Report
L *Logger
}
func NewClusterClient(cfg *RunnerConfig) *ClusterClient {
clients := make([]*NodeClient, 0)
var failed bool
for _, addr := range cfg.ClusterOptions.Nodes {
c := NewNodeClient(addr)
res, err := c.Status(context.Background(), &StatusRequest{})
if err != nil {
log.Fatal(err)
}
if res.Busy {
log.Printf(errNodeIsBusy, addr)
failed = true
}
clients = append(clients, c)
}
c := &ClusterClient{
testCfg: cfg,
clients: clients,
Results: make(chan []AttackResult),
clusterTickMetrics: make(map[int]*ClusterTickMetrics),
failed: failed,
L: NewLogger(cfg).With("cluster", cfg.Name),
}
if cfg.ReportOptions.CSV {
c.Report = NewReport(cfg)
}
return c
}
func (m *ClusterClient) configToNodes() *RunnerConfig {
var nodeTestCfg RunnerConfig
if err := copier.Copy(&nodeTestCfg, &m.testCfg); err != nil {
m.L.Fatal(err)
}
// no need to write logs on nodes in cluster mode
nodeTestCfg.ReportOptions.CSV = false
nodeTestCfg.ReportOptions.PNG = false
// split start/step rps by nodes equally
nodeTestCfg.Attackers = nodeTestCfg.Attackers / len(nodeTestCfg.ClusterOptions.Nodes)
nodeTestCfg.StartRPS = nodeTestCfg.StartRPS / len(nodeTestCfg.ClusterOptions.Nodes)
nodeTestCfg.StepRPS = nodeTestCfg.StepRPS / len(nodeTestCfg.ClusterOptions.Nodes)
return &nodeTestCfg
}
func (m *ClusterClient) CheckBusy() bool {
res, err := m.clients[0].Status(context.Background(), &StatusRequest{})
if err != nil {
m.L.Error(err)
}
return res.Busy
}
func (m *ClusterClient) Run() {
for _, c := range m.clients {
atomic.AddInt32(&m.activeClients, 1)
go c.StartRunner(m)
}
m.collectResults()
for _, c := range m.clients {
c.Close()
}
if m.testCfg.ReportOptions.CSV {
m.Report.flushLogs()
m.Report.plot()
}
}
func (m *ClusterClient) collectResults() {
for {
select {
case res := <-m.Results:
token := res[0].AttackToken
tick := res[0].AttackToken.Tick
if _, ok := m.clusterTickMetrics[tick]; !ok {
m.clusterTickMetrics[tick] = &ClusterTickMetrics{
Samples: make([][]AttackResult, 0),
Metrics: NewMetrics(),
}
}
currentTickMetrics := m.clusterTickMetrics[tick]
currentTickMetrics.Samples = append(currentTickMetrics.Samples, res)
if len(currentTickMetrics.Samples) == len(m.testCfg.ClusterOptions.Nodes) {
// aggregate over all ticks across cluster
for _, sampleBatch := range currentTickMetrics.Samples {
for _, s := range sampleBatch {
currentTickMetrics.Metrics.add(s)
}
}
currentTickMetrics.Metrics.update()
m.L.Infof(
"step: %d, tick: %d, rate [%4f -> %v], perc: 50 [%v] 95 [%v] 99 [%v], # requests [%d], %% success [%d]",
token.Step,
tick,
currentTickMetrics.Metrics.Rate,
token.TargetRPS*len(m.testCfg.ClusterOptions.Nodes),
currentTickMetrics.Metrics.Latencies.P50,
currentTickMetrics.Metrics.Latencies.P95,
currentTickMetrics.Metrics.Latencies.P99,
currentTickMetrics.Metrics.Requests,
currentTickMetrics.Metrics.successLogEntry(),
)
if m.testCfg.ReportOptions.CSV {
m.Report.writePercentilesEntry(res[0], currentTickMetrics.Metrics)
}
// shutdown other Nodes if error is present in tick
if m.shutdownOnNodeSampleError(currentTickMetrics.Metrics) {
return
}
}
default:
if atomic.LoadInt32(&m.activeClients) == 0 {
m.L.Infof("all nodes exited, test ended")
return
}
}
}
}
func (m *ClusterClient) shutdownOnNodeSampleError(metrics *Metrics) bool {
if metrics.Success < m.testCfg.SuccessRatio {
for idx, c := range m.clients {
m.L.Infof("shutting down runner: %d", idx)
c.Shutdown()
}
m.failed = true
return true
}
return false
}