3 // This is package generic_test in order to get around an import cycle: this
4 // package imports teststat to do its testing, but package teststat imports
5 // generic to use its Histogram in the Quantiles helper function.
13 "github.com/go-kit/kit/metrics/generic"
14 "github.com/go-kit/kit/metrics/teststat"
17 func TestCounter(t *testing.T) {
19 counter := generic.NewCounter(name).With("label", "counter").(*generic.Counter)
20 if want, have := name, counter.Name; want != have {
21 t.Errorf("Name: want %q, have %q", want, have)
23 value := func() float64 { return counter.Value() }
24 if err := teststat.TestCounter(counter, value); err != nil {
29 func TestValueReset(t *testing.T) {
30 counter := generic.NewCounter("test_value_reset")
34 if want, have := float64(123+456+789), counter.ValueReset(); want != have {
35 t.Errorf("want %f, have %f", want, have)
37 if want, have := float64(0), counter.Value(); want != have {
38 t.Errorf("want %f, have %f", want, have)
42 func TestGauge(t *testing.T) {
44 gauge := generic.NewGauge(name).With("label", "gauge").(*generic.Gauge)
45 if want, have := name, gauge.Name; want != have {
46 t.Errorf("Name: want %q, have %q", want, have)
48 value := func() float64 { return gauge.Value() }
49 if err := teststat.TestGauge(gauge, value); err != nil {
54 func TestHistogram(t *testing.T) {
55 name := "my_histogram"
56 histogram := generic.NewHistogram(name, 50).With("label", "histogram").(*generic.Histogram)
57 if want, have := name, histogram.Name; want != have {
58 t.Errorf("Name: want %q, have %q", want, have)
60 quantiles := func() (float64, float64, float64, float64) {
61 return histogram.Quantile(0.50), histogram.Quantile(0.90), histogram.Quantile(0.95), histogram.Quantile(0.99)
63 if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
68 func TestIssue424(t *testing.T) {
70 histogram = generic.NewHistogram("dont_panic", 50)
77 for i := 0; i < concurrency; i++ {
80 for j := 0; j < operations; j++ {
81 histogram.Observe(float64(j))
82 histogram.Observe(histogram.Quantile(0.5))
89 func TestSimpleHistogram(t *testing.T) {
90 histogram := generic.NewSimpleHistogram().With("label", "simple_histogram").(*generic.SimpleHistogram)
93 count = 1234 // not too big
95 for i := 0; i < count; i++ {
96 value := rand.Intn(1000)
98 histogram.Observe(float64(value))
102 want = float64(sum) / float64(count)
103 have = histogram.ApproximateMovingAverage()
104 tolerance = 0.001 // real real slim
106 if math.Abs(want-have)/want > tolerance {
107 t.Errorf("want %f, have %f", want, have)