Skip to content

Commit

Permalink
attack: Measure latency when requests timeout (tsenart#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenart authored May 12, 2019
1 parent d48d090 commit 0f5577e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,19 @@ func (a *Attacker) hit(tr Targeter, name string) *Result {
err error
)

defer func() {
if err != nil {
res.Error = err.Error()
}
}()

a.seqmu.Lock()
res.Timestamp = a.began.Add(time.Since(a.began))
res.Seq = a.seq
a.seq++
a.seqmu.Unlock()

defer func() {
res.Latency = time.Since(res.Timestamp)
if err != nil {
res.Error = err.Error()
}
}()

if err = tr(&tgt); err != nil {
a.Stop()
return &res
Expand Down Expand Up @@ -324,7 +325,6 @@ func (a *Attacker) hit(tr Targeter, name string) *Result {
return &res
}

res.Latency = time.Since(res.Timestamp)
res.BytesIn = uint64(len(res.Body))

if req.ContentLength != -1 {
Expand Down
5 changes: 5 additions & 0 deletions lib/attack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ func TestTimeout(t *testing.T) {
atk := NewAttacker(Timeout(10 * time.Millisecond))
tr := NewStaticTargeter(Target{Method: "GET", URL: server.URL})
res := atk.hit(tr, "")

want := "Client.Timeout exceeded while awaiting headers"
if got := res.Error; !strings.Contains(got, want) {
t.Fatalf("want: '%v' in '%v'", want, got)
}

if res.Latency == 0 {
t.Fatal("Latency wasn't captured with a timed-out result")
}
}

func TestLocalAddr(t *testing.T) {
Expand Down

0 comments on commit 0f5577e

Please sign in to comment.