1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 var sumTests = []sumTest{
26 type bucketingTest struct {
32 var bucketingTests = []bucketingTest{
44 type multiplyTest struct {
49 expectedSumOfSquares float64
52 var multiplyTests = []multiplyTest{
53 {15, 2.5, 37, 2, 562.5},
54 {128, 4.6, 758, 13, 77953.9},
57 type percentileTest struct {
62 var percentileTests = []percentileTest{
72 func TestSum(t *testing.T) {
75 for _, test := range sumTests {
76 h.addMeasurement(test.value)
79 t.Errorf("h.Sum = %v WANT: %v", sum, test.sum)
82 sumOfSquares := h.sumOfSquares
83 if sumOfSquares != test.sumOfSquares {
84 t.Errorf("h.SumOfSquares = %v WANT: %v", sumOfSquares, test.sumOfSquares)
88 if total != test.total {
89 t.Errorf("h.Total = %v WANT: %v", total, test.total)
94 func TestMultiply(t *testing.T) {
96 for i, test := range multiplyTests {
97 h.addMeasurement(test.in)
98 h.Multiply(test.ratio)
99 if h.sum != test.expectedSum {
100 t.Errorf("#%v: h.sum = %v WANT: %v", i, h.sum, test.expectedSum)
102 if h.total() != test.expectedTotal {
103 t.Errorf("#%v: h.total = %v WANT: %v", i, h.total(), test.expectedTotal)
105 if h.sumOfSquares != test.expectedSumOfSquares {
106 t.Errorf("#%v: h.SumOfSquares = %v WANT: %v", i, test.expectedSumOfSquares, h.sumOfSquares)
111 func TestBucketingFunctions(t *testing.T) {
112 for _, test := range bucketingTests {
115 t.Errorf("log2 = %v WANT: %v", log, test.log)
118 bucket := getBucket(test.in)
119 if bucket != test.bucket {
120 t.Errorf("getBucket = %v WANT: %v", bucket, test.bucket)
125 func TestAverage(t *testing.T) {
127 average := a.average()
129 t.Errorf("Average of empty histogram was %v WANT: 0", average)
135 const expected = float64(5) / float64(3)
136 average = a.average()
138 if !isApproximate(average, expected) {
139 t.Errorf("Average = %g WANT: %v", average, expected)
143 func TestStandardDeviation(t *testing.T) {
148 stdDev := a.standardDeviation()
149 const expected = 19.95
151 if !isApproximate(stdDev, expected) {
152 t.Errorf("StandardDeviation = %v WANT: %v", stdDev, expected)
157 stdDev = a.standardDeviation()
159 if !isApproximate(stdDev, 0) {
160 t.Errorf("StandardDeviation = %v WANT: 0", stdDev)
164 if !isApproximate(stdDev, 0) {
165 t.Errorf("StandardDeviation = %v WANT: 0", stdDev)
169 if !isApproximate(stdDev, 0) {
170 t.Errorf("StandardDeviation = %v WANT: 0", stdDev)
174 func TestPercentileBoundary(t *testing.T) {
180 for _, test := range percentileTests {
181 percentile := a.percentileBoundary(test.fraction)
182 if percentile != test.expected {
183 t.Errorf("h.PercentileBoundary (fraction=%v) = %v WANT: %v", test.fraction, percentile, test.expected)
188 func TestCopyFrom(t *testing.T) {
189 a := histogram{5, 25, []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
190 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38}, 4, -1}
191 b := histogram{6, 36, []int64{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
192 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39}, 5, -1}
196 if a.String() != b.String() {
197 t.Errorf("a.String = %s WANT: %s", a.String(), b.String())
201 func TestClear(t *testing.T) {
202 a := histogram{5, 25, []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
203 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38}, 4, -1}
207 expected := "0, 0.000000, 0, 0, []"
208 if a.String() != expected {
209 t.Errorf("a.String = %s WANT %s", a.String(), expected)
213 func TestNew(t *testing.T) {
214 a := histogram{5, 25, []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
215 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38}, 4, -1}
218 expected := "0, 0.000000, 0, 0, []"
219 if b.(*histogram).String() != expected {
220 t.Errorf("b.(*histogram).String = %s WANT: %s", b.(*histogram).String(), expected)
224 func TestAdd(t *testing.T) {
225 // The tests here depend on the associativity of addMeasurement and Add.
226 // Add empty observation
227 a := histogram{5, 25, []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
228 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38}, 4, -1}
231 expected := a.String()
233 if a.String() != expected {
234 t.Errorf("a.String = %s WANT: %s", a.String(), expected)
237 // Add same bucketed value, no new buckets
246 if c.String() != e.String() {
247 t.Errorf("c.String = %s WANT: %s", c.String(), e.String())
250 // Add bucketed values
256 f.addMeasurement(100)
259 g.addMeasurement(255)
262 h.addMeasurement(100)
265 h.addMeasurement(255)
267 if f.String() != h.String() {
268 t.Errorf("f.String = %q WANT: %q", f.String(), h.String())
271 // add buckets to no buckets
277 j.addMeasurement(255)
280 k.addMeasurement(255)
282 if i.String() != k.String() {
283 t.Errorf("i.String = %q WANT: %q", i.String(), k.String())
286 // add buckets to single value (no overlap)
293 m.addMeasurement(255)
297 n.addMeasurement(255)
299 if l.String() != n.String() {
300 t.Errorf("l.String = %q WANT: %q", l.String(), n.String())
312 if o.String() != p.String() {
313 t.Errorf("o.String = %q WANT: %q", o.String(), p.String())
317 func add(h *histogram, times int, val int64) {
318 for i := 0; i < times; i++ {
319 h.addMeasurement(val)
323 func isApproximate(x, y float64) bool {
324 return math.Abs(x-y) < 1e-2