OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
qyearsley
2013/08/08 17:35:52
This module contains basically what was just in 'h
| |
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 from metrics import histogram_util | 4 |
5 """This is a helper module to get and manipulate histogram data. | |
6 | |
7 The histogram data is the same data as is visible from "chrome://histograms". | |
8 More information can be found at: chromium/src/base/metrics/histogram.h | |
9 | |
10 Histogram data is collected with either the window.statsCollectionController | |
11 object or the window.domAutomationController object. | |
12 """ | |
13 | |
14 import json | |
15 import logging | |
5 | 16 |
6 BROWSER_HISTOGRAM = 'browser_histogram' | 17 BROWSER_HISTOGRAM = 'browser_histogram' |
7 RENDERER_HISTOGRAM = 'renderer_histogram' | 18 RENDERER_HISTOGRAM = 'renderer_histogram' |
8 | 19 |
9 class HistogramMetric(object): | 20 def GetHistogramData(histogram_type, histogram_name, tab): |
qyearsley
2013/08/08 17:35:52
I renamed GetHistogramFromDomAutomation to GetHist
| |
10 def __init__(self, histogram, histogram_type): | 21 """Get a json serialization of a histogram.""" |
11 self.name = histogram['name'] | 22 assert histogram_type in [BROWSER_HISTOGRAM, RENDERER_HISTOGRAM] |
12 self.units = histogram['units'] | 23 function = 'getHistogram' |
13 self.histogram_type = histogram_type | 24 if histogram_type == BROWSER_HISTOGRAM: |
14 self._start_values = dict() | 25 function = 'getBrowserHistogram' |
26 # TODO(jeremy): Remove references to | |
27 # domAutomationController when we update the reference builds. | |
28 return tab.EvaluateJavaScript( | |
29 '(window.statsCollectionController ? ' | |
30 'statsCollectionController : ' | |
31 'domAutomationController).%s("%s")' % | |
32 (function, histogram_name)) | |
15 | 33 |
16 def Start(self, page, tab): | |
17 """Get the starting value for the histogram. This value will then be | |
18 subtracted from the actual measurement.""" | |
19 data = self._GetHistogramFromDomAutomation(tab) | |
20 if data: | |
21 self._start_values[page.url + self.name] = data | |
22 | 34 |
23 def GetValue(self, page, tab, results): | 35 def SubtractHistogram(histogram_json, start_histogram_json): |
24 data = self._GetHistogramFromDomAutomation(tab) | 36 """Subtracts a previous histogram from a histogram. |
25 if not data: | |
26 return | |
27 new_histogram = histogram_util.SubtractHistogram( | |
28 data, self._start_values[page.url + self.name]) | |
29 results.Add(self.name, self.units, new_histogram, | |
30 data_type='unimportant-histogram') | |
31 | 37 |
32 @property | 38 Both parameters and the returned result are json serializations. |
33 def histogram_function(self): | 39 """ |
34 if self.histogram_type == BROWSER_HISTOGRAM: | 40 start_histogram = json.loads(start_histogram_json) |
35 return 'getBrowserHistogram' | 41 # It's ok if the start histogram is empty (we had no data, maybe even no |
36 return 'getHistogram' | 42 # histogram at all, at the start of the test). |
43 if 'buckets' not in start_histogram: | |
44 return histogram_json | |
37 | 45 |
38 def _GetHistogramFromDomAutomation(self, tab): | 46 histogram = json.loads(histogram_json) |
39 return histogram_util.GetHistogramFromDomAutomation( | 47 if ('pid' in start_histogram and 'pid' in histogram |
40 self.histogram_function, self.name, tab) | 48 and start_histogram['pid'] != histogram['pid']): |
49 raise Exception( | |
50 'Trying to compare histograms from different processes (%d and %d)' | |
51 % (start_histogram['pid'], histogram['pid'])) | |
52 | |
53 start_histogram_buckets = dict() | |
54 for b in start_histogram['buckets']: | |
55 start_histogram_buckets[b['low']] = b['count'] | |
56 | |
57 new_buckets = [] | |
58 for b in histogram['buckets']: | |
59 new_bucket = b | |
60 low = b['low'] | |
61 if low in start_histogram_buckets: | |
62 new_bucket['count'] = b['count'] - start_histogram_buckets[low] | |
63 if new_bucket['count'] < 0: | |
64 logging.error('Histogram subtraction error, starting histogram most ' | |
65 'probably invalid.') | |
66 if new_bucket['count']: | |
67 new_buckets.append(new_bucket) | |
68 histogram['buckets'] = new_buckets | |
69 histogram['count'] -= start_histogram['count'] | |
70 | |
71 return json.dumps(histogram) | |
72 | |
OLD | NEW |