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.
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
):
16 story_set
= story
.StorySet(base_dir
=os
.path
.dirname(__file__
))
18 page_module
.Page("http://www.bar.com/", story_set
, story_set
.base_dir
))
20 page_module
.Page("http://www.baz.com/", story_set
, story_set
.base_dir
))
22 page_module
.Page("http://www.foo.com/", story_set
, story_set
.base_dir
))
23 self
.story_set
= story_set
27 return self
.story_set
.stories
29 class ValueTest(TestBase
):
32 v
= histogram_module
.HistogramValue(
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
):
46 histogram
= histogram_module
.HistogramValue(
48 raw_value_json
='{"buckets": [{"low": 1, "high": 2, "count": 1}]}',
49 important
=False, improvement_direction
=improvement_direction
.UP
)
51 ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
52 histogram
.GetBuildbotValue())
53 self
.assertEquals(1.5,
54 histogram
.GetRepresentativeNumber())
56 ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
57 histogram
.GetBuildbotValue())
60 'unimportant-histogram',
61 histogram
.GetBuildbotDataType(value
.SUMMARY_RESULT_OUTPUT_CONTEXT
))
62 histogram
.important
= True
65 histogram
.GetBuildbotDataType(value
.SUMMARY_RESULT_OUTPUT_CONTEXT
))
67 def testBucketAsDict(self
):
68 bucket
= histogram_module
.HistogramValueBucket(33, 45, 78)
71 self
.assertEquals(d
, {
78 histogram
= histogram_module
.HistogramValue(
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
):
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
))
100 ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
101 v
.GetBuildbotValue())
102 self
.assertEquals(improvement_direction
.DOWN
, v
.improvement_direction
)
104 def testFromDictWithoutImprovementDirection(self
):
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
):
121 'description': 'histogram-based metric',
122 'buckets': [{'low': 1, 'high': 3, 'count': 1}],
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
)