metrics: fix issues reported by staticcheck (#20365)

pull/20375/head
Guillaume Ballet 5 years ago committed by Felix Lange
parent dd21f079e8
commit 58f2ce8671
  1. 16
      metrics/counter_test.go
  2. 2
      metrics/gauge_float64_test.go
  3. 8
      metrics/gauge_test.go
  4. 34
      metrics/histogram_test.go
  5. 6
      metrics/influxdb/influxdb.go
  6. 2
      metrics/json_test.go
  7. 2
      metrics/librato/client.go
  8. 5
      metrics/librato/librato.go
  9. 10
      metrics/meter_test.go
  10. 22
      metrics/registry_test.go
  11. 8
      metrics/runtime_test.go
  12. 8
      metrics/sample.go
  13. 54
      metrics/sample_test.go
  14. 5
      metrics/timer.go
  15. 32
      metrics/timer_test.go

@ -14,7 +14,7 @@ func TestCounterClear(t *testing.T) {
c := NewCounter() c := NewCounter()
c.Inc(1) c.Inc(1)
c.Clear() c.Clear()
if count := c.Count(); 0 != count { if count := c.Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count) t.Errorf("c.Count(): 0 != %v\n", count)
} }
} }
@ -22,7 +22,7 @@ func TestCounterClear(t *testing.T) {
func TestCounterDec1(t *testing.T) { func TestCounterDec1(t *testing.T) {
c := NewCounter() c := NewCounter()
c.Dec(1) c.Dec(1)
if count := c.Count(); -1 != count { if count := c.Count(); count != -1 {
t.Errorf("c.Count(): -1 != %v\n", count) t.Errorf("c.Count(): -1 != %v\n", count)
} }
} }
@ -30,7 +30,7 @@ func TestCounterDec1(t *testing.T) {
func TestCounterDec2(t *testing.T) { func TestCounterDec2(t *testing.T) {
c := NewCounter() c := NewCounter()
c.Dec(2) c.Dec(2)
if count := c.Count(); -2 != count { if count := c.Count(); count != -2 {
t.Errorf("c.Count(): -2 != %v\n", count) t.Errorf("c.Count(): -2 != %v\n", count)
} }
} }
@ -38,7 +38,7 @@ func TestCounterDec2(t *testing.T) {
func TestCounterInc1(t *testing.T) { func TestCounterInc1(t *testing.T) {
c := NewCounter() c := NewCounter()
c.Inc(1) c.Inc(1)
if count := c.Count(); 1 != count { if count := c.Count(); count != 1 {
t.Errorf("c.Count(): 1 != %v\n", count) t.Errorf("c.Count(): 1 != %v\n", count)
} }
} }
@ -46,7 +46,7 @@ func TestCounterInc1(t *testing.T) {
func TestCounterInc2(t *testing.T) { func TestCounterInc2(t *testing.T) {
c := NewCounter() c := NewCounter()
c.Inc(2) c.Inc(2)
if count := c.Count(); 2 != count { if count := c.Count(); count != 2 {
t.Errorf("c.Count(): 2 != %v\n", count) t.Errorf("c.Count(): 2 != %v\n", count)
} }
} }
@ -56,14 +56,14 @@ func TestCounterSnapshot(t *testing.T) {
c.Inc(1) c.Inc(1)
snapshot := c.Snapshot() snapshot := c.Snapshot()
c.Inc(1) c.Inc(1)
if count := snapshot.Count(); 1 != count { if count := snapshot.Count(); count != 1 {
t.Errorf("c.Count(): 1 != %v\n", count) t.Errorf("c.Count(): 1 != %v\n", count)
} }
} }
func TestCounterZero(t *testing.T) { func TestCounterZero(t *testing.T) {
c := NewCounter() c := NewCounter()
if count := c.Count(); 0 != count { if count := c.Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count) t.Errorf("c.Count(): 0 != %v\n", count)
} }
} }
@ -71,7 +71,7 @@ func TestCounterZero(t *testing.T) {
func TestGetOrRegisterCounter(t *testing.T) { func TestGetOrRegisterCounter(t *testing.T) {
r := NewRegistry() r := NewRegistry()
NewRegisteredCounter("foo", r).Inc(47) NewRegisteredCounter("foo", r).Inc(47)
if c := GetOrRegisterCounter("foo", r); 47 != c.Count() { if c := GetOrRegisterCounter("foo", r); c.Count() != 47 {
t.Fatal(c) t.Fatal(c)
} }
} }

@ -53,7 +53,7 @@ func TestFunctionalGaugeFloat64(t *testing.T) {
func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) { func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) {
r := NewRegistry() r := NewRegistry()
NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 }) NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 })
if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() { if g := GetOrRegisterGaugeFloat64("foo", r); g.Value() != 47 {
t.Fatal(g) t.Fatal(g)
} }
} }

