Source file
src/runtime/histogram_test.go
1
2
3
4
5 package runtime_test
6
7 import (
8 "math"
9 . "runtime"
10 "testing"
11 )
12
13 var dummyTimeHistogram TimeHistogram
14
15 func TestTimeHistogram(t *testing.T) {
16
17
18
19
20 h := &dummyTimeHistogram
21
22
23 for i := 0; i < TimeHistNumSuperBuckets; i++ {
24 var base int64
25 if i > 0 {
26 base = int64(1) << (i + TimeHistSubBucketBits - 1)
27 }
28 for j := 0; j < TimeHistNumSubBuckets; j++ {
29 v := int64(j)
30 if i > 0 {
31 v <<= i - 1
32 }
33 h.Record(base + v)
34 }
35 }
36
37 h.Record(int64(-1))
38
39
40
41 for i := uint(0); i < TimeHistNumSuperBuckets; i++ {
42 for j := uint(0); j < TimeHistNumSubBuckets; j++ {
43 c, ok := h.Count(i, j)
44 if !ok {
45 t.Errorf("hit underflow bucket unexpectedly: (%d, %d)", i, j)
46 } else if c != 1 {
47 t.Errorf("bucket (%d, %d) has count that is not 1: %d", i, j, c)
48 }
49 }
50 }
51 c, ok := h.Count(TimeHistNumSuperBuckets, 0)
52 if ok {
53 t.Errorf("expected to hit underflow bucket: (%d, %d)", TimeHistNumSuperBuckets, 0)
54 }
55 if c != 1 {
56 t.Errorf("underflow bucket has count that is not 1: %d", c)
57 }
58
59
60
61 h.Record(math.MaxInt64)
62 c, ok = h.Count(TimeHistNumSuperBuckets-1, TimeHistNumSubBuckets-1)
63 if !ok {
64 t.Error("hit underflow bucket in highest bucket unexpectedly")
65 } else if c != 2 {
66 t.Errorf("highest has count that is not 2: %d", c)
67 }
68
69 dummyTimeHistogram = TimeHistogram{}
70 }
71
72 func TestTimeHistogramMetricsBuckets(t *testing.T) {
73 buckets := TimeHistogramMetricsBuckets()
74
75 nonInfBucketsLen := TimeHistNumSubBuckets * TimeHistNumSuperBuckets
76 expBucketsLen := nonInfBucketsLen + 2
77 if len(buckets) != expBucketsLen {
78 t.Fatalf("unexpected length of buckets: got %d, want %d", len(buckets), expBucketsLen)
79 }
80
81
82
83
84
85
86
87 for i := 1; i <= 2*TimeHistNumSubBuckets+1; i++ {
88 if got, want := buckets[i], float64(i-1)/1e9; got != want {
89 t.Errorf("expected bucket %d to have value %e, got %e", i, want, got)
90 }
91 }
92
93 idxToBucket := map[int]float64{
94 0: math.Inf(-1),
95 33: float64(0x10<<1) / 1e9,
96 34: float64(0x11<<1) / 1e9,
97 49: float64(0x10<<2) / 1e9,
98 58: float64(0x19<<2) / 1e9,
99 65: float64(0x10<<3) / 1e9,
100 513: float64(0x10<<31) / 1e9,
101 519: float64(0x16<<31) / 1e9,
102 expBucketsLen - 2: float64(0x1f<<43) / 1e9,
103 expBucketsLen - 1: math.Inf(1),
104 }
105 for idx, bucket := range idxToBucket {
106 if got, want := buckets[idx], bucket; got != want {
107 t.Errorf("expected bucket %d to have value %e, got %e", idx, want, got)
108 }
109 }
110 }
111
View as plain text