Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: common/tsmon/metric/metric_test.go

Issue 2123853002: Added unit annotation supports onto tsmon in go. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: Specify the unit of logdog/collector/subscription/processing_time_ms with types.Milliseconds Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « common/tsmon/metric/metric.go ('k') | common/tsmon/metric/standard_metrics.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package metric 5 package metric
6 6
7 import ( 7 import (
8 "testing" 8 "testing"
9 9
10 "github.com/golang/protobuf/proto" 10 "github.com/golang/protobuf/proto"
(...skipping 12 matching lines...) Expand all
23 return tsmon.WithState(context.Background(), &tsmon.State{ 23 return tsmon.WithState(context.Background(), &tsmon.State{
24 S: store.NewInMemory(&target.Task{ServiceName: p roto.String("default target")}), 24 S: store.NewInMemory(&target.Task{ServiceName: p roto.String("default target")}),
25 M: monitor.NewNilMonitor(), 25 M: monitor.NewNilMonitor(),
26 RegisteredMetrics: map[string]types.Metric{}, 26 RegisteredMetrics: map[string]types.Metric{},
27 }) 27 })
28 } 28 }
29 29
30 func TestMetrics(t *testing.T) { 30 func TestMetrics(t *testing.T) {
31 Convey("Int", t, func() { 31 Convey("Int", t, func() {
32 c := makeContext() 32 c := makeContext()
33 » » m := NewIntIn(c, "foo", "description") 33 » » m := NewIntIn(c, "foo", "description", types.MetricMetadata{})
34 34
35 v, err := m.Get(c) 35 v, err := m.Get(c)
36 So(v, ShouldEqual, 0) 36 So(v, ShouldEqual, 0)
37 So(err, ShouldBeNil) 37 So(err, ShouldBeNil)
38 38
39 v, err = m.Get(c, "field") 39 v, err = m.Get(c, "field")
40 So(err, ShouldNotBeNil) 40 So(err, ShouldNotBeNil)
41 41
42 err = m.Set(c, 42) 42 err = m.Set(c, 42)
43 So(err, ShouldBeNil) 43 So(err, ShouldBeNil)
44 44
45 v, err = m.Get(c) 45 v, err = m.Get(c)
46 So(v, ShouldEqual, 42) 46 So(v, ShouldEqual, 42)
47 So(err, ShouldBeNil) 47 So(err, ShouldBeNil)
48 48
49 err = m.Set(c, 42, "field") 49 err = m.Set(c, 42, "field")
50 So(err, ShouldNotBeNil) 50 So(err, ShouldNotBeNil)
51 51
52 » » So(func() { NewIntIn(c, "foo", "description") }, ShouldPanic) 52 » » So(func() { NewIntIn(c, "foo", "description", types.MetricMetada ta{}) }, ShouldPanic)
53 }) 53 })
54 54
55 Convey("Counter", t, func() { 55 Convey("Counter", t, func() {
56 c := makeContext() 56 c := makeContext()
57 » » m := NewCounterIn(c, "foo", "description") 57 » » m := NewCounterIn(c, "foo", "description", types.MetricMetadata{ })
58 58
59 v, err := m.Get(c) 59 v, err := m.Get(c)
60 So(v, ShouldEqual, 0) 60 So(v, ShouldEqual, 0)
61 So(err, ShouldBeNil) 61 So(err, ShouldBeNil)
62 62
63 err = m.Add(c, 3) 63 err = m.Add(c, 3)
64 So(err, ShouldBeNil) 64 So(err, ShouldBeNil)
65 65
66 v, err = m.Get(c) 66 v, err = m.Get(c)
67 So(v, ShouldEqual, 3) 67 So(v, ShouldEqual, 3)
68 So(err, ShouldBeNil) 68 So(err, ShouldBeNil)
69 69
70 err = m.Add(c, 2) 70 err = m.Add(c, 2)
71 So(err, ShouldBeNil) 71 So(err, ShouldBeNil)
72 72
73 v, err = m.Get(c) 73 v, err = m.Get(c)
74 So(v, ShouldEqual, 5) 74 So(v, ShouldEqual, 5)
75 So(err, ShouldBeNil) 75 So(err, ShouldBeNil)
76 76
77 » » So(func() { NewCounterIn(c, "foo", "description") }, ShouldPanic ) 77 » » So(func() { NewCounterIn(c, "foo", "description", types.MetricMe tadata{}) }, ShouldPanic)
78 }) 78 })
79 79
80 Convey("Float", t, func() { 80 Convey("Float", t, func() {
81 c := makeContext() 81 c := makeContext()
82 » » m := NewFloatIn(c, "foo", "description") 82 » » m := NewFloatIn(c, "foo", "description", types.MetricMetadata{})
83 83
84 v, err := m.Get(c) 84 v, err := m.Get(c)
85 So(v, ShouldAlmostEqual, 0.0) 85 So(v, ShouldAlmostEqual, 0.0)
86 So(err, ShouldBeNil) 86 So(err, ShouldBeNil)
87 87
88 v, err = m.Get(c, "field") 88 v, err = m.Get(c, "field")
89 So(err, ShouldNotBeNil) 89 So(err, ShouldNotBeNil)
90 90
91 err = m.Set(c, 42.3) 91 err = m.Set(c, 42.3)
92 So(err, ShouldBeNil) 92 So(err, ShouldBeNil)
93 93
94 v, err = m.Get(c) 94 v, err = m.Get(c)
95 So(v, ShouldAlmostEqual, 42.3) 95 So(v, ShouldAlmostEqual, 42.3)
96 So(err, ShouldBeNil) 96 So(err, ShouldBeNil)
97 97
98 err = m.Set(c, 42.3, "field") 98 err = m.Set(c, 42.3, "field")
99 So(err, ShouldNotBeNil) 99 So(err, ShouldNotBeNil)
100 100
101 » » So(func() { NewFloatIn(c, "foo", "description") }, ShouldPanic) 101 » » So(func() { NewFloatIn(c, "foo", "description", types.MetricMeta data{}) }, ShouldPanic)
102 }) 102 })
103 103
104 Convey("FloatCounter", t, func() { 104 Convey("FloatCounter", t, func() {
105 c := makeContext() 105 c := makeContext()
106 » » m := NewFloatCounterIn(c, "foo", "description") 106 » » m := NewFloatCounterIn(c, "foo", "description", types.MetricMeta data{})
107 107
108 v, err := m.Get(c) 108 v, err := m.Get(c)
109 So(v, ShouldAlmostEqual, 0.0) 109 So(v, ShouldAlmostEqual, 0.0)
110 So(err, ShouldBeNil) 110 So(err, ShouldBeNil)
111 111
112 err = m.Add(c, 3.1) 112 err = m.Add(c, 3.1)
113 So(err, ShouldBeNil) 113 So(err, ShouldBeNil)
114 114
115 v, err = m.Get(c) 115 v, err = m.Get(c)
116 So(v, ShouldAlmostEqual, 3.1) 116 So(v, ShouldAlmostEqual, 3.1)
117 So(err, ShouldBeNil) 117 So(err, ShouldBeNil)
118 118
119 err = m.Add(c, 2.2) 119 err = m.Add(c, 2.2)
120 So(err, ShouldBeNil) 120 So(err, ShouldBeNil)
121 121
122 v, err = m.Get(c) 122 v, err = m.Get(c)
123 So(v, ShouldAlmostEqual, 5.3) 123 So(v, ShouldAlmostEqual, 5.3)
124 So(err, ShouldBeNil) 124 So(err, ShouldBeNil)
125 125
126 » » So(func() { NewFloatCounterIn(c, "foo", "description") }, Should Panic) 126 » » So(func() { NewFloatCounterIn(c, "foo", "description", types.Met ricMetadata{}) }, ShouldPanic)
127 }) 127 })
128 128
129 Convey("String", t, func() { 129 Convey("String", t, func() {
130 c := makeContext() 130 c := makeContext()
131 » » m := NewStringIn(c, "foo", "description") 131 » » m := NewStringIn(c, "foo", "description", types.MetricMetadata{} )
132 132
133 v, err := m.Get(c) 133 v, err := m.Get(c)
134 So(v, ShouldEqual, "") 134 So(v, ShouldEqual, "")
135 So(err, ShouldBeNil) 135 So(err, ShouldBeNil)
136 136
137 v, err = m.Get(c, "field") 137 v, err = m.Get(c, "field")
138 So(err, ShouldNotBeNil) 138 So(err, ShouldNotBeNil)
139 139
140 err = m.Set(c, "hello") 140 err = m.Set(c, "hello")
141 So(err, ShouldBeNil) 141 So(err, ShouldBeNil)
142 142
143 v, err = m.Get(c) 143 v, err = m.Get(c)
144 So(v, ShouldEqual, "hello") 144 So(v, ShouldEqual, "hello")
145 So(err, ShouldBeNil) 145 So(err, ShouldBeNil)
146 146
147 err = m.Set(c, "hello", "field") 147 err = m.Set(c, "hello", "field")
148 So(err, ShouldNotBeNil) 148 So(err, ShouldNotBeNil)
149 149
150 » » So(func() { NewStringIn(c, "foo", "description") }, ShouldPanic) 150 » » So(func() { NewStringIn(c, "foo", "description", types.MetricMet adata{}) }, ShouldPanic)
151 }) 151 })
152 152
153 Convey("Bool", t, func() { 153 Convey("Bool", t, func() {
154 c := makeContext() 154 c := makeContext()
155 » » m := NewBoolIn(c, "foo", "description") 155 » » m := NewBoolIn(c, "foo", "description", types.MetricMetadata{})
156 156
157 v, err := m.Get(c) 157 v, err := m.Get(c)
158 So(v, ShouldEqual, false) 158 So(v, ShouldEqual, false)
159 So(err, ShouldBeNil) 159 So(err, ShouldBeNil)
160 160
161 v, err = m.Get(c, "field") 161 v, err = m.Get(c, "field")
162 So(err, ShouldNotBeNil) 162 So(err, ShouldNotBeNil)
163 163
164 err = m.Set(c, true) 164 err = m.Set(c, true)
165 So(err, ShouldBeNil) 165 So(err, ShouldBeNil)
166 166
167 v, err = m.Get(c) 167 v, err = m.Get(c)
168 So(v, ShouldEqual, true) 168 So(v, ShouldEqual, true)
169 So(err, ShouldBeNil) 169 So(err, ShouldBeNil)
170 170
171 err = m.Set(c, true, "field") 171 err = m.Set(c, true, "field")
172 So(err, ShouldNotBeNil) 172 So(err, ShouldNotBeNil)
173 173
174 » » So(func() { NewBoolIn(c, "foo", "description") }, ShouldPanic) 174 » » So(func() { NewBoolIn(c, "foo", "description", types.MetricMetad ata{}) }, ShouldPanic)
175 }) 175 })
176 176
177 Convey("CumulativeDistribution", t, func() { 177 Convey("CumulativeDistribution", t, func() {
178 c := makeContext() 178 c := makeContext()
179 » » m := NewCumulativeDistributionIn(c, "foo", "description", distri bution.FixedWidthBucketer(10, 20)) 179 » » m := NewCumulativeDistributionIn(c, "foo", "description", types. MetricMetadata{}, distribution.FixedWidthBucketer(10, 20))
180 180
181 v, err := m.Get(c) 181 v, err := m.Get(c)
182 So(v, ShouldBeNil) 182 So(v, ShouldBeNil)
183 So(err, ShouldBeNil) 183 So(err, ShouldBeNil)
184 184
185 So(m.Bucketer().GrowthFactor(), ShouldEqual, 0) 185 So(m.Bucketer().GrowthFactor(), ShouldEqual, 0)
186 So(m.Bucketer().Width(), ShouldEqual, 10) 186 So(m.Bucketer().Width(), ShouldEqual, 10)
187 So(m.Bucketer().NumFiniteBuckets(), ShouldEqual, 20) 187 So(m.Bucketer().NumFiniteBuckets(), ShouldEqual, 20)
188 188
189 v, err = m.Get(c, "field") 189 v, err = m.Get(c, "field")
190 So(err, ShouldNotBeNil) 190 So(err, ShouldNotBeNil)
191 191
192 err = m.Add(c, 5) 192 err = m.Add(c, 5)
193 So(err, ShouldBeNil) 193 So(err, ShouldBeNil)
194 194
195 v, err = m.Get(c) 195 v, err = m.Get(c)
196 So(v.Bucketer().GrowthFactor(), ShouldEqual, 0) 196 So(v.Bucketer().GrowthFactor(), ShouldEqual, 0)
197 So(v.Bucketer().Width(), ShouldEqual, 10) 197 So(v.Bucketer().Width(), ShouldEqual, 10)
198 So(v.Bucketer().NumFiniteBuckets(), ShouldEqual, 20) 198 So(v.Bucketer().NumFiniteBuckets(), ShouldEqual, 20)
199 So(v.Sum(), ShouldEqual, 5) 199 So(v.Sum(), ShouldEqual, 5)
200 So(v.Count(), ShouldEqual, 1) 200 So(v.Count(), ShouldEqual, 1)
201 So(err, ShouldBeNil) 201 So(err, ShouldBeNil)
202 202
203 » » So(func() { NewCumulativeDistributionIn(c, "foo", "description", m.Bucketer()) }, ShouldPanic) 203 » » So(func() { NewCumulativeDistributionIn(c, "foo", "description", types.MetricMetadata{}, m.Bucketer()) }, ShouldPanic)
204 }) 204 })
205 205
206 Convey("NonCumulativeDistribution", t, func() { 206 Convey("NonCumulativeDistribution", t, func() {
207 c := makeContext() 207 c := makeContext()
208 » » m := NewNonCumulativeDistributionIn(c, "foo", "description", dis tribution.FixedWidthBucketer(10, 20)) 208 » » m := NewNonCumulativeDistributionIn(c, "foo", "description", typ es.MetricMetadata{}, distribution.FixedWidthBucketer(10, 20))
209 209
210 v, err := m.Get(c) 210 v, err := m.Get(c)
211 So(v, ShouldBeNil) 211 So(v, ShouldBeNil)
212 So(err, ShouldBeNil) 212 So(err, ShouldBeNil)
213 213
214 So(m.Bucketer().GrowthFactor(), ShouldEqual, 0) 214 So(m.Bucketer().GrowthFactor(), ShouldEqual, 0)
215 So(m.Bucketer().Width(), ShouldEqual, 10) 215 So(m.Bucketer().Width(), ShouldEqual, 10)
216 So(m.Bucketer().NumFiniteBuckets(), ShouldEqual, 20) 216 So(m.Bucketer().NumFiniteBuckets(), ShouldEqual, 20)
217 217
218 v, err = m.Get(c, "field") 218 v, err = m.Get(c, "field")
219 So(err, ShouldNotBeNil) 219 So(err, ShouldNotBeNil)
220 220
221 d := distribution.New(m.Bucketer()) 221 d := distribution.New(m.Bucketer())
222 d.Add(15) 222 d.Add(15)
223 err = m.Set(c, d) 223 err = m.Set(c, d)
224 So(err, ShouldBeNil) 224 So(err, ShouldBeNil)
225 225
226 v, err = m.Get(c) 226 v, err = m.Get(c)
227 So(v.Bucketer().GrowthFactor(), ShouldEqual, 0) 227 So(v.Bucketer().GrowthFactor(), ShouldEqual, 0)
228 So(v.Bucketer().Width(), ShouldEqual, 10) 228 So(v.Bucketer().Width(), ShouldEqual, 10)
229 So(v.Bucketer().NumFiniteBuckets(), ShouldEqual, 20) 229 So(v.Bucketer().NumFiniteBuckets(), ShouldEqual, 20)
230 So(v.Sum(), ShouldEqual, 15) 230 So(v.Sum(), ShouldEqual, 15)
231 So(v.Count(), ShouldEqual, 1) 231 So(v.Count(), ShouldEqual, 1)
232 So(err, ShouldBeNil) 232 So(err, ShouldBeNil)
233 233
234 » » So(func() { NewNonCumulativeDistributionIn(c, "foo", "descriptio n", m.Bucketer()) }, ShouldPanic) 234 » » So(func() { NewNonCumulativeDistributionIn(c, "foo", "descriptio n", types.MetricMetadata{}, m.Bucketer()) }, ShouldPanic)
235 }) 235 })
236 } 236 }
OLDNEW
« no previous file with comments | « common/tsmon/metric/metric.go ('k') | common/tsmon/metric/standard_metrics.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698