Skip to content

Commit

Permalink
Add URL and Method to Result struct (tsenart#438)
Browse files Browse the repository at this point in the history
This change set makes vegeta capture the URL and Method of a Target
that's been hit in the Result.

Fixes tsenart#437
  • Loading branch information
tsenart authored Sep 17, 2019
1 parent 70ac8c7 commit ab06ddb
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 91 deletions.
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module github.com/tsenart/vegeta

go 1.13

require (
github.com/alecthomas/jsonschema v0.0.0-20180308105923-f2c93856175a
github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b
github.com/c2h5oh/datasize v0.0.0-20171227191756-4eba002a5eae
github.com/dgryski/go-gk v0.0.0-20140819190930-201884a44051
github.com/dgryski/go-lttb v0.0.0-20180810165845-318fcdf10a77
github.com/google/go-cmp v0.2.0
github.com/influxdata/tdigest v0.0.0-20180711151920-a7d76c6f093a
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329
github.com/miekg/dns v1.1.17
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371
github.com/shurcooL/vfsgen v0.0.0-20180711163814-62bca832be04
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25
github.com/tsenart/go-tsz v0.0.0-20180814232043-cdeb9e1e981e
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
golang.org/x/text v0.3.2
)
3 changes: 3 additions & 0 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ func (a *Attacker) hit(tr Targeter, name string) *Result {
return &res
}

res.Method = tgt.Method
res.URL = tgt.URL

req, err := tgt.Request()
if err != nil {
return &res
Expand Down
28 changes: 20 additions & 8 deletions lib/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Result struct {
BytesIn uint64 `json:"bytes_in"`
Error string `json:"error"`
Body []byte `json:"body"`
Method string `json:"method"`
URL string `json:"url"`
}

// End returns the time at which a Result ended.
Expand All @@ -45,7 +47,9 @@ func (r Result) Equal(other Result) bool {
r.BytesIn == other.BytesIn &&
r.BytesOut == other.BytesOut &&
r.Error == other.Error &&
bytes.Equal(r.Body, other.Body)
bytes.Equal(r.Body, other.Body) &&
r.Method == other.Method &&
r.URL == other.URL
}

// Results is a slice of Result type elements.
Expand Down Expand Up @@ -150,8 +154,9 @@ func NewCSVEncoder(w io.Writer) Encoder {
base64.StdEncoding.EncodeToString(r.Body),
r.Attack,
strconv.FormatUint(r.Seq, 10),
r.Method,
r.URL,
})

if err != nil {
return err
}
Expand All @@ -163,9 +168,9 @@ func NewCSVEncoder(w io.Writer) Encoder {
}

// NewCSVDecoder returns a Decoder that decodes CSV encoded Results.
func NewCSVDecoder(rd io.Reader) Decoder {
dec := csv.NewReader(rd)
dec.FieldsPerRecord = 9
func NewCSVDecoder(r io.Reader) Decoder {
dec := csv.NewReader(r)
dec.FieldsPerRecord = 11
dec.TrimLeadingSpace = true

return func(r *Result) error {
Expand Down Expand Up @@ -208,16 +213,23 @@ func NewCSVDecoder(rd io.Reader) Decoder {
return err
}

r.Method = rec[9]
r.URL = rec[10]

return err
}
}

//go:generate easyjson -no_std_marshalers -output_filename results_easyjson.go results.go
//easyjson:json
type jsonResult Result

// 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)
(*jsonResult)(r).MarshalEasyJSON(&jw)
if jw.Error != nil {
return jw.Error
}
Expand All @@ -232,10 +244,10 @@ 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 {
if jl.Data, err = rd.ReadBytes('\n'); err != nil {
return err
}
(*jsonResult)(r).decode(&jl)
(*jsonResult)(r).UnmarshalEasyJSON(&jl)
return jl.Error()
}
}
150 changes: 67 additions & 83 deletions lib/results_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab06ddb

Please sign in to comment.