OLD | NEW |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 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 import logging | 5 import logging |
6 | 6 |
7 def SubtractHistogram(histogram_json, start_histogram_json): | 7 def SubtractHistogram(histogram_json, start_histogram_json): |
8 """Subtracts a previous histogram from a histogram. Both parameters are json | 8 """Subtracts a previous histogram from a histogram. Both parameters are json |
9 serializations of histograms.""" | 9 serializations of histograms.""" |
10 start_histogram = json.loads(start_histogram_json) | 10 start_histogram = json.loads(start_histogram_json) |
11 # It's ok if the start histogram is empty (we had no data, maybe even no | 11 # It's ok if the start histogram is empty (we had no data, maybe even no |
12 # histogram at all, at the start of the test). | 12 # histogram at all, at the start of the test). |
13 if 'buckets' not in start_histogram: | 13 if 'buckets' not in start_histogram: |
14 return histogram_json | 14 return histogram_json |
15 | 15 |
16 histogram = json.loads(histogram_json) | |
17 if ('pid' in start_histogram and 'pid' in histogram | |
18 and start_histogram['pid'] != histogram['pid']): | |
19 raise Exception( | |
20 'Trying to compare histograms from different processes (%d and %d)' | |
21 % (start_histogram['pid'], histogram['pid'])) | |
22 | |
23 start_histogram_buckets = dict() | 16 start_histogram_buckets = dict() |
24 for b in start_histogram['buckets']: | 17 for b in start_histogram['buckets']: |
25 start_histogram_buckets[b['low']] = b['count'] | 18 start_histogram_buckets[b['low']] = b['count'] |
26 | 19 |
27 new_buckets = [] | 20 new_buckets = [] |
| 21 histogram = json.loads(histogram_json) |
28 for b in histogram['buckets']: | 22 for b in histogram['buckets']: |
29 new_bucket = b | 23 new_bucket = b |
30 low = b['low'] | 24 low = b['low'] |
31 if low in start_histogram_buckets: | 25 if low in start_histogram_buckets: |
32 new_bucket['count'] = b['count'] - start_histogram_buckets[low] | 26 new_bucket['count'] = b['count'] - start_histogram_buckets[low] |
33 if new_bucket['count'] < 0: | 27 if new_bucket['count'] < 0: |
34 logging.error('Histogram subtraction error, starting histogram most ' | 28 logging.error('Histogram subtraction error, starting histogram most ' |
35 'probably invalid.') | 29 'probably invalid.') |
36 if new_bucket['count']: | 30 if new_bucket['count']: |
37 new_buckets.append(new_bucket) | 31 new_buckets.append(new_bucket) |
38 histogram['buckets'] = new_buckets | 32 histogram['buckets'] = new_buckets |
39 histogram['count'] -= start_histogram['count'] | 33 histogram['count'] -= start_histogram['count'] |
40 | 34 |
41 return json.dumps(histogram) | 35 return json.dumps(histogram) |
OLD | NEW |