Skip to content

Commit

Permalink
lib: Fix Close method for empty Metrics
Browse files Browse the repository at this point in the history
Fixes #461
  • Loading branch information
tsenart committed Jan 12, 2020
1 parent 0f0809e commit 7146527
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ func (m *Metrics) Add(r *Result) {
// Close implements the Close method of the Report interface by computing
// derived summary metrics which don't need to be run on every Add call.
func (m *Metrics) Close() {
if m.Requests == 0 {
return
}

m.init()

m.Rate = float64(m.Requests)
m.Throughput = float64(m.success)
m.Duration = m.Latest.Sub(m.Earliest)
Expand Down
13 changes: 13 additions & 0 deletions lib/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vegeta

import (
"io/ioutil"
"math/rand"
"reflect"
"testing"
Expand Down Expand Up @@ -98,6 +99,18 @@ func TestMetrics_NonNilErrorsOnClose(t *testing.T) {
}
}

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

var m Metrics
m.Close()

reporter := NewJSONReporter(&m)
if err := reporter(ioutil.Discard); err != nil {
t.Error(err)
}
}
func BenchmarkMetrics(b *testing.B) {
b.StopTimer()
b.ResetTimer()
Expand Down

0 comments on commit 7146527

Please sign in to comment.