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.
4
5import json
6import unittest
7
8from telemetry.value import histogram_util
9
10class TestHistogram(unittest.TestCase):
11  def testSubtractHistogram(self):
12    baseline_histogram = """{"count": 3, "buckets": [
13        {"low": 1, "high": 2, "count": 1},
14        {"low": 2, "high": 3, "count": 2}]}"""
15
16    later_histogram = """{"count": 14, "buckets": [
17        {"low": 1, "high": 2, "count": 1},
18        {"low": 2, "high": 3, "count": 3},
19        {"low": 3, "high": 4, "count": 10}]}"""
20
21    new_histogram = json.loads(
22        histogram_util.SubtractHistogram(later_histogram, baseline_histogram))
23    new_buckets = dict()
24    for b in new_histogram['buckets']:
25      new_buckets[b['low']] = b['count']
26    self.assertFalse(1 in new_buckets)
27    self.assertEquals(1, new_buckets[2])
28    self.assertEquals(10, new_buckets[3])
29
30
31  def testAddHistograms(self):
32    histograms = []
33    histograms.append("""{"count": 3, "buckets": [
34        {"low": 1, "high": 2, "count": 1},
35        {"low": 2, "high": 3, "count": 2}]}""")
36
37    histograms.append("""{"count": 20, "buckets": [
38        {"low": 2, "high": 3, "count": 10},
39        {"low": 3, "high": 4, "count": 10}]}""")
40
41    histograms.append("""{"count": 15, "buckets": [
42        {"low": 1, "high": 2, "count": 4},
43        {"low": 3, "high": 4, "count": 11}]}""")
44
45    new_histogram = json.loads(
46        histogram_util.AddHistograms(histograms))
47    new_buckets = dict()
48    for b in new_histogram['buckets']:
49      new_buckets[b['low']] = b['count']
50    self.assertEquals(5, new_buckets[1])
51    self.assertEquals(12, new_buckets[2])
52    self.assertEquals(21, new_buckets[3])
53