Skip to content

Commit

Permalink
Finish Histogram tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon committed Jul 11, 2016
1 parent 24e19c8 commit 8f3c165
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 25 deletions.
2 changes: 2 additions & 0 deletions metrics2/circonus/circonus_test.go
Expand Up @@ -100,6 +100,8 @@ func TestHistogram(t *testing.T) {
histogram := New(m).NewHistogram(name)
quantiles := func() (float64, float64, float64, float64) { m.Flush(); return p50, p90, p95, p99 }

// Circonus metrics, because they do their own bucketing, are less precise
// than other systems. So, we bump the tolerance to 5 percent.
if err := teststat.TestHistogram(histogram, quantiles, 0.05); err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion metrics2/dogstatsd/dogstatsd_test.go
Expand Up @@ -70,7 +70,7 @@ func TestHistogram(t *testing.T) {
return h.Quantile(0.50), h.Quantile(0.90), h.Quantile(0.95), h.Quantile(0.99)
}

if err := teststat.TestHistogram(histogram, quantiles, 0.02); err != nil {
if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
t.Fatal(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion metrics2/expvar/expvar.go
Expand Up @@ -81,7 +81,7 @@ func (h *Histogram) With(labelValues ...string) metrics.Histogram { return h }
func (h *Histogram) Observe(value float64) {
h.mtx.Lock()
defer h.mtx.Unlock()
h.Observe(value)
h.h.Observe(value)
h.p50.Set(h.h.Quantile(0.50))
h.p90.Set(h.h.Quantile(0.90))
h.p95.Set(h.h.Quantile(0.95))
Expand Down
16 changes: 14 additions & 2 deletions metrics2/expvar/expvar_test.go
Expand Up @@ -10,7 +10,6 @@ import (
func TestCounter(t *testing.T) {
counter := NewCounter("expvar_counter")
value := func() float64 { f, _ := strconv.ParseFloat(counter.f.String(), 64); return f }

if err := teststat.TestCounter(counter, value); err != nil {
t.Fatal(err)
}
Expand All @@ -19,8 +18,21 @@ func TestCounter(t *testing.T) {
func TestGauge(t *testing.T) {
gauge := NewGauge("expvar_gauge")
value := func() float64 { f, _ := strconv.ParseFloat(gauge.f.String(), 64); return f }

if err := teststat.TestGauge(gauge, value); err != nil {
t.Fatal(err)
}
}

func TestHistogram(t *testing.T) {
histogram := NewHistogram("expvar_histogram", 50)
quantiles := func() (float64, float64, float64, float64) {
p50, _ := strconv.ParseFloat(histogram.p50.String(), 64)
p90, _ := strconv.ParseFloat(histogram.p90.String(), 64)
p95, _ := strconv.ParseFloat(histogram.p95.String(), 64)
p99, _ := strconv.ParseFloat(histogram.p99.String(), 64)
return p50, p90, p95, p99
}
if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
t.Fatal(err)
}
}
40 changes: 33 additions & 7 deletions metrics2/graphite/graphite_test.go
Expand Up @@ -12,9 +12,8 @@ import (

func TestCounter(t *testing.T) {
prefix, name := "foo.", "bar"
re := regexp.MustCompile(prefix + name + `.count ([0-9\.]+)`) // Graphite protocol
re := regexp.MustCompile(prefix + name + `.count ([0-9\.]+) [0-9]+`) // Graphite protocol
g := NewRaw(prefix, log.NewNopLogger())

counter := g.NewCounter(name)
value := func() float64 {
var buf bytes.Buffer
Expand All @@ -23,15 +22,15 @@ func TestCounter(t *testing.T) {
f, _ := strconv.ParseFloat(match[1], 64)
return f
}

teststat.TestCounter(counter, value)
if err := teststat.TestCounter(counter, value); err != nil {
t.Fatal(err)
}
}

func TestGauge(t *testing.T) {
prefix, name := "baz.", "quux"
re := regexp.MustCompile(prefix + name + ` ([0-9\.]+)`)
re := regexp.MustCompile(prefix + name + ` ([0-9\.]+) [0-9]+`)
g := NewRaw(prefix, log.NewNopLogger())

gauge := g.NewGauge(name)
value := func() float64 {
var buf bytes.Buffer
Expand All @@ -40,6 +39,33 @@ func TestGauge(t *testing.T) {
f, _ := strconv.ParseFloat(match[1], 64)
return f
}
if err := teststat.TestGauge(gauge, value); err != nil {
t.Fatal(err)
}
}

teststat.TestGauge(gauge, value)
func TestHistogram(t *testing.T) {
prefix, name := "head.", "toes"
re50 := regexp.MustCompile(prefix + name + `.p50 ([0-9\.]+) [0-9]+`)
re90 := regexp.MustCompile(prefix + name + `.p90 ([0-9\.]+) [0-9]+`)
re95 := regexp.MustCompile(prefix + name + `.p95 ([0-9\.]+) [0-9]+`)
re99 := regexp.MustCompile(prefix + name + `.p99 ([0-9\.]+) [0-9]+`)
g := NewRaw(prefix, log.NewNopLogger())
histogram := g.NewHistogram(name, 50)
quantiles := func() (float64, float64, float64, float64) {
var buf bytes.Buffer
g.WriteTo(&buf)
match50 := re50.FindStringSubmatch(buf.String())
p50, _ := strconv.ParseFloat(match50[1], 64)
match90 := re90.FindStringSubmatch(buf.String())
p90, _ := strconv.ParseFloat(match90[1], 64)
match95 := re95.FindStringSubmatch(buf.String())
p95, _ := strconv.ParseFloat(match95[1], 64)
match99 := re99.FindStringSubmatch(buf.String())
p99, _ := strconv.ParseFloat(match99[1], 64)
return p50, p90, p95, p99
}
if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
t.Fatal(err)
}
}
33 changes: 27 additions & 6 deletions metrics2/influx/influx_test.go
Expand Up @@ -15,8 +15,7 @@ import (

func TestCounter(t *testing.T) {
i := NewRaw(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())

re := regexp.MustCompile(`influx_counter,a=b,c=d count=([0-9\.]+)`) // reverse-engineered :\
re := regexp.MustCompile(`influx_counter,a=b,c=d count=([0-9\.]+) [0-9]+`) // reverse-engineered :\
counter := i.NewCounter("influx_counter", map[string]string{"c": "d"})
value := func() float64 {
client := &mockClient{}
Expand All @@ -25,16 +24,14 @@ func TestCounter(t *testing.T) {
f, _ := strconv.ParseFloat(match[1], 64)
return f
}

if err := teststat.TestCounter(counter, value); err != nil {
t.Fatal(err)
}
}

func TestGauge(t *testing.T) {
i := NewRaw(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())

re := regexp.MustCompile(`influx_gauge,bar=beta,foo=alpha value=([0-9\.]+)`)
re := regexp.MustCompile(`influx_gauge,bar=beta,foo=alpha value=([0-9\.]+) [0-9]+`)
gauge := i.NewGauge("influx_gauge", map[string]string{"bar": "beta"})
value := func() float64 {
client := &mockClient{}
Expand All @@ -43,12 +40,36 @@ func TestGauge(t *testing.T) {
f, _ := strconv.ParseFloat(match[1], 64)
return f
}

if err := teststat.TestGauge(gauge, value); err != nil {
t.Fatal(err)
}
}

func TestHistogram(t *testing.T) {
i := NewRaw(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
re50 := regexp.MustCompile(`influx_histogram.p50,foo=alpha value=([0-9\.]+) [0-9]+`)
re90 := regexp.MustCompile(`influx_histogram.p90,foo=alpha value=([0-9\.]+) [0-9]+`)
re95 := regexp.MustCompile(`influx_histogram.p95,foo=alpha value=([0-9\.]+) [0-9]+`)
re99 := regexp.MustCompile(`influx_histogram.p99,foo=alpha value=([0-9\.]+) [0-9]+`)
histogram := i.NewHistogram("influx_histogram", map[string]string{}, 50)
quantiles := func() (float64, float64, float64, float64) {
client := &mockClient{}
i.WriteTo(client)
match50 := re50.FindStringSubmatch(client.buf.String())
p50, _ := strconv.ParseFloat(match50[1], 64)
match90 := re90.FindStringSubmatch(client.buf.String())
p90, _ := strconv.ParseFloat(match90[1], 64)
match95 := re95.FindStringSubmatch(client.buf.String())
p95, _ := strconv.ParseFloat(match95[1], 64)
match99 := re99.FindStringSubmatch(client.buf.String())
p99, _ := strconv.ParseFloat(match99[1], 64)
return p50, p90, p95, p99
}
if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
t.Fatal(err)
}
}

type mockClient struct {
buf bytes.Buffer
}
Expand Down
8 changes: 0 additions & 8 deletions metrics2/teststat/populate.go
Expand Up @@ -29,14 +29,6 @@ func nvq(mean, stdev, quantile int) float64 {
return float64(mean) + float64(stdev)*math.Sqrt2*erfinv(2*(float64(quantile)/100)-1)
}

/*
// https://code.google.com/p/gostat/source/browse/stat/normal.go
func observationsLessThan(mean, stdev int64, x float64, total int) int {
cdf := ((1.0 / 2.0) * (1 + math.Erf((x-float64(mean))/(float64(stdev)*math.Sqrt2))))
return int(cdf * float64(total))
}
*/

func erfinv(y float64) float64 {
// https://stackoverflow.com/questions/5971830/need-code-for-inverse-error-function
if y < -1.0 || y > 1.0 {
Expand Down

0 comments on commit 8f3c165

Please sign in to comment.