Commit adddbecc authored by marja's avatar marja Committed by Commit bot

Telemetry: add missing functionality to aggregate histograms.

This way we'll get saner and less noisy histogram-based results.

Rationale for moving histogram_utils.py: HistogramValue is part of
telemetry.value, so it makes sense that all utils it needs are there too;
doesn't make sense to import the utils from metrics.

BUG=

Review URL: https://codereview.chromium.org/578593004

Cr-Commit-Position: refs/heads/master@{#295254}
parent f8269e33
......@@ -10,7 +10,7 @@ the entire test rather than each single page.
import logging
from metrics import histogram_util
from telemetry.value import histogram_util
from telemetry.page import page_test
......
......@@ -6,9 +6,9 @@ import collections
from measurements import startup
from metrics import cpu
from metrics import histogram_util
from metrics import startup_metric
from telemetry.core import util
from telemetry.value import histogram_util
class SessionRestore(startup.Startup):
......
......@@ -12,11 +12,11 @@ Power usage is also measured.
import time
from metrics import histogram_util
from metrics import power
from telemetry.core import util
from telemetry.page import page_test
from telemetry.value import histogram
from telemetry.value import histogram_util
# TODO: Revisit this test once multitab support is finalized.
......
......@@ -4,9 +4,9 @@
import sys
from metrics import histogram_util
from metrics import Metric
from telemetry.value import histogram
from telemetry.value import histogram_util
from telemetry.value import scalar
......
......@@ -6,9 +6,9 @@ import json
import logging
from metrics import Metric
from metrics import histogram_util
from telemetry.core import util
from telemetry.value import histogram_util
from telemetry.value import scalar
......
......@@ -5,7 +5,7 @@ import json
from telemetry import value as value_module
from telemetry import perf_tests_helper
from telemetry.value import histogram_util
class HistogramValueBucket(object):
def __init__(self, low, high, count=0):
......@@ -113,7 +113,8 @@ class HistogramValue(value_module.Value):
v0 = values[0]
return HistogramValue(
v0.page, v0.name, v0.units,
raw_value_json=v0.ToJSONString(),
raw_value_json=histogram_util.AddHistograms(
[v.ToJSONString() for v in values]),
important=v0.important)
@classmethod
......
......@@ -8,6 +8,7 @@ The histogram data is the same data as is visible from "chrome://histograms".
More information can be found at: chromium/src/base/metrics/histogram.h
"""
import collections
import json
import logging
......@@ -59,6 +60,32 @@ def SubtractHistogram(histogram_json, start_histogram_json):
return json.dumps(histogram)
def AddHistograms(histogram_jsons):
"""Adds histograms together. Used for aggregating data.
The parameter is a list of json serializations and the returned result is a
json serialization too.
Note that the histograms to be added together are typically from different
processes.
"""
buckets = collections.defaultdict(int)
for histogram_json in histogram_jsons:
h = json.loads(histogram_json)
for b in h['buckets']:
key = (b['low'], b['high'])
buckets[key] += b['count']
buckets = [{'low': key[0], 'high': key[1], 'count': value}
for key, value in buckets.iteritems()]
buckets.sort(key = lambda h : h['low'])
result_histogram = {}
result_histogram['buckets'] = buckets
return json.dumps(result_histogram)
def GetHistogram(histogram_type, histogram_name, tab):
"""Get a json serialization of a histogram."""
assert histogram_type in [BROWSER_HISTOGRAM, RENDERER_HISTOGRAM]
......
......@@ -5,7 +5,7 @@
import json
import unittest
from metrics import histogram_util
from telemetry.value import histogram_util
class TestHistogram(unittest.TestCase):
def testSubtractHistogram(self):
......@@ -26,3 +26,27 @@ class TestHistogram(unittest.TestCase):
self.assertFalse(1 in new_buckets)
self.assertEquals(1, new_buckets[2])
self.assertEquals(10, new_buckets[3])
def testAddHistograms(self):
histograms = []
histograms.append("""{"count": 3, "buckets": [
{"low": 1, "high": 2, "count": 1},
{"low": 2, "high": 3, "count": 2}]}""")
histograms.append("""{"count": 20, "buckets": [
{"low": 2, "high": 3, "count": 10},
{"low": 3, "high": 4, "count": 10}]}""")
histograms.append("""{"count": 15, "buckets": [
{"low": 1, "high": 2, "count": 4},
{"low": 3, "high": 4, "count": 11}]}""")
new_histogram = json.loads(
histogram_util.AddHistograms(histograms))
new_buckets = dict()
for b in new_histogram['buckets']:
new_buckets[b['low']] = b['count']
self.assertEquals(5, new_buckets[1])
self.assertEquals(12, new_buckets[2])
self.assertEquals(21, new_buckets[3])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment