histogram_unittest.py revision 33259e44c8229f70ffe0cf3bb5ca9375c4feb2f9
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.
4import os
5import unittest
6
7from telemetry import story
8from telemetry import page as page_module
9from telemetry import value
10from telemetry.value import histogram as histogram_module
11from telemetry.value import improvement_direction
12
13
14class 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
24
25  @property
26  def pages(self):
27    return self.story_set.stories
28
29class 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={})')
41
42    self.assertEquals(expected, str(v))
43
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())
58
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))
66
67  def testBucketAsDict(self):
68    bucket = histogram_module.HistogramValueBucket(33, 45, 78)
69    d = bucket.AsDict()
70
71    self.assertEquals(d, {
72          'low': 33,
73          'high': 45,
74          'count': 78
75        })
76
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()
83
84    self.assertEquals(['buckets'], d.keys())
85    self.assertTrue(isinstance(d['buckets'], list))
86    self.assertEquals(len(d['buckets']), 1)
87
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',
95    }
96    v = value.Value.FromDict(d, {})
97
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)
103
104  def testFromDictWithoutImprovementDirection(self):
105    d = {
106      'type': 'histogram',
107      'name': 'x',
108      'units': 'counts',
109      'buckets': [{'low': 1, 'high': 2, 'count': 1}],
110    }
111    v = value.Value.FromDict(d, {})
112
113    self.assertTrue(isinstance(v, histogram_module.HistogramValue))
114    self.assertIsNone(v.improvement_direction)
115
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}],
123    }
124
125    d2 = {
126      'type': 'histogram',
127      'name': 'x',
128      'units': 'counts',
129      'description': 'histogram-based metric',
130      'buckets': [{'low': 2, 'high': 4, 'count': 1}],
131    }
132
133    v0, v1 = value.Value.FromDict(d1, {}), value.Value.FromDict(d2, {})
134
135    vM = histogram_module.HistogramValue.MergeLikeValuesFromSamePage([v0, v1])
136    self.assertTrue(isinstance(vM, histogram_module.HistogramValue))
137    self.assertEquals('histogram-based metric', vM.description)
138