@ -16,7 +16,7 @@ func BenchmarkGuage(b *testing.B) {
func TestGauge(t *testing.T) { func TestGauge(t *testing.T) {
g := NewGauge() g := NewGauge()
g.Update(int64(47)) g.Update(int64(47))
if v := g.Value(); 47 != v { if v := g.Value(); v != 47 {
t.Errorf("g.Value(): 47 != %v\n", v) t.Errorf("g.Value(): 47 != %v\n", v)
} }
} }
@ -26,7 +26,7 @@ func TestGaugeSnapshot(t *testing.T) {
g.Update(int64(47)) g.Update(int64(47))
snapshot := g.Snapshot() snapshot := g.Snapshot()
g.Update(int64(0)) g.Update(int64(0))
if v := snapshot.Value(); 47 != v { if v := snapshot.Value(); v != 47 {
t.Errorf("g.Value(): 47 != %v\n", v) t.Errorf("g.Value(): 47 != %v\n", v)
} }
} }
@ -34,7 +34,7 @@ func TestGaugeSnapshot(t *testing.T) {
func TestGetOrRegisterGauge(t *testing.T) { func TestGetOrRegisterGauge(t *testing.T) {
r := NewRegistry() r := NewRegistry()
NewRegisteredGauge("foo", r).Update(47) NewRegisteredGauge("foo", r).Update(47)
if g := GetOrRegisterGauge("foo", r); 47 != g.Value() { if g := GetOrRegisterGauge("foo", r); g.Value() != 47 {
t.Fatal(g) t.Fatal(g)
} }
} }
@ -55,7 +55,7 @@ func TestFunctionalGauge(t *testing.T) {
func TestGetOrRegisterFunctionalGauge(t *testing.T) { func TestGetOrRegisterFunctionalGauge(t *testing.T) {
r := NewRegistry() r := NewRegistry()
NewRegisteredFunctionalGauge("foo", r, func() int64 { return 47 }) NewRegisteredFunctionalGauge("foo", r, func() int64 { return 47 })
if g := GetOrRegisterGauge("foo", r); 47 != g.Value() { if g := GetOrRegisterGauge("foo", r); g.Value() != 47 {
t.Fatal(g) t.Fatal(g)
} }
} }

@ -14,7 +14,7 @@ func TestGetOrRegisterHistogram(t *testing.T) {
r := NewRegistry() r := NewRegistry()
s := NewUniformSample(100) s := NewUniformSample(100)
NewRegisteredHistogram("foo", r, s).Update(47) NewRegisteredHistogram("foo", r, s).Update(47)
if h := GetOrRegisterHistogram("foo", r, s); 1 != h.Count() { if h := GetOrRegisterHistogram("foo", r, s); h.Count() != 1 {
t.Fatal(h) t.Fatal(h)
} }
} }
@ -29,29 +29,29 @@ func TestHistogram10000(t *testing.T) {
func TestHistogramEmpty(t *testing.T) { func TestHistogramEmpty(t *testing.T) {
h := NewHistogram(NewUniformSample(100)) h := NewHistogram(NewUniformSample(100))
if count := h.Count(); 0 != count { if count := h.Count(); count != 0 {
t.Errorf("h.Count(): 0 != %v\n", count) t.Errorf("h.Count(): 0 != %v\n", count)
} }
if min := h.Min(); 0 != min { if min := h.Min(); min != 0 {
t.Errorf("h.Min(): 0 != %v\n", min) t.Errorf("h.Min(): 0 != %v\n", min)
} }
if max := h.Max(); 0 != max { if max := h.Max(); max != 0 {
t.Errorf("h.Max(): 0 != %v\n", max) t.Errorf("h.Max(): 0 != %v\n", max)
} }
if mean := h.Mean(); 0.0 != mean { if mean := h.Mean(); mean != 0.0 {
t.Errorf("h.Mean(): 0.0 != %v\n", mean) t.Errorf("h.Mean(): 0.0 != %v\n", mean)
} }
if stdDev := h.StdDev(); 0.0 != stdDev { if stdDev := h.StdDev(); stdDev != 0.0 {
t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev) t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev)
} }
ps := h.Percentiles([]float64{0.5, 0.75, 0.99}) ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
if 0.0 != ps[0] { if ps[0] != 0.0 {
t.Errorf("median: 0.0 != %v\n", ps[0]) t.Errorf("median: 0.0 != %v\n", ps[0])
} }
if 0.0 != ps[1] { if ps[1] != 0.0 {
t.Errorf("75th percentile: 0.0 != %v\n", ps[1]) t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
} }
if 0.0 != ps[2] { if ps[2] != 0.0 {
t.Errorf("99th percentile: 0.0 != %v\n", ps[2]) t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
} }
} }
@ -67,29 +67,29 @@ func TestHistogramSnapshot(t *testing.T) {
} }
func testHistogram10000(t *testing.T, h Histogram) { func testHistogram10000(t *testing.T, h Histogram) {
if count := h.Count(); 10000 != count { if count := h.Count(); count != 10000 {
t.Errorf("h.Count(): 10000 != %v\n", count) t.Errorf("h.Count(): 10000 != %v\n", count)
} }
if min := h.Min(); 1 != min { if min := h.Min(); min != 1 {
t.Errorf("h.Min(): 1 != %v\n", min) t.Errorf("h.Min(): 1 != %v\n", min)
} }
if max := h.Max(); 10000 != max { if max := h.Max(); max != 10000 {
t.Errorf("h.Max(): 10000 != %v\n", max) t.Errorf("h.Max(): 10000 != %v\n", max)
} }
if mean := h.Mean(); 5000.5 != mean { if mean := h.Mean(); mean != 5000.5 {
t.Errorf("h.Mean(): 5000.5 != %v\n", mean) t.Errorf("h.Mean(): 5000.5 != %v\n", mean)
} }
if stdDev := h.StdDev(); 2886.751331514372 != stdDev { if stdDev := h.StdDev(); stdDev != 2886.751331514372 {
t.Errorf("h.StdDev(): 2886.751331514372 != %v\n", stdDev) t.Errorf("h.StdDev(): 2886.751331514372 != %v\n", stdDev)
} }
ps := h.Percentiles([]float64{0.5, 0.75, 0.99}) ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
if 5000.5 != ps[0] { if ps[0] != 5000.5 {
t.Errorf("median: 5000.5 != %v\n", ps[0]) t.Errorf("median: 5000.5 != %v\n", ps[0])
} }
if 7500.75 != ps[1] { if ps[1] != 7500.75 {
t.Errorf("75th percentile: 7500.75 != %v\n", ps[1]) t.Errorf("75th percentile: 7500.75 != %v\n", ps[1])
} }
if 9900.99 != ps[2] { if ps[2] != 9900.99 {
t.Errorf("99th percentile: 9900.99 != %v\n", ps[2]) t.Errorf("99th percentile: 9900.99 != %v\n", ps[2])
} }
} }

@ -62,7 +62,7 @@ func InfluxDBWithTags(r metrics.Registry, d time.Duration, url, database, userna
func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password, namespace string, tags map[string]string) error { func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password, namespace string, tags map[string]string) error {
u, err := uurl.Parse(url) u, err := uurl.Parse(url)
if err != nil { if err != nil {
return fmt.Errorf("Unable to parse InfluxDB. url: %s, err: %v", url, err) return fmt.Errorf("unable to parse InfluxDB. url: %s, err: %v", url, err)
} }
rep := &reporter{ rep := &reporter{
@ -76,11 +76,11 @@ func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password,
cache: make(map[string]int64), cache: make(map[string]int64),
} }
if err := rep.makeClient(); err != nil { if err := rep.makeClient(); err != nil {
return fmt.Errorf("Unable to make InfluxDB client. err: %v", err) return fmt.Errorf("unable to make InfluxDB client. err: %v", err)
} }
if err := rep.send(); err != nil { if err := rep.send(); err != nil {
return fmt.Errorf("Unable to send to InfluxDB. err: %v", err) return fmt.Errorf("unable to send to InfluxDB. err: %v", err)
} }
return nil return nil

@ -12,7 +12,7 @@ func TestRegistryMarshallJSON(t *testing.T) {
r := NewRegistry() r := NewRegistry()
r.Register("counter", NewCounter()) r.Register("counter", NewCounter())
enc.Encode(r) enc.Encode(r)
if s := b.String(); "{\"counter\":{\"count\":0}}\n" != s { if s := b.String(); s != "{\"counter\":{\"count\":0}}\n" {
t.Fatalf(s) t.Fatalf(s)
} }
} }

@ -96,7 +96,7 @@ func (c *LibratoClient) PostMetrics(batch Batch) (err error) {
if body, err = ioutil.ReadAll(resp.Body); err != nil { if body, err = ioutil.ReadAll(resp.Body); err != nil {
body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err)) body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err))
} }
err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body)) err = fmt.Errorf("unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
} }
return return
} }

