Update sdk/platform-tools to version 26.0.0.
[android_tools.git] / sdk / platform-tools / systrace / catapult / telemetry / telemetry / value / histogram_unittest.py
blob3e8e66218dae898a5a8a1c3f948a8fb658f51b3e
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.
4 import os
5 import unittest
7 from telemetry import story
8 from telemetry import page as page_module
9 from telemetry import value
10 from telemetry.value import histogram as histogram_module
11 from telemetry.value import improvement_direction
14 class TestBase(unittest.TestCase):
15 def setUp(self):
16 story_set = story.StorySet(base_dir=os.path.dirname(__file__))
17 story_set.AddStory(
18 page_module.Page("http://www.bar.com/", story_set, story_set.base_dir))
19 story_set.AddStory(
20 page_module.Page("http://www.baz.com/", story_set, story_set.base_dir))
21 story_set.AddStory(
22 page_module.Page("http://www.foo.com/", story_set, story_set.base_dir))
23 self.story_set = story_set
25 @property
26 def pages(self):
27 return self.story_set.stories
29 class ValueTest(TestBase):
30 def testRepr(self):
31 page = self.pages[0]
32 v = histogram_module.HistogramValue(
33 page, 'x', 'counts',
34 raw_value_json='{"buckets": [{"low": 1, "high": 2, "count": 1}]}',
35 important=True, description='desc', tir_label='my_ir',
36 improvement_direction=improvement_direction.UP)
37 expected = ('HistogramValue(http://www.bar.com/, x, counts, '
38 'raw_json_string={"buckets": [{"low": 1, "high": 2, "count": '
39 '1}]}, important=True, description=desc, tir_label=my_ir, '
40 'improvement_direction=up, grouping_keys={})')
42 self.assertEquals(expected, str(v))
44 def testHistogramBasic(self):
45 page0 = self.pages[0]
46 histogram = histogram_module.HistogramValue(
47 page0, 'x', 'counts',
48 raw_value_json='{"buckets": [{"low": 1, "high": 2, "count": 1}]}',
49 important=False, improvement_direction=improvement_direction.UP)
50 self.assertEquals(
51 ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
52 histogram.GetBuildbotValue())
53 self.assertEquals(1.5,
54 histogram.GetRepresentativeNumber())
55 self.assertEquals(
56 ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
57 histogram.GetBuildbotValue())
59 self.assertEquals(
60 'unimportant-histogram',
61 histogram.GetBuildbotDataType(value.SUMMARY_RESULT_OUTPUT_CONTEXT))
62 histogram.important = True
63 self.assertEquals(
64 'histogram',
65 histogram.GetBuildbotDataType(value.SUMMARY_RESULT_OUTPUT_CONTEXT))
67 def testBucketAsDict(self):
68 bucket = histogram_module.HistogramValueBucket(33, 45, 78)
69 d = bucket.AsDict()
71 self.assertEquals(d, {
72 'low': 33,
73 'high': 45,
74 'count': 78
77 def testAsDict(self):
78 histogram = histogram_module.HistogramValue(
79 None, 'x', 'counts',
80 raw_value_json='{"buckets": [{"low": 1, "high": 2, "count": 1}]}',
81 important=False, improvement_direction=improvement_direction.DOWN)
82 d = histogram.AsDictWithoutBaseClassEntries()
84 self.assertEquals(['buckets'], d.keys())
85 self.assertTrue(isinstance(d['buckets'], list))
86 self.assertEquals(len(d['buckets']), 1)
88 def testFromDict(self):
89 d = {
90 'type': 'histogram',
91 'name': 'x',
92 'units': 'counts',
93 'buckets': [{'low': 1, 'high': 2, 'count': 1}],
94 'improvement_direction': 'down',
96 v = value.Value.FromDict(d, {})
98 self.assertTrue(isinstance(v, histogram_module.HistogramValue))
99 self.assertEquals(
100 ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
101 v.GetBuildbotValue())
102 self.assertEquals(improvement_direction.DOWN, v.improvement_direction)
104 def testFromDictWithoutImprovementDirection(self):
105 d = {
106 'type': 'histogram',
107 'name': 'x',
108 'units': 'counts',
109 'buckets': [{'low': 1, 'high': 2, 'count': 1}],
111 v = value.Value.FromDict(d, {})
113 self.assertTrue(isinstance(v, histogram_module.HistogramValue))
114 self.assertIsNone(v.improvement_direction)
116 def testMergeLikeValuesFromSamePage(self):
117 d1 = {
118 'type': 'histogram',
119 'name': 'x',
120 'units': 'counts',
121 'description': 'histogram-based metric',
122 'buckets': [{'low': 1, 'high': 3, 'count': 1}],
125 d2 = {
126 'type': 'histogram',
127 'name': 'x',
128 'units': 'counts',
129 'description': 'histogram-based metric',
130 'buckets': [{'low': 2, 'high': 4, 'count': 1}],
133 v0, v1 = value.Value.FromDict(d1, {}), value.Value.FromDict(d2, {})
135 vM = histogram_module.HistogramValue.MergeLikeValuesFromSamePage([v0, v1])
136 self.assertTrue(isinstance(vM, histogram_module.HistogramValue))
137 self.assertEquals('histogram-based metric', vM.description)