Skip to content

Commit

Permalink
Correctly compute Metrics.Rate with sub-second duration results
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenart committed Dec 15, 2016
1 parent 895dd69 commit 89f25bb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ func (m *Metrics) Add(r *Result) {
// derived summary metrics which don't need to be run on every Add call.
func (m *Metrics) Close() {
m.init()
m.Rate = float64(m.Requests)
m.Duration = m.Latest.Sub(m.Earliest)
m.Rate = float64(m.Requests) / m.Duration.Seconds()
if secs := m.Duration.Seconds(); secs > 0 {
m.Rate /= secs
}
m.Wait = m.End.Sub(m.Latest)
m.BytesIn.Mean = float64(m.BytesIn.Total) / float64(m.Requests)
m.BytesOut.Mean = float64(m.BytesOut.Total) / float64(m.Requests)
Expand Down
12 changes: 12 additions & 0 deletions lib/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ func TestMetrics_Add(t *testing.T) {
t.Errorf("\ngot: %+v\nwant: %+v", got, want)
}
}

// https://github.com/tsenart/vegeta/issues/208
func TestMetrics_NoInfiniteRate(t *testing.T) {
t.Parallel()

m := Metrics{Requests: 1, Duration: time.Microsecond}
m.Close()

if got, want := m.Rate, 1.0; got != want {
t.Errorf("got rate %f, want %f", got, want)
}
}

0 comments on commit 89f25bb

Please sign in to comment.