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.
6 from telemetry
.util
import perf_tests_helper
7 from telemetry
import value
as value_module
8 from telemetry
.value
import histogram_util
9 from telemetry
.value
import summarizable
12 class HistogramValueBucket(object):
13 def __init__(self
, low
, high
, count
=0):
25 def ToJSONString(self
):
26 return '{%s}' % ', '.join([
27 '"low": %i' % self
.low
,
28 '"high": %i' % self
.high
,
29 '"count": %i' % self
.count
])
31 class HistogramValue(summarizable
.SummarizableValue
):
32 def __init__(self
, page
, name
, units
,
33 raw_value
=None, raw_value_json
=None, important
=True,
34 description
=None, tir_label
=None, improvement_direction
=None,
36 super(HistogramValue
, self
).__init
__(page
, name
, units
, important
,
37 description
, tir_label
,
38 improvement_direction
, grouping_keys
)
40 assert raw_value
== None, \
41 'Don\'t specify both raw_value and raw_value_json'
42 raw_value
= json
.loads(raw_value_json
)
45 for bucket
in histogram_util
.GetHistogramBucketsFromRawValue(raw_value
):
46 self
.buckets
.append(HistogramValueBucket(
49 count
=bucket
['count']))
55 page_name
= self
.page
.display_name
58 return ('HistogramValue(%s, %s, %s, raw_json_string=%s, '
59 'important=%s, description=%s, tir_label=%s, '
60 'improvement_direction=%s, grouping_keys=%s)') % (
62 self
.name
, self
.units
,
67 self
.improvement_direction
,
70 def GetBuildbotDataType(self
, output_context
):
71 if self
._IsImportantGivenOutputIntent
(output_context
):
73 return 'unimportant-histogram'
75 def GetBuildbotValue(self
):
76 # More buildbot insanity: perf_tests_results_helper requires the histogram
77 # to be an array of size one.
78 return [self
.ToJSONString()]
80 def ToJSONString(self
):
81 # This has to hand-JSONify the histogram to ensure the order of keys
82 # produced is stable across different systems.
84 # This is done because the buildbot unittests are string equality
85 # assertions. Thus, tests that contain histograms require stable
86 # stringification of the histogram.
88 # Sigh, buildbot, Y U gotta be that way.
89 return '{"buckets": [%s]}' % (
90 ', '.join([b
.ToJSONString() for b
in self
.buckets
]))
92 def GetRepresentativeNumber(self
):
93 (mean
, _
) = perf_tests_helper
.GeomMeanAndStdDevFromHistogram(
97 def GetRepresentativeString(self
):
98 return self
.GetBuildbotValue()
101 def GetJSONTypeName():
105 d
= super(HistogramValue
, self
).AsDict()
106 d
['buckets'] = [b
.AsDict() for b
in self
.buckets
]
110 def FromDict(value_dict
, page_dict
):
111 kwargs
= value_module
.Value
.GetConstructorKwArgs(value_dict
, page_dict
)
112 kwargs
['raw_value'] = value_dict
114 if 'improvement_direction' in value_dict
:
115 kwargs
['improvement_direction'] = value_dict
['improvement_direction']
117 return HistogramValue(**kwargs
)
120 def MergeLikeValuesFromSamePage(cls
, values
):
121 assert len(values
) > 0
123 return HistogramValue(
124 v0
.page
, v0
.name
, v0
.units
,
125 raw_value_json
=histogram_util
.AddHistograms(
126 [v
.ToJSONString() for v
in values
]),
127 description
=v0
.description
,
128 important
=v0
.important
, tir_label
=value_module
.MergedTirLabel(values
),
129 improvement_direction
=v0
.improvement_direction
,
130 grouping_keys
=v0
.grouping_keys
)
133 def MergeLikeValuesFromDifferentPages(cls
, values
):
134 # Histograms cannot be merged across pages, at least for now. It should be
135 # theoretically possible, just requires more work. Instead, return None.
136 # This signals to the merging code that the data is unmergable and it will