@ -42,9 +42,10 @@ func Librato(r metrics.Registry, d time.Duration, e string, t string, s string,
func (rep *Reporter) Run() { func (rep *Reporter) Run() {
log.Printf("WARNING: This client has been DEPRECATED! It has been moved to https://github.com/mihasya/go-metrics-librato and will be removed from rcrowley/go-metrics on August 5th 2015") log.Printf("WARNING: This client has been DEPRECATED! It has been moved to https://github.com/mihasya/go-metrics-librato and will be removed from rcrowley/go-metrics on August 5th 2015")
ticker := time.Tick(rep.Interval) ticker := time.NewTicker(rep.Interval)
defer ticker.Stop()
metricsApi := &LibratoClient{rep.Email, rep.Token} metricsApi := &LibratoClient{rep.Email, rep.Token}
for now := range ticker { for now := range ticker.C {
var metrics Batch var metrics Batch
var err error var err error
if metrics, err = rep.BuildRequest(now, rep.Registry); err != nil { if metrics, err = rep.BuildRequest(now, rep.Registry); err != nil {

@ -16,7 +16,7 @@ func BenchmarkMeter(b *testing.B) {
func TestGetOrRegisterMeter(t *testing.T) { func TestGetOrRegisterMeter(t *testing.T) {
r := NewRegistry() r := NewRegistry()
NewRegisteredMeter("foo", r).Mark(47) NewRegisteredMeter("foo", r).Mark(47)
if m := GetOrRegisterMeter("foo", r); 47 != m.Count() { if m := GetOrRegisterMeter("foo", r); m.Count() != 47 {
t.Fatal(m) t.Fatal(m)
} }
} }
@ -40,7 +40,7 @@ func TestMeterDecay(t *testing.T) {
func TestMeterNonzero(t *testing.T) { func TestMeterNonzero(t *testing.T) {
m := NewMeter() m := NewMeter()
m.Mark(3) m.Mark(3)
if count := m.Count(); 3 != count { if count := m.Count(); count != 3 {
t.Errorf("m.Count(): 3 != %v\n", count) t.Errorf("m.Count(): 3 != %v\n", count)
} }
} }
@ -48,11 +48,11 @@ func TestMeterNonzero(t *testing.T) {
func TestMeterStop(t *testing.T) { func TestMeterStop(t *testing.T) {
l := len(arbiter.meters) l := len(arbiter.meters)
m := NewMeter() m := NewMeter()
if len(arbiter.meters) != l+1 { if l+1 != len(arbiter.meters) {
t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters)) t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
} }
m.Stop() m.Stop()
if len(arbiter.meters) != l { if l != len(arbiter.meters) {
t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters)) t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
} }
} }
@ -67,7 +67,7 @@ func TestMeterSnapshot(t *testing.T) {
func TestMeterZero(t *testing.T) { func TestMeterZero(t *testing.T) {
m := NewMeter() m := NewMeter()
if count := m.Count(); 0 != count { if count := m.Count(); count != 0 {
t.Errorf("m.Count(): 0 != %v\n", count) t.Errorf("m.Count(): 0 != %v\n", count)
} }
} }

@ -19,20 +19,20 @@ func TestRegistry(t *testing.T) {
i := 0 i := 0
r.Each(func(name string, iface interface{}) { r.Each(func(name string, iface interface{}) {
i++ i++
if "foo" != name { if name != "foo" {
t.Fatal(name) t.Fatal(name)
} }
if _, ok := iface.(Counter); !ok { if _, ok := iface.(Counter); !ok {
t.Fatal(iface) t.Fatal(iface)
} }
}) })
if 1 != i { if i != 1 {
t.Fatal(i) t.Fatal(i)
} }
r.Unregister("foo") r.Unregister("foo")
i = 0 i = 0
r.Each(func(string, interface{}) { i++ }) r.Each(func(string, interface{}) { i++ })
if 0 != i { if i != 0 {
t.Fatal(i) t.Fatal(i)
} }
} }
@ -52,7 +52,7 @@ func TestRegistryDuplicate(t *testing.T) {
t.Fatal(iface) t.Fatal(iface)
} }
}) })
if 1 != i { if i != 1 {
t.Fatal(i) t.Fatal(i)
} }
} }
@ -60,11 +60,11 @@ func TestRegistryDuplicate(t *testing.T) {
func TestRegistryGet(t *testing.T) { func TestRegistryGet(t *testing.T) {
r := NewRegistry() r := NewRegistry()
r.Register("foo", NewCounter()) r.Register("foo", NewCounter())
if count := r.Get("foo").(Counter).Count(); 0 != count { if count := r.Get("foo").(Counter).Count(); count != 0 {
t.Fatal(count) t.Fatal(count)
} }
r.Get("foo").(Counter).Inc(1) r.Get("foo").(Counter).Inc(1)
if count := r.Get("foo").(Counter).Count(); 1 != count { if count := r.Get("foo").(Counter).Count(); count != 1 {
t.Fatal(count) t.Fatal(count)
} }
} }
@ -271,6 +271,9 @@ func TestChildPrefixedRegistryOfChildRegister(t *testing.T) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
err = r2.Register("baz", NewCounter()) err = r2.Register("baz", NewCounter())
if err != nil {
t.Fatal(err.Error())
}
c := NewCounter() c := NewCounter()
Register("bars", c) Register("bars", c)
@ -278,7 +281,7 @@ func TestChildPrefixedRegistryOfChildRegister(t *testing.T) {
r2.Each(func(name string, m interface{}) { r2.Each(func(name string, m interface{}) {
i++ i++
if name != "prefix.prefix2.baz" { if name != "prefix.prefix2.baz" {
//t.Fatal(name) t.Fatal(name)
} }
}) })
if i != 1 { if i != 1 {
@ -294,11 +297,14 @@ func TestWalkRegistries(t *testing.T) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
err = r2.Register("baz", NewCounter()) err = r2.Register("baz", NewCounter())
if err != nil {
t.Fatal(err.Error())
}
c := NewCounter() c := NewCounter()
Register("bars", c) Register("bars", c)
_, prefix := findPrefix(r2, "") _, prefix := findPrefix(r2, "")
if "prefix.prefix2." != prefix { if prefix != "prefix.prefix2." {
t.Fatal(prefix) t.Fatal(prefix)
} }

@ -22,27 +22,27 @@ func TestRuntimeMemStats(t *testing.T) {
zero := runtimeMetrics.MemStats.PauseNs.Count() // Get a "zero" since GC may have run before these tests. zero := runtimeMetrics.MemStats.PauseNs.Count() // Get a "zero" since GC may have run before these tests.
runtime.GC() runtime.GC()
CaptureRuntimeMemStatsOnce(r) CaptureRuntimeMemStatsOnce(r)
if count := runtimeMetrics.MemStats.PauseNs.Count(); 1 != count-zero { if count := runtimeMetrics.MemStats.PauseNs.Count(); count-zero != 1 {
t.Fatal(count - zero) t.Fatal(count - zero)
} }
runtime.GC() runtime.GC()
runtime.GC() runtime.GC()
CaptureRuntimeMemStatsOnce(r) CaptureRuntimeMemStatsOnce(r)
if count := runtimeMetrics.MemStats.PauseNs.Count(); 3 != count-zero { if count := runtimeMetrics.MemStats.PauseNs.Count(); count-zero != 3 {
t.Fatal(count - zero) t.Fatal(count - zero)
} }
for i := 0; i < 256; i++ { for i := 0; i < 256; i++ {
runtime.GC() runtime.GC()
} }
CaptureRuntimeMemStatsOnce(r) CaptureRuntimeMemStatsOnce(r)
if count := runtimeMetrics.MemStats.PauseNs.Count(); 259 != count-zero { if count := runtimeMetrics.MemStats.PauseNs.Count(); count-zero != 259 {
t.Fatal(count - zero) t.Fatal(count - zero)
} }
for i := 0; i < 257; i++ { for i := 0; i < 257; i++ {
runtime.GC() runtime.GC()
} }
CaptureRuntimeMemStatsOnce(r) CaptureRuntimeMemStatsOnce(r)
if count := runtimeMetrics.MemStats.PauseNs.Count(); 515 != count-zero { // We lost one because there were too many GCs between captures. if count := runtimeMetrics.MemStats.PauseNs.Count(); count-zero != 515 { // We lost one because there were too many GCs between captures.
t.Fatal(count - zero) t.Fatal(count - zero)
} }
} }

@ -234,7 +234,7 @@ func (NilSample) Variance() float64 { return 0.0 }
// SampleMax returns the maximum value of the slice of int64. // SampleMax returns the maximum value of the slice of int64.
func SampleMax(values []int64) int64 { func SampleMax(values []int64) int64 {
if 0 == len(values) { if len(values) == 0 {
return 0 return 0
} }
var max int64 = math.MinInt64 var max int64 = math.MinInt64
@ -248,7 +248,7 @@ func SampleMax(values []int64) int64 {
// SampleMean returns the mean value of the slice of int64. // SampleMean returns the mean value of the slice of int64.
func SampleMean(values []int64) float64 { func SampleMean(values []int64) float64 {
if 0 == len(values) { if len(values) == 0 {
return 0.0 return 0.0
} }
return float64(SampleSum(values)) / float64(len(values)) return float64(SampleSum(values)) / float64(len(values))
@ -256,7 +256,7 @@ func SampleMean(values []int64) float64 {
// SampleMin returns the minimum value of the slice of int64. // SampleMin returns the minimum value of the slice of int64.
func SampleMin(values []int64) int64 { func SampleMin(values []int64) int64 {
if 0 == len(values) { if len(values) == 0 {
return 0 return 0
} }
var min int64 = math.MaxInt64 var min int64 = math.MaxInt64
@ -382,7 +382,7 @@ func SampleSum(values []int64) int64 {
// SampleVariance returns the variance of the slice of int64. // SampleVariance returns the variance of the slice of int64.
func SampleVariance(values []int64) float64 { func SampleVariance(values []int64) float64 {
if 0 == len(values) { if len(values) == 0 {
return 0.0 return 0.0
} }
m := SampleMean(values) m := SampleMean(values)

@ -85,13 +85,13 @@ func TestExpDecaySample10(t *testing.T) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
s.Update(int64(i)) s.Update(int64(i))
} }
if size := s.Count(); 10 != size { if size := s.Count(); size != 10 {
t.Errorf("s.Count(): 10 != %v\n", size) t.Errorf("s.Count(): 10 != %v\n", size)
} }
if size := s.Size(); 10 != size { if size := s.Size(); size != 10 {
t.Errorf("s.Size(): 10 != %v\n", size) t.Errorf("s.Size(): 10 != %v\n", size)
} }
if l := len(s.Values()); 10 != l { if l := len(s.Values()); l != 10 {
t.Errorf("len(s.Values()): 10 != %v\n", l) t.Errorf("len(s.Values()): 10 != %v\n", l)
} }
for _, v := range s.Values() { for _, v := range s.Values() {
@ -107,13 +107,13 @@ func TestExpDecaySample100(t *testing.T) {
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
s.Update(int64(i)) s.Update(int64(i))
} }
if size := s.Count(); 100 != size { if size := s.Count(); size != 100 {
t.Errorf("s.Count(): 100 != %v\n", size) t.Errorf("s.Count(): 100 != %v\n", size)
} }
if size := s.Size(); 100 != size { if size := s.Size(); size != 100 {
t.Errorf("s.Size(): 100 != %v\n", size) t.Errorf("s.Size(): 100 != %v\n", size)
} }
if l := len(s.Values()); 100 != l { if l := len(s.Values()); l != 100 {
t.Errorf("len(s.Values()): 100 != %v\n", l) t.Errorf("len(s.Values()): 100 != %v\n", l)
} }
for _, v := range s.Values() { for _, v := range s.Values() {
@ -129,13 +129,13 @@ func TestExpDecaySample1000(t *testing.T) {
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
s.Update(int64(i)) s.Update(int64(i))
} }
if size := s.Count(); 1000 != size { if size := s.Count(); size != 1000 {
t.Errorf("s.Count(): 1000 != %v\n", size) t.Errorf("s.Count(): 1000 != %v\n", size)
} }
if size := s.Size(); 100 != size { if size := s.Size(); size != 100 {
t.Errorf("s.Size(): 100 != %v\n", size) t.Errorf("s.Size(): 100 != %v\n", size)
} }
if l := len(s.Values()); 100 != l { if l := len(s.Values()); l != 100 {
t.Errorf("len(s.Values()): 100 != %v\n", l) t.Errorf("len(s.Values()): 100 != %v\n", l)
} }
for _, v := range s.Values() { for _, v := range s.Values() {
@ -209,13 +209,13 @@ func TestUniformSample(t *testing.T) {
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
s.Update(int64(i)) s.Update(int64(i))
} }
if size := s.Count(); 1000 != size { if size := s.Count(); size != 1000 {
t.Errorf("s.Count(): 1000 != %v\n", size) t.Errorf("s.Count(): 1000 != %v\n", size)
} }
if size := s.Size(); 100 != size { if size := s.Size(); size != 100 {
t.Errorf("s.Size(): 100 != %v\n", size) t.Errorf("s.Size(): 100 != %v\n", size)
} }
if l := len(s.Values()); 100 != l { if l := len(s.Values()); l != 100 {
t.Errorf("len(s.Values()): 100 != %v\n", l) t.Errorf("len(s.Values()): 100 != %v\n", l)
} }
for _, v := range s.Values() { for _, v := range s.Values() {
@ -277,54 +277,54 @@ func benchmarkSample(b *testing.B, s Sample) {
} }
func testExpDecaySampleStatistics(t *testing.T, s Sample) { func testExpDecaySampleStatistics(t *testing.T, s Sample) {
if count := s.Count(); 10000 != count { if count := s.Count(); count != 10000 {
t.Errorf("s.Count(): 10000 != %v\n", count) t.Errorf("s.Count(): 10000 != %v\n", count)
} }
if min := s.Min(); 107 != min { if min := s.Min(); min != 107 {
t.Errorf("s.Min(): 107 != %v\n", min) t.Errorf("s.Min(): 107 != %v\n", min)
} }
if max := s.Max(); 10000 != max { if max := s.Max(); max != 10000 {
t.Errorf("s.Max(): 10000 != %v\n", max) t.Errorf("s.Max(): 10000 != %v\n", max)
} }
if mean := s.Mean(); 4965.98 != mean { if mean := s.Mean(); mean != 4965.98 {
t.Errorf("s.Mean(): 4965.98 != %v\n", mean) t.Errorf("s.Mean(): 4965.98 != %v\n", mean)
} }
if stdDev := s.StdDev(); 2959.825156930727 != stdDev { if stdDev := s.StdDev(); stdDev != 2959.825156930727 {
t.Errorf("s.StdDev(): 2959.825156930727 != %v\n", stdDev) t.Errorf("s.StdDev(): 2959.825156930727 != %v\n", stdDev)
} }
ps := s.Percentiles([]float64{0.5, 0.75, 0.99}) ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
if 4615 != ps[0] { if ps[0] != 4615 {
t.Errorf("median: 4615 != %v\n", ps[0]) t.Errorf("median: 4615 != %v\n", ps[0])
} }
if 7672 != ps[1] { if ps[1] != 7672 {
t.Errorf("75th percentile: 7672 != %v\n", ps[1]) t.Errorf("75th percentile: 7672 != %v\n", ps[1])
} }
if 9998.99 != ps[2] { if ps[2] != 9998.99 {
t.Errorf("99th percentile: 9998.99 != %v\n", ps[2]) t.Errorf("99th percentile: 9998.99 != %v\n", ps[2])
} }
} }
func testUniformSampleStatistics(t *testing.T, s Sample) { func testUniformSampleStatistics(t *testing.T, s Sample) {
if count := s.Count(); 10000 != count { if count := s.Count(); count != 10000 {
t.Errorf("s.Count(): 10000 != %v\n", count) t.Errorf("s.Count(): 10000 != %v\n", count)
} }
if min := s.Min(); 37 != min { if min := s.Min(); min != 37 {
t.Errorf("s.Min(): 37 != %v\n", min) t.Errorf("s.Min(): 37 != %v\n", min)
} }
if max := s.Max(); 9989 != max { if max := s.Max(); max != 9989 {
t.Errorf("s.Max(): 9989 != %v\n", max) t.Errorf("s.Max(): 9989 != %v\n", max)
} }
if mean := s.Mean(); 4748.14 != mean { if mean := s.Mean(); mean != 4748.14 {
t.Errorf("s.Mean(): 4748.14 != %v\n", mean) t.Errorf("s.Mean(): 4748.14 != %v\n", mean)
} }
if stdDev := s.StdDev(); 2826.684117548333 != stdDev { if stdDev := s.StdDev(); stdDev != 2826.684117548333 {
t.Errorf("s.StdDev(): 2826.684117548333 != %v\n", stdDev) t.Errorf("s.StdDev(): 2826.684117548333 != %v\n", stdDev)
} }
ps := s.Percentiles([]float64{0.5, 0.75, 0.99}) ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
if 4599 != ps[0] { if ps[0] != 4599 {
t.Errorf("median: 4599 != %v\n", ps[0]) t.Errorf("median: 4599 != %v\n", ps[0])
} }
if 7380.5 != ps[1] { if ps[1] != 7380.5 {
t.Errorf("75th percentile: 7380.5 != %v\n", ps[1]) t.Errorf("75th percentile: 7380.5 != %v\n", ps[1])
} }
if math.Abs(9986.429999999998-ps[2]) > epsilonPercentile { if math.Abs(9986.429999999998-ps[2]) > epsilonPercentile {

@ -76,10 +76,7 @@ func NewTimer() Timer {
} }
// NilTimer is a no-op Timer. // NilTimer is a no-op Timer.
type NilTimer struct { type NilTimer struct{}
h Histogram
m Meter
}
// Count is a no-op. // Count is a no-op.
func (NilTimer) Count() int64 { return 0 } func (NilTimer) Count() int64 { return 0 }

@ -18,7 +18,7 @@ func BenchmarkTimer(b *testing.B) {
func TestGetOrRegisterTimer(t *testing.T) { func TestGetOrRegisterTimer(t *testing.T) {
r := NewRegistry() r := NewRegistry()
NewRegisteredTimer("foo", r).Update(47) NewRegisteredTimer("foo", r).Update(47)
if tm := GetOrRegisterTimer("foo", r); 1 != tm.Count() { if tm := GetOrRegisterTimer("foo", r); tm.Count() != 1 {
t.Fatal(tm) t.Fatal(tm)
} }
} }
@ -27,7 +27,7 @@ func TestTimerExtremes(t *testing.T) {
tm := NewTimer() tm := NewTimer()
tm.Update(math.MaxInt64) tm.Update(math.MaxInt64)
tm.Update(0) tm.Update(0)
if stdDev := tm.StdDev(); 4.611686018427388e+18 != stdDev { if stdDev := tm.StdDev(); stdDev != 4.611686018427388e+18 {
t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev) t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev)
} }
} }
@ -35,11 +35,11 @@ func TestTimerExtremes(t *testing.T) {
func TestTimerStop(t *testing.T) { func TestTimerStop(t *testing.T) {
l := len(arbiter.meters) l := len(arbiter.meters)
tm := NewTimer() tm := NewTimer()
if len(arbiter.meters) != l+1 { if l+1 != len(arbiter.meters) {
t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters)) t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
} }
tm.Stop() tm.Stop()
if len(arbiter.meters) != l { if l != len(arbiter.meters) {
t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters)) t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
} }
} }
@ -54,41 +54,41 @@ func TestTimerFunc(t *testing.T) {
func TestTimerZero(t *testing.T) { func TestTimerZero(t *testing.T) {
tm := NewTimer() tm := NewTimer()
if count := tm.Count(); 0 != count { if count := tm.Count(); count != 0 {
t.Errorf("tm.Count(): 0 != %v\n", count) t.Errorf("tm.Count(): 0 != %v\n", count)
} }
if min := tm.Min(); 0 != min { if min := tm.Min(); min != 0 {
t.Errorf("tm.Min(): 0 != %v\n", min) t.Errorf("tm.Min(): 0 != %v\n", min)
} }
if max := tm.Max(); 0 != max { if max := tm.Max(); max != 0 {
t.Errorf("tm.Max(): 0 != %v\n", max) t.Errorf("tm.Max(): 0 != %v\n", max)
} }
if mean := tm.Mean(); 0.0 != mean { if mean := tm.Mean(); mean != 0.0 {
t.Errorf("tm.Mean(): 0.0 != %v\n", mean) t.Errorf("tm.Mean(): 0.0 != %v\n", mean)
} }
if stdDev := tm.StdDev(); 0.0 != stdDev { if stdDev := tm.StdDev(); stdDev != 0.0 {
t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev) t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev)
} }
ps := tm.Percentiles([]float64{0.5, 0.75, 0.99}) ps := tm.Percentiles([]float64{0.5, 0.75, 0.99})
if 0.0 != ps[0] { if ps[0] != 0.0 {
t.Errorf("median: 0.0 != %v\n", ps[0]) t.Errorf("median: 0.0 != %v\n", ps[0])
} }
if 0.0 != ps[1] { if ps[1] != 0.0 {
t.Errorf("75th percentile: 0.0 != %v\n", ps[1]) t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
} }
if 0.0 != ps[2] { if ps[2] != 0.0 {
t.Errorf("99th percentile: 0.0 != %v\n", ps[2]) t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
} }
if rate1 := tm.Rate1(); 0.0 != rate1 { if rate1 := tm.Rate1(); rate1 != 0.0 {
t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1) t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1)
} }
if rate5 := tm.Rate5(); 0.0 != rate5 { if rate5 := tm.Rate5(); rate5 != 0.0 {
t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5) t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5)
} }
if rate15 := tm.Rate15(); 0.0 != rate15 { if rate15 := tm.Rate15(); rate15 != 0.0 {
t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15) t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15)
} }
if rateMean := tm.RateMean(); 0.0 != rateMean { if rateMean := tm.RateMean(); rateMean != 0.0 {
t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean) t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean)
} }
} }

Loading…
Cancel
Save