OSDN Git Service

delete miner
[bytom/vapor.git] / vendor / github.com / go-kit / kit / metrics / generic / generic_test.go
1 package generic_test
2
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.
6
7 import (
8         "math"
9         "math/rand"
10         "sync"
11         "testing"
12
13         "github.com/go-kit/kit/metrics/generic"
14         "github.com/go-kit/kit/metrics/teststat"
15 )
16
17 func TestCounter(t *testing.T) {
18         name := "my_counter"
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)
22         }
23         value := func() float64 { return counter.Value() }
24         if err := teststat.TestCounter(counter, value); err != nil {
25                 t.Fatal(err)
26         }
27 }
28
29 func TestValueReset(t *testing.T) {
30         counter := generic.NewCounter("test_value_reset")
31         counter.Add(123)
32         counter.Add(456)
33         counter.Add(789)
34         if want, have := float64(123+456+789), counter.ValueReset(); want != have {
35                 t.Errorf("want %f, have %f", want, have)
36         }
37         if want, have := float64(0), counter.Value(); want != have {
38                 t.Errorf("want %f, have %f", want, have)
39         }
40 }
41
42 func TestGauge(t *testing.T) {
43         name := "my_gauge"
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)
47         }
48         value := func() float64 { return gauge.Value() }
49         if err := teststat.TestGauge(gauge, value); err != nil {
50                 t.Fatal(err)
51         }
52 }
53
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)
59         }
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)
62         }
63         if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
64                 t.Fatal(err)
65         }
66 }
67
68 func TestIssue424(t *testing.T) {
69         var (
70                 histogram   = generic.NewHistogram("dont_panic", 50)
71                 concurrency = 100
72                 operations  = 1000
73                 wg          sync.WaitGroup
74         )
75
76         wg.Add(concurrency)
77         for i := 0; i < concurrency; i++ {
78                 go func() {
79                         defer wg.Done()
80                         for j := 0; j < operations; j++ {
81                                 histogram.Observe(float64(j))
82                                 histogram.Observe(histogram.Quantile(0.5))
83                         }
84                 }()
85         }
86         wg.Wait()
87 }
88
89 func TestSimpleHistogram(t *testing.T) {
90         histogram := generic.NewSimpleHistogram().With("label", "simple_histogram").(*generic.SimpleHistogram)
91         var (
92                 sum   int
93                 count = 1234 // not too big
94         )
95         for i := 0; i < count; i++ {
96                 value := rand.Intn(1000)
97                 sum += value
98                 histogram.Observe(float64(value))
99         }
100
101         var (
102                 want      = float64(sum) / float64(count)
103                 have      = histogram.ApproximateMovingAverage()
104                 tolerance = 0.001 // real real slim
105         )
106         if math.Abs(want-have)/want > tolerance {
107                 t.Errorf("want %f, have %f", want, have)
108         }
109 }