| <!DOCTYPE html> |
| <!-- |
| Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| Use of this source code is governed by a BSD-style license that can be |
| found in the LICENSE file. |
| --> |
| <link rel="import" href="/tracing/base/units/histogram.html"> |
| |
| <script> |
| 'use strict'; |
| |
| tr.b.unittest.testSuite(function() { |
| test('basic', function() { |
| var h = new tr.b.u.Histogram.createLinear( |
| tr.b.u.Units.timeDurationInMs, |
| tr.b.Range.fromExplicitRange(0, 1000), |
| 10); |
| assert.equal(h.getBinForValue(250).range.min, 200); |
| assert.equal(h.getBinForValue(250).range.max, 300); |
| h.add(-1, 'a'); |
| h.add(0, 'b'); |
| h.add(0, 'c'); |
| h.add(500, 'c'); |
| h.add(999, 'd'); |
| h.add(1000, 'd'); |
| assert.equal(h.underflowBin.count, 1); |
| |
| assert.equal(h.getBinForValue(0).count, 2); |
| assert.deepEqual(h.getBinForValue(0).sourceInfos, |
| ['b', 'c']); |
| |
| assert.equal(h.getBinForValue(500).count, 1); |
| assert.equal(h.getBinForValue(999).count, 1); |
| |
| assert.equal(h.overflowBin.count, 1); |
| assert.equal(h.numValues, 6); |
| assert.closeTo(h.average, 416.3, 0.1); |
| }); |
| |
| test('nans', function() { |
| var h = new tr.b.u.Histogram.createLinear( |
| tr.b.u.Units.timeDurationInMs, |
| tr.b.Range.fromExplicitRange(0, 1000), |
| 10); |
| h.add(undefined, 'b'); |
| h.add(NaN, 'c'); |
| |
| assert.equal(h.numNans, 2); |
| assert.deepEqual(h.nanSourceInfos, ['b', 'c']); |
| }); |
| |
| test('addHistogramsValid', function() { |
| var h0 = new tr.b.u.Histogram.createLinear( |
| tr.b.u.Units.timeDurationInMs, |
| tr.b.Range.fromExplicitRange(0, 1000), |
| 10); |
| var h1 = new tr.b.u.Histogram.createLinear( |
| tr.b.u.Units.timeDurationInMs, |
| tr.b.Range.fromExplicitRange(0, 1000), |
| 10); |
| h0.add(-1, 'a0'); |
| h0.add(0, 'b0'); |
| h0.add(0, 'c0'); |
| h0.add(500, 'c0'); |
| h0.add(1000, 'd0'); |
| h0.add(NaN, 'e0'); |
| |
| h1.add(-1, 'a1'); |
| h1.add(0, 'b1'); |
| h1.add(0, 'c1'); |
| h1.add(999, 'd1'); |
| h1.add(1000, 'd1'); |
| h1.add(NaN, 'e1'); |
| |
| h0.addHistogram(h1); |
| |
| assert.equal(h0.numNans, 2); |
| assert.deepEqual(h0.nanSourceInfos, ['e0', 'e1']); |
| |
| assert.equal(h0.underflowBin.count, 2); |
| assert.deepEqual(h0.underflowBin.sourceInfos, ['a0', 'a1']); |
| |
| assert.equal(h0.getBinForValue(0).count, 4); |
| assert.deepEqual(h0.getBinForValue(0).sourceInfos, |
| ['b0', 'c0', 'b1', 'c1']); |
| |
| assert.equal(h0.getBinForValue(500).count, 1); |
| assert.deepEqual(h0.getBinForValue(500).sourceInfos, ['c0']); |
| |
| assert.equal(h0.getBinForValue(999).count, 1); |
| assert.deepEqual(h0.getBinForValue(999).sourceInfos, ['d1']); |
| |
| assert.equal(h0.overflowBin.count, 2); |
| assert.deepEqual(h0.overflowBin.sourceInfos, ['d0', 'd1']); |
| |
| assert.equal(h0.numValues, 10); |
| assert.closeTo(h0.average, 349.7, 0.1); |
| |
| assert.equal(2, h0.maxCount); |
| assert.equal(2, h1.maxCount); |
| }); |
| |
| test('addHistogramsInvalid', function() { |
| var h0 = new tr.b.u.Histogram.createLinear( |
| tr.b.u.Units.timeDurationInMs, |
| tr.b.Range.fromExplicitRange(0, 1000), |
| 10); |
| var h1 = new tr.b.u.Histogram.createLinear( |
| tr.b.u.Units.timeDurationInMs, |
| tr.b.Range.fromExplicitRange(0, 1001), |
| 10); |
| var h2 = new tr.b.u.Histogram.createLinear( |
| tr.b.u.Units.timeDurationInMs, |
| tr.b.Range.fromExplicitRange(0, 1000), |
| 11); |
| |
| assert.throws(h0.addHistogram.bind(h0, h1), Error); |
| assert.throws(h0.addHistogram.bind(h0, h1), Error); |
| }); |
| |
| test('getInterpolateCountAt', function() { |
| var h = tr.b.u.Histogram.fromDict({ |
| unit: 'unitless', |
| min: 0, |
| max: 100, |
| centralBinWidth: 10, |
| underflowBin: {min: -Number.MAX_VALUE, max: 0, count: 11}, |
| centralBins: [ |
| {min: 0, max: 10, count: 10}, |
| {min: 10, max: 20, count: 9}, |
| {min: 20, max: 30, count: 8}, |
| {min: 30, max: 40, count: 7}, |
| {min: 40, max: 50, count: 6}, |
| {min: 50, max: 60, count: 5}, |
| {min: 60, max: 70, count: 4}, |
| {min: 70, max: 80, count: 3}, |
| {min: 80, max: 90, count: 2}, |
| {min: 90, max: 100, count: 1} |
| ], |
| overflowBin: {min: 100, max: Number.MAX_VALUE, count: 0} |
| }); |
| |
| assert.equal(11, h.maxCount); |
| assert.equal(11, h.getInterpolatedCountAt(-1)); |
| assert.equal(0, h.getInterpolatedCountAt(101)); |
| assert.closeTo(10.8, h.getInterpolatedCountAt(1), 1e-3); |
| assert.closeTo(9.5, h.getInterpolatedCountAt(10), 1e-3); |
| assert.closeTo(0.2, h.getInterpolatedCountAt(99), 1e-3); |
| }); |
| }); |
| </script> |