1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
8 from telemetry
.value
import histogram_util
10 class TestHistogram(unittest
.TestCase
):
11 def testSubtractHistogram(self
):
12 baseline_histogram
= """{"count": 3, "buckets": [
13 {"low": 1, "high": 2, "count": 1},
14 {"low": 2, "high": 3, "count": 2}]}"""
16 later_histogram
= """{"count": 14, "buckets": [
17 {"low": 1, "high": 2, "count": 1},
18 {"low": 2, "high": 3, "count": 3},
19 {"low": 3, "high": 4, "count": 10}]}"""
21 new_histogram
= json
.loads(
22 histogram_util
.SubtractHistogram(later_histogram
, baseline_histogram
))
24 for b
in new_histogram
['buckets']:
25 new_buckets
[b
['low']] = b
['count']
26 self
.assertFalse(1 in new_buckets
)
27 self
.assertEquals(1, new_buckets
[2])
28 self
.assertEquals(10, new_buckets
[3])
31 def testAddHistograms(self
):
33 histograms
.append("""{"count": 3, "buckets": [
34 {"low": 1, "high": 2, "count": 1},
35 {"low": 2, "high": 3, "count": 2}]}""")
37 histograms
.append("""{"count": 20, "buckets": [
38 {"low": 2, "high": 3, "count": 10},
39 {"low": 3, "high": 4, "count": 10}]}""")
41 histograms
.append("""{"count": 15, "buckets": [
42 {"low": 1, "high": 2, "count": 4},
43 {"low": 3, "high": 4, "count": 11}]}""")
45 new_histogram
= json
.loads(
46 histogram_util
.AddHistograms(histograms
))
48 for b
in new_histogram
['buckets']:
49 new_buckets
[b
['low']] = b
['count']
50 self
.assertEquals(5, new_buckets
[1])
51 self
.assertEquals(12, new_buckets
[2])
52 self
.assertEquals(21, new_buckets
[3])
55 def testGetHistogramBucketsFromRawValue_Max(self
):
56 raw_value
= {'buckets': [
57 {'count': 4, 'low': 10, 'high': 15,},
58 {'count': 6, 'low': 16, 'high': 18,},
59 {'count': 8, 'low': 19},
61 buckets
= histogram_util
.GetHistogramBucketsFromRawValue(raw_value
)
63 {'count': 4, 'low': 10, 'high': 15,},
64 {'count': 6, 'low': 16, 'high': 18,},
65 {'count': 8, 'low': 19, 'high': 19},],
69 def testGetHistogramBucketsFromJson(self
):
70 json_value
= json
.dumps({'buckets': [
71 {'count': 4, 'low': 10, 'high': 15,},
72 {'count': 6, 'low': 16, 'high': 18,},
73 {'count': 8, 'low': 19, 'high': 25},
75 buckets
= histogram_util
.GetHistogramBucketsFromJson(json_value
)
77 {'count': 4, 'low': 10, 'high': 15,},
78 {'count': 6, 'low': 16, 'high': 18,},
79 {'count': 8, 'low': 19, 'high': 25},],