forked from tsenart/vegeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
241 lines (205 loc) · 6.23 KB
/
results.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package vegeta
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/csv"
"encoding/gob"
"io"
"sort"
"strconv"
"time"
"github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
func init() {
gob.Register(&Result{})
}
// Result contains the results of a single Target hit.
type Result struct {
Attack string `json:"attack"`
Seq uint64 `json:"seq"`
Code uint16 `json:"code"`
Timestamp time.Time `json:"timestamp"`
Latency time.Duration `json:"latency"`
BytesOut uint64 `json:"bytes_out"`
BytesIn uint64 `json:"bytes_in"`
Error string `json:"error"`
Body []byte `json:"body"`
}
// End returns the time at which a Result ended.
func (r *Result) End() time.Time { return r.Timestamp.Add(r.Latency) }
// Equal returns true if the given Result is equal to the receiver.
func (r Result) Equal(other Result) bool {
return r.Attack == other.Attack &&
r.Seq == other.Seq &&
r.Code == other.Code &&
r.Timestamp.Equal(other.Timestamp) &&
r.Latency == other.Latency &&
r.BytesIn == other.BytesIn &&
r.BytesOut == other.BytesOut &&
r.Error == other.Error &&
bytes.Equal(r.Body, other.Body)
}
// Results is a slice of Result type elements.
type Results []Result
// Add implements the Add method of the Report interface by appending the given
// Result to the slice.
func (rs *Results) Add(r *Result) { *rs = append(*rs, *r) }
// Close implements the Close method of the Report interface by sorting the
// Results.
func (rs *Results) Close() { sort.Sort(rs) }
// The following methods implement sort.Interface
func (rs Results) Len() int { return len(rs) }
func (rs Results) Less(i, j int) bool { return rs[i].Timestamp.Before(rs[j].Timestamp) }
func (rs Results) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
// A Decoder decodes a Result and returns an error in case of failure.
type Decoder func(*Result) error
// A DecoderFactory constructs a new Decoder from a given io.Reader.
type DecoderFactory func(io.Reader) Decoder
// DecoderFor automatically detects the encoding of the first few bytes in
// the given io.Reader and then returns the corresponding Decoder or nil
// in case of failing to detect a supported encoding.
func DecoderFor(r io.Reader) Decoder {
var buf bytes.Buffer
for _, dec := range []DecoderFactory{
NewDecoder,
NewJSONDecoder,
NewCSVDecoder,
} {
rd := io.MultiReader(bytes.NewReader(buf.Bytes()), io.TeeReader(r, &buf))
if err := dec(rd).Decode(&Result{}); err == nil {
return dec(io.MultiReader(&buf, r))
}
}
return nil
}
// NewRoundRobinDecoder returns a new Decoder that round robins across the
// given Decoders on every invocation or decoding error.
func NewRoundRobinDecoder(dec ...Decoder) Decoder {
// Optimization for single Decoder case.
if len(dec) == 1 {
return dec[0]
}
var seq uint64
return func(r *Result) (err error) {
for range dec {
robin := seq % uint64(len(dec))
seq++
if err = dec[robin].Decode(r); err != nil {
continue
}
return nil
}
return err
}
}
// NewDecoder returns a new gob Decoder for the given io.Reader.
func NewDecoder(rd io.Reader) Decoder {
dec := gob.NewDecoder(rd)
return func(r *Result) error { return dec.Decode(r) }
}
// Decode is an an adapter method calling the Decoder function itself with the
// given parameters.
func (dec Decoder) Decode(r *Result) error { return dec(r) }
// An Encoder encodes a Result and returns an error in case of failure.
type Encoder func(*Result) error
// NewEncoder returns a new Result encoder closure for the given io.Writer
func NewEncoder(r io.Writer) Encoder {
enc := gob.NewEncoder(r)
return func(r *Result) error { return enc.Encode(r) }
}
// Encode is an an adapter method calling the Encoder function itself with the
// given parameters.
func (enc Encoder) Encode(r *Result) error { return enc(r) }
// NewCSVEncoder returns an Encoder that dumps the given *Result as a CSV
// record. The columns are: UNIX timestamp in ns since epoch,
// HTTP status code, request latency in ns, bytes out, bytes in,
// response body, and lastly the error.
func NewCSVEncoder(w io.Writer) Encoder {
enc := csv.NewWriter(w)
return func(r *Result) error {
err := enc.Write([]string{
strconv.FormatInt(r.Timestamp.UnixNano(), 10),
strconv.FormatUint(uint64(r.Code), 10),
strconv.FormatInt(r.Latency.Nanoseconds(), 10),
strconv.FormatUint(r.BytesOut, 10),
strconv.FormatUint(r.BytesIn, 10),
r.Error,
base64.StdEncoding.EncodeToString(r.Body),
r.Attack,
strconv.FormatUint(r.Seq, 10),
})
if err != nil {
return err
}
enc.Flush()
return enc.Error()
}
}
// NewCSVDecoder returns a Decoder that decodes CSV encoded Results.
func NewCSVDecoder(rd io.Reader) Decoder {
dec := csv.NewReader(rd)
dec.FieldsPerRecord = 9
dec.TrimLeadingSpace = true
return func(r *Result) error {
rec, err := dec.Read()
if err != nil {
return err
}
ts, err := strconv.ParseInt(rec[0], 10, 64)
if err != nil {
return err
}
r.Timestamp = time.Unix(0, ts)
code, err := strconv.ParseUint(rec[1], 10, 16)
if err != nil {
return err
}
r.Code = uint16(code)
latency, err := strconv.ParseInt(rec[2], 10, 64)
if err != nil {
return err
}
r.Latency = time.Duration(latency)
if r.BytesOut, err = strconv.ParseUint(rec[3], 10, 64); err != nil {
return err
}
if r.BytesIn, err = strconv.ParseUint(rec[4], 10, 64); err != nil {
return err
}
r.Error = rec[5]
r.Body, err = base64.StdEncoding.DecodeString(rec[6])
r.Attack = rec[7]
if r.Seq, err = strconv.ParseUint(rec[8], 10, 64); err != nil {
return err
}
return err
}
}
// NewJSONEncoder returns an Encoder that dumps the given *Results as a JSON
// object.
func NewJSONEncoder(w io.Writer) Encoder {
var jw jwriter.Writer
return func(r *Result) error {
(*jsonResult)(r).encode(&jw)
if jw.Error != nil {
return jw.Error
}
jw.RawByte('\n')
_, err := jw.DumpTo(w)
return err
}
}
// NewJSONDecoder returns a Decoder that decodes JSON encoded Results.
func NewJSONDecoder(r io.Reader) Decoder {
rd := bufio.NewReader(r)
return func(r *Result) (err error) {
var jl jlexer.Lexer
if jl.Data, err = rd.ReadSlice('\n'); err != nil {
return err
}
(*jsonResult)(r).decode(&jl)
return jl.Error()
}
}