-
Notifications
You must be signed in to change notification settings - Fork 43
/
client.go
213 lines (178 loc) · 4.95 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
package main
import (
"context"
"fmt"
"io"
"math"
"sync"
"time"
"golang.org/x/sync/errgroup"
)
type clientStats struct {
Concurrency int // Divide total time by concurrency to get rps
mu sync.Mutex
numTotal int // Number of requests
numErrors int // Number of errors
timeErrors time.Duration // Duration of error responses specifically
errors map[string]int
timing histogram
}
func (stats *clientStats) Count(err error, elapsed time.Duration) {
stats.mu.Lock()
defer stats.mu.Unlock()
stats.numTotal += 1
stats.timing.Add(elapsed.Seconds())
if err != nil {
stats.numErrors += 1
stats.timeErrors += elapsed
if stats.errors == nil {
stats.errors = map[string]int{}
}
stats.errors[err.Error()] += 1
}
}
func (stats *clientStats) Render(w io.Writer) error {
// TODO: Use templating?
// TODO: Support JSON
if stats.numTotal == 0 {
fmt.Fprintf(w, " No requests.")
}
var errRate, rps float64
concurrency := 1
if stats.Concurrency > 0 {
concurrency = stats.Concurrency
}
errRate = float64(stats.numErrors*100) / float64(stats.numTotal)
rps = float64(stats.numTotal*concurrency) / stats.timing.Total()
fmt.Fprintf(w, "\n Requests: %0.2f per second", rps)
if stats.numErrors > 0 && stats.numErrors != stats.numTotal {
errAvg := float64(stats.numErrors) / stats.timeErrors.Seconds()
fmt.Fprintf(w, ", %0.2f per second for errors", errAvg)
}
variance := stats.timing.Variance()
stddev := math.Sqrt(variance)
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, " Timing: %0.4fs avg, %0.4fs min, %0.4fs max\n", stats.timing.Average(), stats.timing.Min(), stats.timing.Max())
fmt.Fprintf(w, " %0.4fs standard deviation\n", stddev)
fmt.Fprintf(w, "\n Percentiles:\n")
buckets := []int{25, 50, 75, 90, 95, 99}
percentiles := stats.timing.Percentiles(buckets...)
for i, bucket := range buckets {
fmt.Fprintf(w, " %d%% in %0.4fs\n", bucket, percentiles[i])
}
fmt.Fprintf(w, "\n Errors: %0.2f%%\n", errRate)
for msg, num := range stats.errors {
fmt.Fprintf(w, " %d × %q\n", num, msg)
}
return nil
}
func NewClient(endpoint string, concurrency int) (*Client, error) {
c := Client{
Endpoint: endpoint,
Concurrency: concurrency,
In: make(chan Request, 2*concurrency),
}
c.Stats.Concurrency = concurrency
return &c, nil
}
func NewClients(endpoints []string, concurrency int, timeout time.Duration) (Clients, error) {
clients := make(Clients, 0, len(endpoints))
for _, endpoint := range endpoints {
c, err := NewClient(endpoint, concurrency)
if err != nil {
return nil, err
}
c.Timeout = timeout
clients = append(clients, c)
}
return clients, nil
}
type Client struct {
Endpoint string
Concurrency int // Number of goroutines to make requests with. Must be >=1.
Timeout time.Duration // Timeout of each request
In chan Request
Stats clientStats
}
func (client *Client) Handle(req Request) {
client.In <- req
}
// Serve starts the async request and response goroutine consumers.
func (client *Client) Serve(ctx context.Context, out chan<- Response) error {
g, ctx := errgroup.WithContext(ctx)
if client.Concurrency < 1 {
client.Concurrency = 1
}
logger.Debug().Str("endpoint", client.Endpoint).Int("concurrency", client.Concurrency).Msg("starting client")
for i := 0; i < client.Concurrency; i++ {
g.Go(func() error {
// Consume requests
t, err := NewTransport(client.Endpoint, client.Timeout)
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
logger.Debug().Str("endpoint", client.Endpoint).Msg("aborting client")
return nil
case req := <-client.In:
if req.ID == -1 {
// Final request received, shutdown
logger.Debug().Str("endpoint", client.Endpoint).Msg("received final request, shutting down")
return nil
}
resp := req.Do(t)
client.Stats.Count(resp.Err, resp.Elapsed)
select {
case out <- resp:
default:
logger.Warn().Msg("response channel is overloaded, please open an issue")
out <- resp
}
}
}
})
}
return g.Wait()
}
var id requestID
type Clients []*Client
// Finalize sends a request with ID -1 which signals the end of the stream, so
// serving will end cleanly.
func (c Clients) Finalize() {
for _, client := range c {
for i := 0; i < client.Concurrency; i++ {
// Signal each client instance to shut down
client.In <- Request{
ID: -1,
}
}
}
}
func (c Clients) Send(ctx context.Context, line []byte) error {
id += 1
for _, client := range c {
select {
case client.In <- Request{
client: client,
ID: id,
Line: line,
Timestamp: time.Now(),
}:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
}
func (clients Clients) Serve(ctx context.Context, out chan Response) error {
g, ctx := errgroup.WithContext(ctx)
for _, c := range clients {
c := c // Otherwise c gets mutated per iteration and we get a race
g.Go(func() error {
return c.Serve(ctx, out)
})
}
return g.Wait()
}