Codebase list golang-github-go-kit-kit / dae3ef4
metrics: cleanups - A naïve test of PrintDistribution - Don't use global rand; prefer local - Use teststat.Populate whenever possible Peter Bourgon 8 years ago
6 changed file(s) with 30 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
44 "fmt"
55 "io/ioutil"
66 "math"
7 "math/rand"
87 "net/http"
98 "net/http/httptest"
109 "regexp"
1716 "github.com/go-kit/kit/metrics"
1817 "github.com/go-kit/kit/metrics/expvar"
1918 "github.com/go-kit/kit/metrics/prometheus"
19 "github.com/go-kit/kit/metrics/teststat"
2020 )
2121
2222 func TestMultiWith(t *testing.T) {
124124 )
125125
126126 const seed, mean, stdev int64 = 123, 50, 10
127 populateNormalHistogram(t, h, seed, mean, stdev)
127 teststat.PopulateNormalHistogram(t, h, seed, mean, stdev)
128128 assertExpvarNormalHistogram(t, "omicron", mean, stdev, quantiles)
129129 assertPrometheusNormalHistogram(t, `test_multi_histogram_nu`, mean, stdev)
130 }
131
132 func populateNormalHistogram(t *testing.T, h metrics.Histogram, seed int64, mean, stdev int64) {
133 rand.Seed(seed)
134 for i := 0; i < 1234; i++ {
135 sample := int64(rand.NormFloat64()*float64(stdev) + float64(mean))
136 h.Observe(sample)
137 }
138130 }
139131
140132 func assertExpvarNormalHistogram(t *testing.T, metricName string, mean, stdev int64, quantiles []int) {
3434 }
3535 }
3636
37 tw.Flush() // to buf
37 tw.Flush()
3838 }
00 package metrics_test
11
22 import (
3 "os"
3 "bytes"
44 "testing"
5
6 "math"
57
68 "github.com/go-kit/kit/metrics"
79 "github.com/go-kit/kit/metrics/expvar"
1820 stdev = int64(1)
1921 )
2022 teststat.PopulateNormalHistogram(t, h, seed, mean, stdev)
21 metrics.PrintDistribution(os.Stdout, name, h.Distribution())
23
24 var buf bytes.Buffer
25 metrics.PrintDistribution(&buf, name, h.Distribution())
26 t.Logf("\n%s\n", buf.String())
27
28 // Count the number of bar chart characters.
29 // We should have roughly 100 in any distribution.
30
31 var n int
32 for _, r := range buf.String() {
33 if r == '#' {
34 n++
35 }
36 }
37 if want, have, tol := 100, n, 5; int(math.Abs(float64(want-have))) > tol {
38 t.Errorf("want %d, have %d (tolerance %d)", want, have, tol)
39 }
2240 }
44
55 "github.com/go-kit/kit/metrics"
66 "github.com/go-kit/kit/metrics/expvar"
7 "github.com/go-kit/kit/metrics/teststat"
78 )
89
910 func TestScaledHistogram(t *testing.T) {
1819 h = metrics.NewScaledHistogram(h, scale)
1920 h = h.With(metrics.Field{Key: "a", Value: "b"})
2021
21 const seed, mean, stdev = 333, 500, 100 // input values
22 populateNormalHistogram(t, h, seed, mean, stdev) // will be scaled down
22 const seed, mean, stdev = 333, 500, 100 // input values
23 teststat.PopulateNormalHistogram(t, h, seed, mean, stdev) // will be scaled down
2324 assertExpvarNormalHistogram(t, metricName, mean/scale, stdev/scale, quantiles)
2425 }
1414 // PopulateNormalHistogram populates the Histogram with a normal distribution
1515 // of observations.
1616 func PopulateNormalHistogram(t *testing.T, h metrics.Histogram, seed int64, mean, stdev int64) {
17 rand.Seed(seed)
17 r := rand.New(rand.NewSource(seed))
1818 for i := 0; i < population; i++ {
19 sample := int64(rand.NormFloat64()*float64(stdev) + float64(mean))
19 sample := int64(r.NormFloat64()*float64(stdev) + float64(mean))
2020 if sample < 0 {
2121 sample = 0
2222 }
2020 )
2121
2222 const seed, mean, stdev int64 = 321, 100, 20
23 r := rand.New(rand.NewSource(seed))
2324
2425 for i := 0; i < 4321; i++ {
25 sample := time.Duration(rand.NormFloat64()*float64(stdev)+float64(mean)) * time.Millisecond
26 sample := time.Duration(r.NormFloat64()*float64(stdev)+float64(mean)) * time.Millisecond
2627 th.Observe(sample)
2728 }
2829