OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <!-- | 2 <!-- |
3 Copyright 2016 The Chromium Authors. All rights reserved. | 3 Copyright 2016 The Chromium Authors. All rights reserved. |
4 Use of this source code is governed by a BSD-style license that can be | 4 Use of this source code is governed by a BSD-style license that can be |
5 found in the LICENSE file. | 5 found in the LICENSE file. |
6 --> | 6 --> |
7 | 7 |
8 <link rel="import" href="/tracing/value/diagnostics/generic.html"> | 8 <link rel="import" href="/tracing/value/diagnostics/generic.html"> |
9 <link rel="import" href="/tracing/value/histogram.html"> | 9 <link rel="import" href="/tracing/value/histogram.html"> |
10 | 10 |
11 <script> | 11 <script> |
12 'use strict'; | 12 'use strict'; |
13 | 13 |
14 tr.b.unittest.testSuite(function() { | 14 tr.b.unittest.testSuite(function() { |
15 var TEST_NUMERIC_BUILDER = tr.v.NumericBuilder.createLinear( | 15 var unitlessNumber = tr.v.Unit.byName.unitlessNumber; |
16 tr.v.Unit.byName.timeDurationInMs, tr.b.Range.fromExplicitRange(0, 1000), | 16 var unitlessNumber_sIB = tr.v.Unit.byName.unitlessNumber_smallerIsBetter; |
17 10); | |
18 | 17 |
19 function checkBuilder(builder, expectedMinBoundary, expectedMaxBoundary, | 18 var TEST_BOUNDARIES = tr.v.HistogramBinBoundaries.createLinear(0, 1000, 10); |
| 19 |
| 20 function checkBoundaries(boundaries, expectedMinBoundary, expectedMaxBoundary, |
20 expectedUnit, expectedBinRanges) { | 21 expectedUnit, expectedBinRanges) { |
21 assert.strictEqual(builder.minBinBoundary, expectedMinBoundary); | 22 assert.strictEqual(boundaries.minBinBoundary, expectedMinBoundary); |
22 assert.strictEqual(builder.maxBinBoundary, expectedMaxBoundary); | 23 assert.strictEqual(boundaries.maxBinBoundary, expectedMaxBoundary); |
23 | 24 |
24 // Check that the builder can be used multiple times. | 25 // Check that the boundaries can be used multiple times. |
25 for (var i = 0; i < 3; i++) { | 26 for (var i = 0; i < 3; i++) { |
26 var numeric = builder.build(); | 27 var numeric = new tr.v.Histogram(expectedUnit, boundaries); |
27 assert.instanceOf(numeric, tr.v.Histogram); | 28 assert.instanceOf(numeric, tr.v.Histogram); |
28 assert.strictEqual(numeric.unit, expectedUnit); | 29 assert.strictEqual(numeric.unit, expectedUnit); |
29 assert.strictEqual(numeric.numValues, 0); | 30 assert.strictEqual(numeric.numValues, 0); |
30 | 31 |
31 assert.lengthOf(numeric.allBins, expectedBinRanges.length); | 32 assert.lengthOf(numeric.allBins, expectedBinRanges.length); |
32 for (var j = 0; j < expectedBinRanges.length; j++) { | 33 for (var j = 0; j < expectedBinRanges.length; j++) { |
33 var bin = numeric.allBins[j]; | 34 var bin = numeric.allBins[j]; |
34 assert.instanceOf(bin, tr.v.HistogramBin); | |
35 assert.strictEqual(bin.parentNumeric, numeric); | |
36 assert.strictEqual(bin.count, 0); | 35 assert.strictEqual(bin.count, 0); |
37 assert.isTrue(bin.range.equals(expectedBinRanges[j])); | 36 assert.isTrue(bin.range.equals(expectedBinRanges[j])); |
38 } | 37 } |
39 } | 38 } |
40 } | 39 } |
41 | 40 |
42 test('significance', function() { | 41 test('significance', function() { |
43 assert.strictEqual(TEST_NUMERIC_BUILDER.build().getDifferenceSignificance( | 42 assert.strictEqual( |
44 TEST_NUMERIC_BUILDER.build()), tr.v.Significance.DONT_CARE); | 43 new tr.v.Histogram(unitlessNumber).getDifferenceSignificance( |
| 44 new tr.v.Histogram(unitlessNumber)), tr.v.Significance.DONT_CARE); |
45 | 45 |
46 var builder = tr.v.NumericBuilder.createLinear( | 46 var boundaries = tr.v.HistogramBinBoundaries.createLinear(0, 100, 10); |
47 tr.v.Unit.byName.unitlessNumber_smallerIsBetter, | 47 var numericA = new tr.v.Histogram(unitlessNumber_sIB, boundaries); |
48 tr.b.Range.fromExplicitRange(0, 100), 10); | 48 var numericB = new tr.v.Histogram(unitlessNumber_sIB, boundaries); |
49 var numericA = builder.build(); | |
50 var numericB = builder.build(); | |
51 for (var i = 0; i < 100; ++i) { | 49 for (var i = 0; i < 100; ++i) { |
52 numericA.add(i); | 50 numericA.add(i); |
53 numericB.add(i * 0.85); | 51 numericB.add(i * 0.85); |
54 } | 52 } |
55 | 53 |
56 assert.strictEqual(numericA.getDifferenceSignificance(numericB), | 54 assert.strictEqual(numericA.getDifferenceSignificance(numericB), |
57 tr.v.Significance.INSIGNIFICANT); | 55 tr.v.Significance.INSIGNIFICANT); |
58 assert.strictEqual(numericB.getDifferenceSignificance(numericA), | 56 assert.strictEqual(numericB.getDifferenceSignificance(numericA), |
59 tr.v.Significance.INSIGNIFICANT); | 57 tr.v.Significance.INSIGNIFICANT); |
60 assert.strictEqual(numericA.getDifferenceSignificance(numericB, 0.1), | 58 assert.strictEqual(numericA.getDifferenceSignificance(numericB, 0.1), |
61 tr.v.Significance.SIGNIFICANT); | 59 tr.v.Significance.SIGNIFICANT); |
62 assert.strictEqual(numericB.getDifferenceSignificance(numericA, 0.1), | 60 assert.strictEqual(numericB.getDifferenceSignificance(numericA, 0.1), |
63 tr.v.Significance.SIGNIFICANT); | 61 tr.v.Significance.SIGNIFICANT); |
64 }); | 62 }); |
65 | 63 |
66 test('numericBasic', function() { | 64 test('numericBasic', function() { |
67 var n = TEST_NUMERIC_BUILDER.build(); | 65 var n = new tr.v.Histogram(unitlessNumber, TEST_BOUNDARIES); |
68 assert.equal(n.getBinForValue(250).range.min, 200); | 66 assert.equal(n.getBinForValue(250).range.min, 200); |
69 assert.equal(n.getBinForValue(250).range.max, 300); | 67 assert.equal(n.getBinForValue(250).range.max, 300); |
70 | 68 |
71 var dm0 = new tr.v.d.DiagnosticMap(); | 69 var dm0 = new tr.v.d.DiagnosticMap(); |
72 var dm1 = new tr.v.d.DiagnosticMap(); | 70 var dm1 = new tr.v.d.DiagnosticMap(); |
73 var dm2 = new tr.v.d.DiagnosticMap(); | 71 var dm2 = new tr.v.d.DiagnosticMap(); |
74 var dm3 = new tr.v.d.DiagnosticMap(); | 72 var dm3 = new tr.v.d.DiagnosticMap(); |
75 var dm4 = new tr.v.d.DiagnosticMap(); | 73 var dm4 = new tr.v.d.DiagnosticMap(); |
76 var dm5 = new tr.v.d.DiagnosticMap(); | 74 var dm5 = new tr.v.d.DiagnosticMap(); |
77 dm0.set('foo', new tr.v.d.Generic('a')); | 75 dm0.set('foo', new tr.v.d.Generic('a')); |
(...skipping 18 matching lines...) Expand all Loading... |
96 | 94 |
97 assert.equal(n.getBinForValue(500).count, 1); | 95 assert.equal(n.getBinForValue(500).count, 1); |
98 assert.equal(n.getBinForValue(999).count, 1); | 96 assert.equal(n.getBinForValue(999).count, 1); |
99 | 97 |
100 assert.equal(n.overflowBin.count, 1); | 98 assert.equal(n.overflowBin.count, 1); |
101 assert.equal(n.numValues, 6); | 99 assert.equal(n.numValues, 6); |
102 assert.closeTo(n.average, 416.3, 0.1); | 100 assert.closeTo(n.average, 416.3, 0.1); |
103 }); | 101 }); |
104 | 102 |
105 test('numericNans', function() { | 103 test('numericNans', function() { |
106 var n = TEST_NUMERIC_BUILDER.build(); | 104 var n = new tr.v.Histogram(unitlessNumber, TEST_BOUNDARIES); |
107 | 105 |
108 var dm0 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('b')}); | 106 var dm0 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('b')}); |
109 var dm1 = tr.v.d.DiagnosticMap.fromObject({'foo': new tr.v.d.Generic('c')}); | 107 var dm1 = tr.v.d.DiagnosticMap.fromObject({'foo': new tr.v.d.Generic('c')}); |
110 | 108 |
111 n.add(undefined, dm0); | 109 n.add(undefined, dm0); |
112 n.add(NaN, dm1); | 110 n.add(NaN, dm1); |
113 n.add(undefined); | 111 n.add(undefined); |
114 n.add(NaN); | 112 n.add(NaN); |
115 | 113 |
116 assert.equal(n.numNans, 4); | 114 assert.equal(n.numNans, 4); |
117 assert.deepEqual(n.nanDiagnosticMaps.map(dm => dm.get('foo').value), | 115 assert.deepEqual(n.nanDiagnosticMaps.map(dm => dm.get('foo').value), |
118 ['b', 'c']); | 116 ['b', 'c']); |
119 | 117 |
120 var n2 = n.clone(); | 118 var n2 = n.clone(); |
121 assert.instanceOf(n2.nanDiagnosticMaps[0], tr.v.d.DiagnosticMap); | 119 assert.instanceOf(n2.nanDiagnosticMaps[0], tr.v.d.DiagnosticMap); |
122 assert.instanceOf(n2.nanDiagnosticMaps[0].get('foo'), tr.v.d.Generic); | 120 assert.instanceOf(n2.nanDiagnosticMaps[0].get('foo'), tr.v.d.Generic); |
123 }); | 121 }); |
124 | 122 |
125 test('addNumericsValid', function() { | 123 test('addHistogramsValid', function() { |
126 var n0 = TEST_NUMERIC_BUILDER.build(); | 124 var n0 = new tr.v.Histogram(unitlessNumber, TEST_BOUNDARIES); |
127 var n1 = TEST_NUMERIC_BUILDER.build(); | 125 var n1 = new tr.v.Histogram(unitlessNumber, TEST_BOUNDARIES); |
128 | 126 |
129 var dm0 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('a0')}); | 127 var dm0 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('a0')}); |
130 var dm1 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('b0')}); | 128 var dm1 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('b0')}); |
131 var dm2 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('c0')}); | 129 var dm2 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('c0')}); |
132 var dm3 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('c0')}); | 130 var dm3 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('c0')}); |
133 var dm4 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('d0')}); | 131 var dm4 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('d0')}); |
134 var dm5 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('e0')}); | 132 var dm5 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('e0')}); |
135 var dm6 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('a1')}); | 133 var dm6 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('a1')}); |
136 var dm7 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('b1')}); | 134 var dm7 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('b1')}); |
137 var dm8 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('c1')}); | 135 var dm8 = tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('c1')}); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 | 187 |
190 assert.equal(2, n0.maxCount); | 188 assert.equal(2, n0.maxCount); |
191 assert.equal(2, n1.maxCount); | 189 assert.equal(2, n1.maxCount); |
192 | 190 |
193 var n02 = n0.clone(); | 191 var n02 = n0.clone(); |
194 assert.instanceOf(n02.underflowBin.diagnosticMaps[0], tr.v.d.DiagnosticMap); | 192 assert.instanceOf(n02.underflowBin.diagnosticMaps[0], tr.v.d.DiagnosticMap); |
195 assert.instanceOf(n02.underflowBin.diagnosticMaps[0].get('foo'), | 193 assert.instanceOf(n02.underflowBin.diagnosticMaps[0].get('foo'), |
196 tr.v.d.Generic); | 194 tr.v.d.Generic); |
197 }); | 195 }); |
198 | 196 |
199 test('addNumericsInvalid', function() { | 197 test('addHistogramsInvalid', function() { |
200 var n0 = tr.v.NumericBuilder.createLinear(tr.v.Unit.byName.timeDurationInMs, | 198 var n0 = new tr.v.Histogram(tr.v.Unit.byName.timeDurationInMs, |
201 tr.b.Range.fromExplicitRange(0, 1000), 10).build(); | 199 tr.v.HistogramBinBoundaries.createLinear(0, 1000, 10)); |
202 var n1 = tr.v.NumericBuilder.createLinear(tr.v.Unit.byName.timeDurationInMs, | 200 var n1 = new tr.v.Histogram(tr.v.Unit.byName.timeDurationInMs, |
203 tr.b.Range.fromExplicitRange(0, 1001), 10).build(); | 201 tr.v.HistogramBinBoundaries.createLinear(0, 1001, 10)); |
204 var n2 = tr.v.NumericBuilder.createLinear(tr.v.Unit.byName.timeDurationInMs, | 202 var n2 = new tr.v.Histogram(tr.v.Unit.byName.timeDurationInMs, |
205 tr.b.Range.fromExplicitRange(0, 1000), 11).build(); | 203 tr.v.HistogramBinBoundaries.createLinear(0, 1000, 11)); |
206 | 204 |
207 assert.throws(n0.addNumeric.bind(n0, n1), Error); | 205 assert.throws(n0.addNumeric.bind(n0, n1), Error); |
208 assert.throws(n0.addNumeric.bind(n0, n1), Error); | 206 assert.throws(n0.addNumeric.bind(n0, n2), Error); |
209 }); | 207 }); |
210 | 208 |
211 test('addNumericWithNonDiagnosticMapThrows', function() { | 209 test('addNumericWithNonDiagnosticMapThrows', function() { |
212 var n = TEST_NUMERIC_BUILDER.build(); | 210 var n = new tr.v.Histogram(unitlessNumber, TEST_BOUNDARIES); |
213 assert.throws(n.add.bind(42, 'foo'), Error); | 211 assert.throws(n.add.bind(42, 'foo'), Error); |
214 }); | 212 }); |
215 | 213 |
216 test('numericPercentile', function() { | 214 test('numericPercentile', function() { |
217 function check(array, min, max, bins, precision) { | 215 function check(array, min, max, bins, precision) { |
218 var n = tr.v.NumericBuilder.createLinear( | 216 var boundaries = tr.v.HistogramBinBoundaries.createLinear(min, max, bins); |
219 tr.v.Unit.byName.timeDurationInMs, | 217 var n = new tr.v.Histogram(tr.v.Unit.byName.timeDurationInMs, boundaries); |
220 tr.b.Range.fromExplicitRange(min, max), | |
221 bins).build(); | |
222 array.forEach((x) => n.add( | 218 array.forEach((x) => n.add( |
223 x, tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('x')}))); | 219 x, tr.v.d.DiagnosticMap.fromObject({foo: new tr.v.d.Generic('x')}))); |
224 [0.25, 0.5, 0.75, 0.8, 0.95, 0.99].forEach(function(percent) { | 220 [0.25, 0.5, 0.75, 0.8, 0.95, 0.99].forEach(function(percent) { |
225 var expected = tr.b.Statistics.percentile(array, percent); | 221 var expected = tr.b.Statistics.percentile(array, percent); |
226 var actual = n.getApproximatePercentile(percent); | 222 var actual = n.getApproximatePercentile(percent); |
227 assert.closeTo(expected, actual, precision); | 223 assert.closeTo(expected, actual, precision); |
228 }); | 224 }); |
229 } | 225 } |
230 check([1, 2, 5, 7], 0.5, 10.5, 10, 1e-3); | 226 check([1, 2, 5, 7], 0.5, 10.5, 10, 1e-3); |
231 check([3, 3, 4, 4], 0.5, 10.5, 10, 1e-3); | 227 check([3, 3, 4, 4], 0.5, 10.5, 10, 1e-3); |
232 check([1, 10], 0.5, 10.5, 10, 1e-3); | 228 check([1, 10], 0.5, 10.5, 10, 1e-3); |
233 check([1, 2, 3, 4, 5], 0.5, 10.5, 10, 1e-3); | 229 check([1, 2, 3, 4, 5], 0.5, 10.5, 10, 1e-3); |
234 check([3, 3, 3, 3, 3], 0.5, 10.5, 10, 1e-3); | 230 check([3, 3, 3, 3, 3], 0.5, 10.5, 10, 1e-3); |
235 check([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.5, 10.5, 10, 1e-3); | 231 check([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.5, 10.5, 10, 1e-3); |
236 check([1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10], 0.5, 10.5, 10, 1e-3); | 232 check([1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10], 0.5, 10.5, 10, 1e-3); |
237 check([0, 11], 0.5, 10.5, 10, 1); | 233 check([0, 11], 0.5, 10.5, 10, 1); |
238 check([0, 6, 11], 0.5, 10.5, 10, 1); | 234 check([0, 6, 11], 0.5, 10.5, 10, 1); |
239 var array = []; | 235 var array = []; |
240 for (var i = 0; i < 1000; i++) | 236 for (var i = 0; i < 1000; i++) |
241 array.push((i * i) % 10 + 1); | 237 array.push((i * i) % 10 + 1); |
242 check(array, 0.5, 10.5, 10, 1e-3); | 238 check(array, 0.5, 10.5, 10, 1e-3); |
243 // If the real percentile is outside the bin range then the approximation | 239 // If the real percentile is outside the bin range then the approximation |
244 // error can be high. | 240 // error can be high. |
245 check([-10000], 0, 10, 10, 10000); | 241 check([-10000], 0, 10, 10, 10000); |
246 check([10000], 0, 10, 10, 10000 - 10); | 242 check([10000], 0, 10, 10, 10000 - 10); |
247 // The result is no more than the bin width away from the real percentile. | 243 // The result is no more than the bin width away from the real percentile. |
248 check([1, 1], 0, 10, 1, 10); | 244 check([1, 1], 0, 10, 1, 10); |
249 }); | 245 }); |
250 | 246 |
251 test('numericBuilder_addBinBoundary', function() { | 247 test('histogramBinBoundaries_addBinBoundary', function() { |
252 var b = new tr.v.NumericBuilder(tr.v.Unit.byName.timeDurationInMs, -100); | 248 var b = new tr.v.HistogramBinBoundaries(-100); |
253 b.addBinBoundary(50); | 249 b.addBinBoundary(50); |
254 | 250 |
255 checkBuilder(b, -100, 50, tr.v.Unit.byName.timeDurationInMs, [ | 251 checkBoundaries(b, -100, 50, tr.v.Unit.byName.timeDurationInMs, [ |
256 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, -100), | 252 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, -100), |
257 tr.b.Range.fromExplicitRange(-100, 50), | 253 tr.b.Range.fromExplicitRange(-100, 50), |
258 tr.b.Range.fromExplicitRange(50, Number.MAX_VALUE) | 254 tr.b.Range.fromExplicitRange(50, Number.MAX_VALUE) |
259 ]); | 255 ]); |
260 | 256 |
261 b.addBinBoundary(60); | 257 b.addBinBoundary(60); |
262 b.addBinBoundary(75); | 258 b.addBinBoundary(75); |
263 | 259 |
264 checkBuilder(b, -100, 75, tr.v.Unit.byName.timeDurationInMs, [ | 260 checkBoundaries(b, -100, 75, tr.v.Unit.byName.timeDurationInMs, [ |
265 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, -100), | 261 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, -100), |
266 tr.b.Range.fromExplicitRange(-100, 50), | 262 tr.b.Range.fromExplicitRange(-100, 50), |
267 tr.b.Range.fromExplicitRange(50, 60), | 263 tr.b.Range.fromExplicitRange(50, 60), |
268 tr.b.Range.fromExplicitRange(60, 75), | 264 tr.b.Range.fromExplicitRange(60, 75), |
269 tr.b.Range.fromExplicitRange(75, Number.MAX_VALUE) | 265 tr.b.Range.fromExplicitRange(75, Number.MAX_VALUE) |
270 ]); | 266 ]); |
271 }); | 267 }); |
272 | 268 |
273 test('numericBuilder_addLinearBins', function() { | 269 test('histogramBinBoundaries_addLinearBins', function() { |
274 var b = new tr.v.NumericBuilder(tr.v.Unit.byName.powerInWatts, 1000); | 270 var b = new tr.v.HistogramBinBoundaries(1000); |
275 b.addLinearBins(1200, 5); | 271 b.addLinearBins(1200, 5); |
276 | 272 |
277 checkBuilder(b, 1000, 1200, tr.v.Unit.byName.powerInWatts, [ | 273 checkBoundaries(b, 1000, 1200, tr.v.Unit.byName.powerInWatts, [ |
278 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, 1000), | 274 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, 1000), |
279 tr.b.Range.fromExplicitRange(1000, 1040), | 275 tr.b.Range.fromExplicitRange(1000, 1040), |
280 tr.b.Range.fromExplicitRange(1040, 1080), | 276 tr.b.Range.fromExplicitRange(1040, 1080), |
281 tr.b.Range.fromExplicitRange(1080, 1120), | 277 tr.b.Range.fromExplicitRange(1080, 1120), |
282 tr.b.Range.fromExplicitRange(1120, 1160), | 278 tr.b.Range.fromExplicitRange(1120, 1160), |
283 tr.b.Range.fromExplicitRange(1160, 1200), | 279 tr.b.Range.fromExplicitRange(1160, 1200), |
284 tr.b.Range.fromExplicitRange(1200, Number.MAX_VALUE) | 280 tr.b.Range.fromExplicitRange(1200, Number.MAX_VALUE) |
285 ]); | 281 ]); |
286 }); | 282 }); |
287 | 283 |
288 test('numericBuilder_addExponentialBins', function() { | 284 test('histogramBinBoundaries_addExponentialBins', function() { |
289 var b = new tr.v.NumericBuilder(tr.v.Unit.byName.energyInJoules, 0.5); | 285 var b = new tr.v.HistogramBinBoundaries(0.5); |
290 b.addExponentialBins(8, 4); | 286 b.addExponentialBins(8, 4); |
291 | 287 |
292 checkBuilder(b, 0.5, 8, tr.v.Unit.byName.energyInJoules, [ | 288 checkBoundaries(b, 0.5, 8, tr.v.Unit.byName.energyInJoules, [ |
293 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, 0.5), | 289 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, 0.5), |
294 tr.b.Range.fromExplicitRange(0.5, 1), | 290 tr.b.Range.fromExplicitRange(0.5, 1), |
295 tr.b.Range.fromExplicitRange(1, 2), | 291 tr.b.Range.fromExplicitRange(1, 2), |
296 tr.b.Range.fromExplicitRange(2, 4), | 292 tr.b.Range.fromExplicitRange(2, 4), |
297 tr.b.Range.fromExplicitRange(4, 8), | 293 tr.b.Range.fromExplicitRange(4, 8), |
298 tr.b.Range.fromExplicitRange(8, Number.MAX_VALUE) | 294 tr.b.Range.fromExplicitRange(8, Number.MAX_VALUE) |
299 ]); | 295 ]); |
300 }); | 296 }); |
301 | 297 |
302 test('numericBuilder_combined', function() { | 298 test('histogramBinBoundaries_combined', function() { |
303 var b = new tr.v.NumericBuilder(tr.v.Unit.byName.unitlessNumber, -273.15); | 299 var b = new tr.v.HistogramBinBoundaries(-273.15); |
304 b.addBinBoundary(-50); | 300 b.addBinBoundary(-50); |
305 b.addLinearBins(4, 3); | 301 b.addLinearBins(4, 3); |
306 b.addExponentialBins(16, 2); | 302 b.addExponentialBins(16, 2); |
307 b.addLinearBins(17, 4); | 303 b.addLinearBins(17, 4); |
308 b.addBinBoundary(100); | 304 b.addBinBoundary(100); |
309 | 305 |
310 checkBuilder(b, -273.15, 100, tr.v.Unit.byName.unitlessNumber, [ | 306 checkBoundaries(b, -273.15, 100, tr.v.Unit.byName.unitlessNumber, [ |
311 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, -273.15), | 307 tr.b.Range.fromExplicitRange(-Number.MAX_VALUE, -273.15), |
312 tr.b.Range.fromExplicitRange(-273.15, -50), | 308 tr.b.Range.fromExplicitRange(-273.15, -50), |
313 tr.b.Range.fromExplicitRange(-50, -32), | 309 tr.b.Range.fromExplicitRange(-50, -32), |
314 tr.b.Range.fromExplicitRange(-32, -14), | 310 tr.b.Range.fromExplicitRange(-32, -14), |
315 tr.b.Range.fromExplicitRange(-14, 4), | 311 tr.b.Range.fromExplicitRange(-14, 4), |
316 tr.b.Range.fromExplicitRange(4, 8), | 312 tr.b.Range.fromExplicitRange(4, 8), |
317 tr.b.Range.fromExplicitRange(8, 16), | 313 tr.b.Range.fromExplicitRange(8, 16), |
318 tr.b.Range.fromExplicitRange(16, 16.25), | 314 tr.b.Range.fromExplicitRange(16, 16.25), |
319 tr.b.Range.fromExplicitRange(16.25, 16.5), | 315 tr.b.Range.fromExplicitRange(16.25, 16.5), |
320 tr.b.Range.fromExplicitRange(16.5, 16.75), | 316 tr.b.Range.fromExplicitRange(16.5, 16.75), |
321 tr.b.Range.fromExplicitRange(16.75, 17), | 317 tr.b.Range.fromExplicitRange(16.75, 17), |
322 tr.b.Range.fromExplicitRange(17, 100), | 318 tr.b.Range.fromExplicitRange(17, 100), |
323 tr.b.Range.fromExplicitRange(100, Number.MAX_VALUE) | 319 tr.b.Range.fromExplicitRange(100, Number.MAX_VALUE) |
324 ]); | 320 ]); |
325 }); | 321 }); |
326 | 322 |
327 test('numericBuilder_throws', function() { | 323 test('histogramBinBoundaries_throws', function() { |
328 var b0 = new tr.v.NumericBuilder(tr.v.Unit.byName.timeStampInMs, -7); | 324 var b0 = new tr.v.HistogramBinBoundaries(-7); |
329 assert.throws(function() { b0.addBinBoundary(-10 /* must be > -7 */); }); | 325 assert.throws(function() { b0.addBinBoundary(-10 /* must be > -7 */); }); |
330 assert.throws(function() { b0.addBinBoundary(-7 /* must be > -7 */); }); | 326 assert.throws(function() { b0.addBinBoundary(-7 /* must be > -7 */); }); |
331 assert.throws(function() { b0.addLinearBins(-10 /* must be > -7 */, 10); }); | 327 assert.throws(function() { b0.addLinearBins(-10 /* must be > -7 */, 10); }); |
332 assert.throws(function() { b0.addLinearBins(-7 /* must be > -7 */, 100); }); | 328 assert.throws(function() { b0.addLinearBins(-7 /* must be > -7 */, 100); }); |
333 assert.throws(function() { b0.addLinearBins(10, 0 /* must be > 0 */); }); | 329 assert.throws(function() { b0.addLinearBins(10, 0 /* must be > 0 */); }); |
334 assert.throws(function() { | 330 assert.throws(function() { |
335 // Current max bin boundary (-7) must be positive. | 331 // Current max bin boundary (-7) must be positive. |
336 b0.addExponentialBins(16, 4); | 332 b0.addExponentialBins(16, 4); |
337 }); | 333 }); |
338 | 334 |
339 var b1 = new tr.v.NumericBuilder(tr.v.Unit.byName.sizeInBytes, 8); | 335 var b1 = new tr.v.HistogramBinBoundaries(8); |
340 assert.throws( | 336 assert.throws(() => b1.addExponentialBins(20, 0 /* must be > 0 */)); |
341 function() { b1.addExponentialBins(20, 0 /* must be > 0 */); }); | 337 assert.throws(() => b1.addExponentialBins(5 /* must be > 8 */, 3)); |
342 assert.throws( | 338 assert.throws(() => b1.addExponentialBins(8 /* must be > 8 */, 3)); |
343 function() { b1.addExponentialBins(5 /* must be > 8 */, 3); }); | |
344 assert.throws( | |
345 function() { b1.addExponentialBins(8 /* must be > 8 */, 3); }); | |
346 }); | 339 }); |
347 | 340 |
348 test('getSummarizedScalarNumericsWithNames', function() { | 341 test('getSummarizedScalarNumericsWithNames', function() { |
349 var n = tr.v.NumericBuilder.createLinear( | 342 var boundaries = tr.v.HistogramBinBoundaries.createLinear(0, 100, 100); |
350 tr.v.Unit.byName.timeDurationInMs, tr.b.Range.fromExplicitRange(0, 100), | 343 var n = new tr.v.Histogram(unitlessNumber, boundaries); |
351 100).build(); | |
352 | 344 |
353 n.add(50); | 345 n.add(50); |
354 n.add(60); | 346 n.add(60); |
355 n.add(70); | 347 n.add(70); |
356 | 348 |
357 n.customizeSummaryOptions({ | 349 n.customizeSummaryOptions({ |
358 count: true, | 350 count: true, |
359 min: true, | 351 min: true, |
360 max: true, | 352 max: true, |
361 sum: true, | 353 sum: true, |
(...skipping 12 matching lines...) Expand all Loading... |
374 assert.strictEqual(values.min.value, 50); | 366 assert.strictEqual(values.min.value, 50); |
375 assert.strictEqual(values.max.value, 70); | 367 assert.strictEqual(values.max.value, 70); |
376 assert.strictEqual(values.sum.value, 180); | 368 assert.strictEqual(values.sum.value, 180); |
377 assert.strictEqual(values.avg.value, 60); | 369 assert.strictEqual(values.avg.value, 60); |
378 assert.strictEqual(values.std.value, 10); | 370 assert.strictEqual(values.std.value, 10); |
379 assert.closeTo(values.pct_050.value, 60, 1); | 371 assert.closeTo(values.pct_050.value, 60, 1); |
380 assert.closeTo(values.pct_100.value, 70, 1); | 372 assert.closeTo(values.pct_100.value, 70, 1); |
381 }); | 373 }); |
382 | 374 |
383 test('getSummarizedScalarNumericsWithNamesNoSummaryOptions', function() { | 375 test('getSummarizedScalarNumericsWithNamesNoSummaryOptions', function() { |
384 var n = tr.v.NumericBuilder.createLinear( | 376 var boundaries = tr.v.HistogramBinBoundaries.createLinear(0, 100, 100); |
385 tr.v.Unit.byName.timeDurationInMs, tr.b.Range.fromExplicitRange(0, 100), | 377 var n = new tr.v.Histogram(unitlessNumber, boundaries); |
386 100).build(); | |
387 | 378 |
388 n.add(50); | 379 n.add(50); |
389 n.add(60); | 380 n.add(60); |
390 n.add(70); | 381 n.add(70); |
391 | 382 |
392 n.customizeSummaryOptions({ | 383 n.customizeSummaryOptions({ |
393 count: false, | 384 count: false, |
394 min: false, | 385 min: false, |
395 max: false, | 386 max: false, |
396 sum: false, | 387 sum: false, |
397 avg: false, | 388 avg: false, |
398 std: false, | 389 std: false, |
399 percentile: [] | 390 percentile: [] |
400 }); | 391 }); |
401 | 392 |
402 var results = n.getSummarizedScalarNumericsWithNames('abc'); | 393 var results = n.getSummarizedScalarNumericsWithNames('abc'); |
403 assert.strictEqual(results.length, 0); | 394 assert.strictEqual(results.length, 0); |
404 }); | 395 }); |
405 | 396 |
406 test('getSummarizedScalarNumericsWithNamesEmptyNumericValue', function() { | 397 test('getSummarizedScalarNumericsWithNamesEmptyNumericValue', function() { |
407 var n = tr.v.NumericBuilder.createLinear( | 398 var boundaries = tr.v.HistogramBinBoundaries.createLinear(0, 100, 100); |
408 tr.v.Unit.byName.timeDurationInMs, tr.b.Range.fromExplicitRange(0, 100), | 399 var n = new tr.v.Histogram(unitlessNumber, boundaries); |
409 100).build(); | |
410 | 400 |
411 n.customizeSummaryOptions({ | 401 n.customizeSummaryOptions({ |
412 count: true, | 402 count: true, |
413 min: true, | 403 min: true, |
414 max: true, | 404 max: true, |
415 sum: true, | 405 sum: true, |
416 avg: true, | 406 avg: true, |
417 std: true, | 407 std: true, |
418 percentile: [0, 0.01, 0.1, 0.5, 0.995, 1] | 408 percentile: [0, 0.01, 0.1, 0.5, 0.995, 1] |
419 }); | 409 }); |
(...skipping 12 matching lines...) Expand all Loading... |
432 assert.strictEqual(values.std, undefined); | 422 assert.strictEqual(values.std, undefined); |
433 assert.strictEqual(values.pct_000.value, 0); | 423 assert.strictEqual(values.pct_000.value, 0); |
434 assert.strictEqual(values.pct_001.value, 0); | 424 assert.strictEqual(values.pct_001.value, 0); |
435 assert.strictEqual(values.pct_010.value, 0); | 425 assert.strictEqual(values.pct_010.value, 0); |
436 assert.strictEqual(values.pct_050.value, 0); | 426 assert.strictEqual(values.pct_050.value, 0); |
437 assert.strictEqual(values.pct_099_5.value, 0); | 427 assert.strictEqual(values.pct_099_5.value, 0); |
438 assert.strictEqual(values.pct_100.value, 0); | 428 assert.strictEqual(values.pct_100.value, 0); |
439 }); | 429 }); |
440 | 430 |
441 test('sampleValues', function() { | 431 test('sampleValues', function() { |
442 var n0 = TEST_NUMERIC_BUILDER.build(); | 432 var n0 = new tr.v.Histogram(unitlessNumber, TEST_BOUNDARIES); |
443 var n1 = TEST_NUMERIC_BUILDER.build(); | 433 var n1 = new tr.v.Histogram(unitlessNumber, TEST_BOUNDARIES); |
444 // maxNumSampleValues defaults to numBins * 10, which, including the | 434 // maxNumSampleValues defaults to numBins * 10, which, including the |
445 // underflowBin and overflowBin plus this builder's 10 central bins, | 435 // underflowBin and overflowBin plus this builder's 10 central bins, |
446 // is 12 * 10. | 436 // is 12 * 10. |
447 assert.strictEqual(n0.maxNumSampleValues, 120); | 437 assert.strictEqual(n0.maxNumSampleValues, 120); |
448 assert.strictEqual(n1.maxNumSampleValues, 120); | 438 assert.strictEqual(n1.maxNumSampleValues, 120); |
449 var values0 = []; | 439 var values0 = []; |
450 var values1 = []; | 440 var values1 = []; |
451 for (var i = 0; i < 10; ++i) { | 441 for (var i = 0; i < 10; ++i) { |
452 values0.push(i); | 442 values0.push(i); |
453 n0.add(i); | 443 n0.add(i); |
454 } | 444 } |
455 for (var i = 10; i < 20; ++i) { | 445 for (var i = 10; i < 20; ++i) { |
456 values1.push(i); | 446 values1.push(i); |
457 n1.add(i); | 447 n1.add(i); |
458 } | 448 } |
459 assert.deepEqual(n0.sampleValues, values0); | 449 assert.deepEqual(n0.sampleValues, values0); |
460 assert.deepEqual(n1.sampleValues, values1); | 450 assert.deepEqual(n1.sampleValues, values1); |
461 n0.addNumeric(n1); | 451 n0.addNumeric(n1); |
462 assert.deepEqual(n0.sampleValues, values0.concat(values1)); | 452 assert.deepEqual(n0.sampleValues, values0.concat(values1)); |
463 var n2 = n0.clone(); | 453 var n2 = n0.clone(); |
464 assert.deepEqual(n2.sampleValues, values0.concat(values1)); | 454 assert.deepEqual(n2.sampleValues, values0.concat(values1)); |
465 | 455 |
466 for (var i = 0; i < 200; ++i) | 456 for (var i = 0; i < 200; ++i) |
467 n0.add(i); | 457 n0.add(i); |
468 assert.strictEqual(n0.sampleValues.length, n0.maxNumSampleValues); | 458 assert.strictEqual(n0.sampleValues.length, n0.maxNumSampleValues); |
469 | 459 |
470 var n3 = TEST_NUMERIC_BUILDER.build(); | 460 var n3 = new tr.v.Histogram(unitlessNumber, TEST_BOUNDARIES); |
471 n3.maxNumSampleValues = 10; | 461 n3.maxNumSampleValues = 10; |
472 for (var i = 0; i < 100; ++i) | 462 for (var i = 0; i < 100; ++i) |
473 n3.add(i); | 463 n3.add(i); |
474 assert.strictEqual(n3.sampleValues.length, 10); | 464 assert.strictEqual(n3.sampleValues.length, 10); |
475 }); | 465 }); |
476 | 466 |
477 test('mergeScalarNumerics', function() { | 467 test('mergeScalarNumerics', function() { |
478 var scalarA = new tr.v.ScalarNumeric(tr.v.Unit.byName.timeDurationInMs, 10); | 468 var scalarA = new tr.v.ScalarNumeric(tr.v.Unit.byName.timeDurationInMs, 10); |
479 var scalarB = new tr.v.ScalarNumeric(tr.v.Unit.byName.timeDurationInMs, 20); | 469 var scalarB = new tr.v.ScalarNumeric(tr.v.Unit.byName.timeDurationInMs, 20); |
480 | 470 |
(...skipping 17 matching lines...) Expand all Loading... |
498 scalarA.value = 'i am not a number'; | 488 scalarA.value = 'i am not a number'; |
499 scalarB.value = 'i am also not a number'; | 489 scalarB.value = 'i am also not a number'; |
500 numeric = scalarA.merge(scalarB); | 490 numeric = scalarA.merge(scalarB); |
501 assert.instanceOf(numeric, tr.v.Histogram); | 491 assert.instanceOf(numeric, tr.v.Histogram); |
502 assert.strictEqual(numeric.maxNumSampleValues, 1000); | 492 assert.strictEqual(numeric.maxNumSampleValues, 1000); |
503 assert.strictEqual(numeric.numValues, 0); | 493 assert.strictEqual(numeric.numValues, 0); |
504 assert.strictEqual(numeric.numNans, 2); | 494 assert.strictEqual(numeric.numNans, 2); |
505 assert.lengthOf(numeric.sampleValues, 2); | 495 assert.lengthOf(numeric.sampleValues, 2); |
506 assert.lengthOf(numeric.allBins, 4); | 496 assert.lengthOf(numeric.allBins, 4); |
507 }); | 497 }); |
508 | |
509 test('mergeNumericWithScalar', function() { | |
510 var scalar = new tr.v.ScalarNumeric( | |
511 tr.v.Unit.byName.timeDurationInMs, 1010); | |
512 var numeric = TEST_NUMERIC_BUILDER.build(); | |
513 for (var i = 0; i < 100; ++i) | |
514 numeric.add(Math.random() * 1000); | |
515 numeric.add('i am not a number'); | |
516 | |
517 var mergedA = scalar.merge(numeric); | |
518 var mergedB = numeric.merge(scalar); | |
519 assert.instanceOf(mergedA, tr.v.Histogram); | |
520 assert.instanceOf(mergedB, tr.v.Histogram); | |
521 | |
522 assert.strictEqual(mergedA.maxNumSampleValues, 1000); | |
523 // numValues does not include non-number samples. | |
524 assert.strictEqual(mergedA.numValues, 101); | |
525 assert.strictEqual(mergedA.numNans, 1); | |
526 | |
527 assert.strictEqual(mergedB.maxNumSampleValues, 1000); | |
528 // numValues does not include non-number samples. | |
529 assert.strictEqual(mergedB.numValues, 101); | |
530 assert.strictEqual(mergedB.numNans, 1); | |
531 | |
532 // Normalize away some insignificant differences so that deepEqual can test | |
533 // the significant fields. | |
534 var mergedAdict = mergedA.asDict(); | |
535 var mergedBdict = mergedB.asDict(); | |
536 mergedAdict.sampleValues.sort(); | |
537 mergedBdict.sampleValues.sort(); | |
538 for (var stat in mergedAdict.running) { | |
539 mergedAdict.running[stat] = Math.round(mergedAdict.running[stat]); | |
540 mergedBdict.running[stat] = Math.round(mergedBdict.running[stat]); | |
541 } | |
542 assert.deepEqual(mergedAdict, mergedBdict); | |
543 }); | |
544 }); | 498 }); |
545 | 499 |
546 </script> | 500 </script> |
OLD | NEW |