| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 import json | 4 import json |
| 5 | 5 |
| 6 from telemetry import perf_tests_helper |
| 6 from telemetry import value as value_module | 7 from telemetry import value as value_module |
| 7 from telemetry import perf_tests_helper | |
| 8 from telemetry.value import histogram_util | 8 from telemetry.value import histogram_util |
| 9 from telemetry.value import summarizable |
| 9 | 10 |
| 10 class HistogramValueBucket(object): | 11 class HistogramValueBucket(object): |
| 11 def __init__(self, low, high, count=0): | 12 def __init__(self, low, high, count=0): |
| 12 self.low = low | 13 self.low = low |
| 13 self.high = high | 14 self.high = high |
| 14 self.count = count | 15 self.count = count |
| 15 | 16 |
| 16 def AsDict(self): | 17 def AsDict(self): |
| 17 return { | 18 return { |
| 18 'low': self.low, | 19 'low': self.low, |
| 19 'high': self.high, | 20 'high': self.high, |
| 20 'count': self.count | 21 'count': self.count |
| 21 } | 22 } |
| 22 | 23 |
| 23 def ToJSONString(self): | 24 def ToJSONString(self): |
| 24 return '{%s}' % ', '.join([ | 25 return '{%s}' % ', '.join([ |
| 25 '"low": %i' % self.low, | 26 '"low": %i' % self.low, |
| 26 '"high": %i' % self.high, | 27 '"high": %i' % self.high, |
| 27 '"count": %i' % self.count]) | 28 '"count": %i' % self.count]) |
| 28 | 29 |
| 29 class HistogramValue(value_module.Value): | 30 class HistogramValue(summarizable.SummarizableValue): |
| 30 def __init__(self, page, name, units, | 31 def __init__(self, page, name, units, |
| 31 raw_value=None, raw_value_json=None, important=True, | 32 raw_value=None, raw_value_json=None, important=True, |
| 32 description=None): | 33 description=None, improvement_direction=None): |
| 33 super(HistogramValue, self).__init__(page, name, units, important, | 34 super(HistogramValue, self).__init__(page, name, units, important, |
| 34 description) | 35 description, improvement_direction) |
| 35 if raw_value_json: | 36 if raw_value_json: |
| 36 assert raw_value == None, \ | 37 assert raw_value == None, \ |
| 37 'Don\'t specify both raw_value and raw_value_json' | 38 'Don\'t specify both raw_value and raw_value_json' |
| 38 raw_value = json.loads(raw_value_json) | 39 raw_value = json.loads(raw_value_json) |
| 39 if raw_value: | 40 if raw_value: |
| 40 assert 'buckets' in raw_value | 41 assert 'buckets' in raw_value |
| 41 assert isinstance(raw_value['buckets'], list) | 42 assert isinstance(raw_value['buckets'], list) |
| 42 self.buckets = [] | 43 self.buckets = [] |
| 43 for bucket in raw_value['buckets']: | 44 for bucket in raw_value['buckets']: |
| 44 self.buckets.append(HistogramValueBucket( | 45 self.buckets.append(HistogramValueBucket( |
| 45 low=bucket['low'], | 46 low=bucket['low'], |
| 46 high=bucket['high'], | 47 high=bucket['high'], |
| 47 count=bucket['count'])) | 48 count=bucket['count'])) |
| 48 else: | 49 else: |
| 49 self.buckets = [] | 50 self.buckets = [] |
| 50 | 51 |
| 51 def __repr__(self): | 52 def __repr__(self): |
| 52 if self.page: | 53 if self.page: |
| 53 page_name = self.page.url | 54 page_name = self.page.url |
| 54 else: | 55 else: |
| 55 page_name = None | 56 page_name = None |
| 56 return ('HistogramValue(%s, %s, %s, raw_json_string="%s", ' | 57 return ('HistogramValue(%s, %s, %s, raw_json_string="%s", ' |
| 57 'important=%s, description=%s') % ( | 58 'important=%s, description=%s, improvement_direction=%s') % ( |
| 58 page_name, | 59 page_name, |
| 59 self.name, self.units, | 60 self.name, self.units, |
| 60 self.ToJSONString(), | 61 self.ToJSONString(), |
| 61 self.important, | 62 self.important, |
| 62 self.description) | 63 self.description, |
| 64 self.improvement_direction) |
| 63 | 65 |
| 64 def GetBuildbotDataType(self, output_context): | 66 def GetBuildbotDataType(self, output_context): |
| 65 if self._IsImportantGivenOutputIntent(output_context): | 67 if self._IsImportantGivenOutputIntent(output_context): |
| 66 return 'histogram' | 68 return 'histogram' |
| 67 return 'unimportant-histogram' | 69 return 'unimportant-histogram' |
| 68 | 70 |
| 69 def GetBuildbotValue(self): | 71 def GetBuildbotValue(self): |
| 70 # More buildbot insanity: perf_tests_results_helper requires the histogram | 72 # More buildbot insanity: perf_tests_results_helper requires the histogram |
| 71 # to be an array of size one. | 73 # to be an array of size one. |
| 72 return [self.ToJSONString()] | 74 return [self.ToJSONString()] |
| (...skipping 24 matching lines...) Expand all Loading... |
| 97 | 99 |
| 98 def AsDict(self): | 100 def AsDict(self): |
| 99 d = super(HistogramValue, self).AsDict() | 101 d = super(HistogramValue, self).AsDict() |
| 100 d['buckets'] = [b.AsDict() for b in self.buckets] | 102 d['buckets'] = [b.AsDict() for b in self.buckets] |
| 101 return d | 103 return d |
| 102 | 104 |
| 103 @staticmethod | 105 @staticmethod |
| 104 def FromDict(value_dict, page_dict): | 106 def FromDict(value_dict, page_dict): |
| 105 kwargs = value_module.Value.GetConstructorKwArgs(value_dict, page_dict) | 107 kwargs = value_module.Value.GetConstructorKwArgs(value_dict, page_dict) |
| 106 kwargs['raw_value'] = value_dict | 108 kwargs['raw_value'] = value_dict |
| 109 kwargs['improvement_direction'] = value_dict['improvement_direction'] |
| 107 | 110 |
| 108 return HistogramValue(**kwargs) | 111 return HistogramValue(**kwargs) |
| 109 | 112 |
| 110 @classmethod | 113 @classmethod |
| 111 def MergeLikeValuesFromSamePage(cls, values): | 114 def MergeLikeValuesFromSamePage(cls, values): |
| 112 assert len(values) > 0 | 115 assert len(values) > 0 |
| 113 v0 = values[0] | 116 v0 = values[0] |
| 114 return HistogramValue( | 117 return HistogramValue( |
| 115 v0.page, v0.name, v0.units, | 118 v0.page, v0.name, v0.units, |
| 116 raw_value_json=histogram_util.AddHistograms( | 119 raw_value_json=histogram_util.AddHistograms( |
| 117 [v.ToJSONString() for v in values]), | 120 [v.ToJSONString() for v in values]), |
| 118 important=v0.important) | 121 important=v0.important, improvement_direction=v0.improvement_direction) |
| 119 | 122 |
| 120 @classmethod | 123 @classmethod |
| 121 def MergeLikeValuesFromDifferentPages(cls, values, | 124 def MergeLikeValuesFromDifferentPages(cls, values, |
| 122 group_by_name_suffix=False): | 125 group_by_name_suffix=False): |
| 123 # Histograms cannot be merged across pages, at least for now. It should be | 126 # Histograms cannot be merged across pages, at least for now. It should be |
| 124 # theoretically possible, just requires more work. Instead, return None. | 127 # theoretically possible, just requires more work. Instead, return None. |
| 125 # This signals to the merging code that the data is unmergable and it will | 128 # This signals to the merging code that the data is unmergable and it will |
| 126 # cope accordingly. | 129 # cope accordingly. |
| 127 return None | 130 return None |
| OLD | NEW |