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
4 | 4 | "fmt" |
5 | 5 | "io/ioutil" |
6 | 6 | "math" |
7 | "math/rand" | |
8 | 7 | "net/http" |
9 | 8 | "net/http/httptest" |
10 | 9 | "regexp" |
17 | 16 | "github.com/go-kit/kit/metrics" |
18 | 17 | "github.com/go-kit/kit/metrics/expvar" |
19 | 18 | "github.com/go-kit/kit/metrics/prometheus" |
19 | "github.com/go-kit/kit/metrics/teststat" | |
20 | 20 | ) |
21 | 21 | |
22 | 22 | func TestMultiWith(t *testing.T) { |
124 | 124 | ) |
125 | 125 | |
126 | 126 | const seed, mean, stdev int64 = 123, 50, 10 |
127 | populateNormalHistogram(t, h, seed, mean, stdev) | |
127 | teststat.PopulateNormalHistogram(t, h, seed, mean, stdev) | |
128 | 128 | assertExpvarNormalHistogram(t, "omicron", mean, stdev, quantiles) |
129 | 129 | 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 | } | |
138 | 130 | } |
139 | 131 | |
140 | 132 | func assertExpvarNormalHistogram(t *testing.T, metricName string, mean, stdev int64, quantiles []int) { |
0 | 0 | package metrics_test |
1 | 1 | |
2 | 2 | import ( |
3 | "os" | |
3 | "bytes" | |
4 | 4 | "testing" |
5 | ||
6 | "math" | |
5 | 7 | |
6 | 8 | "github.com/go-kit/kit/metrics" |
7 | 9 | "github.com/go-kit/kit/metrics/expvar" |
18 | 20 | stdev = int64(1) |
19 | 21 | ) |
20 | 22 | 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 | } | |
22 | 40 | } |
4 | 4 | |
5 | 5 | "github.com/go-kit/kit/metrics" |
6 | 6 | "github.com/go-kit/kit/metrics/expvar" |
7 | "github.com/go-kit/kit/metrics/teststat" | |
7 | 8 | ) |
8 | 9 | |
9 | 10 | func TestScaledHistogram(t *testing.T) { |
18 | 19 | h = metrics.NewScaledHistogram(h, scale) |
19 | 20 | h = h.With(metrics.Field{Key: "a", Value: "b"}) |
20 | 21 | |
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 | |
23 | 24 | assertExpvarNormalHistogram(t, metricName, mean/scale, stdev/scale, quantiles) |
24 | 25 | } |
14 | 14 | // PopulateNormalHistogram populates the Histogram with a normal distribution |
15 | 15 | // of observations. |
16 | 16 | func PopulateNormalHistogram(t *testing.T, h metrics.Histogram, seed int64, mean, stdev int64) { |
17 | rand.Seed(seed) | |
17 | r := rand.New(rand.NewSource(seed)) | |
18 | 18 | for i := 0; i < population; i++ { |
19 | sample := int64(rand.NormFloat64()*float64(stdev) + float64(mean)) | |
19 | sample := int64(r.NormFloat64()*float64(stdev) + float64(mean)) | |
20 | 20 | if sample < 0 { |
21 | 21 | sample = 0 |
22 | 22 | } |
20 | 20 | ) |
21 | 21 | |
22 | 22 | const seed, mean, stdev int64 = 321, 100, 20 |
23 | r := rand.New(rand.NewSource(seed)) | |
23 | 24 | |
24 | 25 | 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 | |
26 | 27 | th.Observe(sample) |
27 | 28 | } |
28 | 29 |