source: trip-planner-front/node_modules/hdr-histogram-js/dist/Histogram.spec.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 18.6 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const _1 = require(".");
4const JsHistogram_1 = require("./JsHistogram");
5const Histogram_1 = require("./Histogram");
6const Int32Histogram_1 = require("./Int32Histogram");
7const wasm_1 = require("./wasm");
8class HistogramForTests extends JsHistogram_1.default {
9 //constructor() {}
10 clearCounts() { }
11 incrementCountAtIndex(index) { }
12 incrementTotalCount() { }
13 addToTotalCount(value) { }
14 setTotalCount(totalCount) { }
15 resize(newHighestTrackableValue) {
16 this.establishSize(newHighestTrackableValue);
17 }
18 addToCountAtIndex(index, value) { }
19 setCountAtIndex(index, value) { }
20 getTotalCount() {
21 return 0;
22 }
23 getCountAtIndex(index) {
24 return 0;
25 }
26 _getEstimatedFootprintInBytes() {
27 return 42;
28 }
29 copyCorrectedForCoordinatedOmission(expectedIntervalBetweenValueSamples) {
30 return this;
31 }
32}
33describe("Histogram initialization", () => {
34 let histogram;
35 beforeEach(() => {
36 histogram = new HistogramForTests(1, Number.MAX_SAFE_INTEGER, 3);
37 });
38 it("should set sub bucket size", () => {
39 expect(histogram.subBucketCount).toBe(2048);
40 });
41 it("should set resize to false when max value specified", () => {
42 expect(histogram.autoResize).toBe(false);
43 });
44 it("should compute counts array length", () => {
45 expect(histogram.countsArrayLength).toBe(45056);
46 });
47 it("should compute bucket count", () => {
48 expect(histogram.bucketCount).toBe(43);
49 });
50 it("should set min non zero value", () => {
51 expect(histogram.minNonZeroValue).toBe(Number.MAX_SAFE_INTEGER);
52 });
53 it("should set max value", () => {
54 expect(histogram.maxValue).toBe(0);
55 });
56});
57describe("Histogram recording values", () => {
58 it("should compute count index when value in first bucket", () => {
59 // given
60 const histogram = new HistogramForTests(1, Number.MAX_SAFE_INTEGER, 3);
61 // when
62 const index = histogram.countsArrayIndex(2000); // 2000 < 2048
63 expect(index).toBe(2000);
64 });
65 it("should compute count index when value outside first bucket", () => {
66 // given
67 const histogram = new HistogramForTests(1, Number.MAX_SAFE_INTEGER, 3);
68 // when
69 const index = histogram.countsArrayIndex(2050); // 2050 > 2048
70 // then
71 expect(index).toBe(2049);
72 });
73 it("should compute count index taking into account lowest discernible value", () => {
74 // given
75 const histogram = new HistogramForTests(2000, Number.MAX_SAFE_INTEGER, 2);
76 // when
77 const index = histogram.countsArrayIndex(16000);
78 // then
79 expect(index).toBe(15);
80 });
81 it("should compute count index of a big value taking into account lowest discernible value", () => {
82 // given
83 const histogram = new HistogramForTests(2000, Number.MAX_SAFE_INTEGER, 2);
84 // when
85 const bigValue = Number.MAX_SAFE_INTEGER - 1;
86 const index = histogram.countsArrayIndex(bigValue);
87 // then
88 expect(index).toBe(4735);
89 });
90 it("should update min non zero value", () => {
91 // given
92 const histogram = new HistogramForTests(1, Number.MAX_SAFE_INTEGER, 3);
93 // when
94 histogram.recordValue(123);
95 // then
96 expect(histogram.minNonZeroValue).toBe(123);
97 });
98 it("should update max value", () => {
99 // given
100 const histogram = new HistogramForTests(1, Number.MAX_SAFE_INTEGER, 3);
101 // when
102 histogram.recordValue(123);
103 // then
104 expect(histogram.maxValue).toBe(123);
105 });
106 it("should throw an error when value bigger than highest trackable value", () => {
107 // given
108 const histogram = new HistogramForTests(1, 4096, 3);
109 // when then
110 expect(() => histogram.recordValue(9000)).toThrowError();
111 });
112 it("should not throw an error when autoresize enable and value bigger than highest trackable value", () => {
113 // given
114 const histogram = new HistogramForTests(1, 4096, 3);
115 histogram.autoResize = true;
116 // when then
117 expect(() => histogram.recordValue(9000)).not.toThrowError();
118 });
119 it("should increase counts array size when recording value bigger than highest trackable value", () => {
120 // given
121 const histogram = new HistogramForTests(1, 4096, 3);
122 histogram.autoResize = true;
123 // when
124 histogram.recordValue(9000);
125 // then
126 expect(histogram.highestTrackableValue).toBeGreaterThan(9000);
127 });
128});
129describe("Histogram computing statistics", () => {
130 const histogram = new Int32Histogram_1.default(1, Number.MAX_SAFE_INTEGER, 3);
131 it("should compute mean value", () => {
132 // given
133 histogram.reset();
134 // when
135 histogram.recordValue(25);
136 histogram.recordValue(50);
137 histogram.recordValue(75);
138 // then
139 expect(histogram.mean).toBe(50);
140 });
141 it("should compute standard deviation", () => {
142 // given
143 histogram.reset();
144 // when
145 histogram.recordValue(25);
146 histogram.recordValue(50);
147 histogram.recordValue(75);
148 // then
149 expect(histogram.stdDeviation).toBeGreaterThan(20.4124);
150 expect(histogram.stdDeviation).toBeLessThan(20.4125);
151 });
152 it("should compute percentile distribution", () => {
153 // given
154 histogram.reset();
155 // when
156 histogram.recordValue(25);
157 histogram.recordValue(50);
158 histogram.recordValue(75);
159 // then
160 const expectedResult = ` Value Percentile TotalCount 1/(1-Percentile)
161
162 25.000 0.000000000000 1 1.00
163 25.000 0.100000000000 1 1.11
164 25.000 0.200000000000 1 1.25
165 25.000 0.300000000000 1 1.43
166 50.000 0.400000000000 2 1.67
167 50.000 0.500000000000 2 2.00
168 50.000 0.550000000000 2 2.22
169 50.000 0.600000000000 2 2.50
170 50.000 0.650000000000 2 2.86
171 75.000 0.700000000000 3 3.33
172 75.000 1.000000000000 3
173#[Mean = 50.000, StdDeviation = 20.412]
174#[Max = 75.000, Total count = 3]
175#[Buckets = 43, SubBuckets = 2048]
176`;
177 expect(histogram.outputPercentileDistribution()).toBe(expectedResult);
178 });
179 it("should compute percentile distribution in csv format", () => {
180 // given
181 histogram.reset();
182 // when
183 histogram.recordValue(25);
184 histogram.recordValue(50);
185 histogram.recordValue(75);
186 // then
187 const expectedResult = `"Value","Percentile","TotalCount","1/(1-Percentile)"
18825.000,0.000000000000,1,1.00
18925.000,0.100000000000,1,1.11
19025.000,0.200000000000,1,1.25
19125.000,0.300000000000,1,1.43
19250.000,0.400000000000,2,1.67
19350.000,0.500000000000,2,2.00
19450.000,0.550000000000,2,2.22
19550.000,0.600000000000,2,2.50
19650.000,0.650000000000,2,2.86
19775.000,0.700000000000,3,3.33
19875.000,1.000000000000,3,Infinity
199`;
200 expect(histogram.outputPercentileDistribution(undefined, undefined, true)).toBe(expectedResult);
201 });
202 it("should compute percentile distribution in JSON format with rounding according to number of significant digits", () => {
203 // given
204 histogram.reset();
205 // when
206 histogram.recordValue(25042);
207 histogram.recordValue(50042);
208 histogram.recordValue(75042);
209 // then
210 const { summary } = histogram;
211 expect(summary.p50).toEqual(50000);
212 });
213});
214describe("Histogram correcting coordinated omissions", () => {
215 const histogram = new Int32Histogram_1.default(1, Number.MAX_SAFE_INTEGER, 3);
216 it("should generate additional values when recording", () => {
217 // given
218 histogram.reset();
219 // when
220 histogram.recordValueWithExpectedInterval(207, 100);
221 // then
222 expect(histogram.totalCount).toBe(2);
223 expect(histogram.minNonZeroValue).toBe(107);
224 expect(histogram.maxValue).toBe(207);
225 });
226 it("should generate additional values when correcting after recording", () => {
227 // given
228 histogram.reset();
229 histogram.recordValue(207);
230 histogram.recordValue(207);
231 // when
232 const correctedHistogram = histogram.copyCorrectedForCoordinatedOmission(100);
233 // then
234 expect(correctedHistogram.totalCount).toBe(4);
235 expect(correctedHistogram.minNonZeroValue).toBe(107);
236 expect(correctedHistogram.maxValue).toBe(207);
237 });
238 it("should not generate additional values when correcting after recording", () => {
239 // given
240 histogram.reset();
241 histogram.recordValue(207);
242 histogram.recordValue(207);
243 // when
244 const correctedHistogram = histogram.copyCorrectedForCoordinatedOmission(1000);
245 // then
246 expect(correctedHistogram.totalCount).toBe(2);
247 expect(correctedHistogram.minNonZeroValue).toBe(207);
248 expect(correctedHistogram.maxValue).toBe(207);
249 });
250});
251describe("WASM Histogram not initialized", () => {
252 it("should throw a clear error message", () => {
253 expect(() => _1.build({ useWebAssembly: true })).toThrow("WebAssembly is not ready yet");
254 expect(() => wasm_1.WasmHistogram.build()).toThrow("WebAssembly is not ready yet");
255 expect(() => wasm_1.WasmHistogram.decode(null)).toThrow("WebAssembly is not ready yet");
256 });
257});
258describe("WASM Histogram not happy path", () => {
259 beforeEach(wasm_1.initWebAssemblySync);
260 it("should throw a clear error message when used after destroy", () => {
261 const destroyedHistogram = _1.build({ useWebAssembly: true });
262 destroyedHistogram.destroy();
263 expect(() => destroyedHistogram.recordValue(42)).toThrow("Cannot use a destroyed histogram");
264 });
265 it("should not crash when displayed after destroy", () => {
266 const destroyedHistogram = _1.build({ useWebAssembly: true });
267 destroyedHistogram.destroy();
268 expect(destroyedHistogram + "").toEqual("Destroyed WASM histogram");
269 });
270 it("should throw a clear error message when added to a JS regular Histogram", () => {
271 const wasmHistogram = _1.build({ useWebAssembly: true });
272 const jsHistogram = _1.build({ useWebAssembly: false });
273 expect(() => jsHistogram.add(wasmHistogram)).toThrow("Cannot add a WASM histogram to a regular JS histogram");
274 });
275 it("should throw a clear error message when trying to add a JS regular Histogram", () => {
276 const wasmHistogram = _1.build({ useWebAssembly: true });
277 const jsHistogram = _1.build({ useWebAssembly: false });
278 expect(() => wasmHistogram.add(jsHistogram)).toThrow("Cannot add a regular JS histogram to a WASM histogram");
279 });
280 it("should throw a clear error message when substracted to a JS regular Histogram", () => {
281 const wasmHistogram = _1.build({ useWebAssembly: true });
282 const jsHistogram = _1.build({ useWebAssembly: false });
283 expect(() => jsHistogram.subtract(wasmHistogram)).toThrow("Cannot subtract a WASM histogram to a regular JS histogram");
284 });
285 it("should throw a clear error message when trying to add a JS regular Histogram", () => {
286 const wasmHistogram = _1.build({ useWebAssembly: true });
287 const jsHistogram = _1.build({ useWebAssembly: false });
288 expect(() => wasmHistogram.subtract(jsHistogram)).toThrow("Cannot subtract a regular JS histogram to a WASM histogram");
289 });
290});
291describe("WASM estimated memory footprint", () => {
292 let wasmHistogram;
293 beforeAll(wasm_1.initWebAssembly);
294 afterEach(() => wasmHistogram.destroy());
295 it("should be a little bit more than js footprint for packed histograms", () => {
296 wasmHistogram = _1.build({ useWebAssembly: true, bitBucketSize: "packed" });
297 expect(wasmHistogram.estimatedFootprintInBytes).toBeGreaterThan(_1.build({ bitBucketSize: "packed" }).estimatedFootprintInBytes);
298 });
299});
300describe("WASM Histogram correcting coordinated omissions", () => {
301 let histogram;
302 beforeAll(wasm_1.initWebAssembly);
303 beforeEach(() => {
304 histogram = _1.build({ useWebAssembly: true });
305 });
306 afterEach(() => histogram.destroy());
307 it("should generate additional values when recording", () => {
308 // given
309 histogram.reset();
310 // when
311 histogram.recordValueWithExpectedInterval(207, 100);
312 // then
313 expect(histogram.totalCount).toBe(2);
314 expect(histogram.minNonZeroValue).toBe(107);
315 expect(histogram.maxValue).toBe(207);
316 });
317 it("should generate additional values when correcting after recording", () => {
318 // given
319 histogram.reset();
320 histogram.recordValue(207);
321 histogram.recordValue(207);
322 // when
323 const correctedHistogram = histogram.copyCorrectedForCoordinatedOmission(100);
324 // then
325 expect(correctedHistogram.totalCount).toBe(4);
326 expect(correctedHistogram.minNonZeroValue).toBe(107);
327 expect(correctedHistogram.maxValue).toBe(207);
328 });
329 it("should not generate additional values when correcting after recording", () => {
330 // given
331 histogram.reset();
332 histogram.recordValue(207);
333 histogram.recordValue(207);
334 // when
335 const correctedHistogram = histogram.copyCorrectedForCoordinatedOmission(1000);
336 // then
337 expect(correctedHistogram.totalCount).toBe(2);
338 expect(correctedHistogram.minNonZeroValue).toBe(207);
339 expect(correctedHistogram.maxValue).toBe(207);
340 });
341});
342describe("Histogram add & substract", () => {
343 beforeAll(wasm_1.initWebAssembly);
344 it("should add histograms of same size", () => {
345 // given
346 const histogram = new Int32Histogram_1.default(1, Number.MAX_SAFE_INTEGER, 2);
347 const histogram2 = new Int32Histogram_1.default(1, Number.MAX_SAFE_INTEGER, 2);
348 histogram.recordValue(42);
349 histogram2.recordValue(158);
350 // when
351 histogram.add(histogram2);
352 // then
353 expect(histogram.totalCount).toBe(2);
354 expect(histogram.mean).toBe(100);
355 });
356 it("should add histograms of different sizes & precisions", () => {
357 // given
358 const histogram = _1.build({
359 lowestDiscernibleValue: 1,
360 highestTrackableValue: 1024,
361 autoResize: true,
362 numberOfSignificantValueDigits: 2,
363 bitBucketSize: "packed",
364 useWebAssembly: true,
365 });
366 const histogram2 = _1.build({
367 lowestDiscernibleValue: 1,
368 highestTrackableValue: 1024,
369 autoResize: true,
370 numberOfSignificantValueDigits: 3,
371 bitBucketSize: 32,
372 useWebAssembly: true,
373 });
374 //histogram2.autoResize = true;
375 histogram.recordValue(42000);
376 histogram2.recordValue(1000);
377 // when
378 histogram.add(histogram2);
379 // then
380 expect(histogram.totalCount).toBe(2);
381 expect(Math.floor(histogram.mean / 100)).toBe(215);
382 });
383 it("should add histograms of different sizes", () => {
384 // given
385 const histogram = new Int32Histogram_1.default(1, Number.MAX_SAFE_INTEGER, 2);
386 const histogram2 = new Int32Histogram_1.default(1, 1024, 2);
387 histogram2.autoResize = true;
388 histogram.recordValue(42000);
389 histogram2.recordValue(1000);
390 // when
391 histogram.add(histogram2);
392 // then
393 expect(histogram.totalCount).toBe(2);
394 expect(Math.floor(histogram.mean / 100)).toBe(215);
395 });
396 it("should be equal when another histogram of lower precision is added then subtracted", () => {
397 // given
398 const histogram = _1.build({ numberOfSignificantValueDigits: 5 });
399 const histogram2 = _1.build({ numberOfSignificantValueDigits: 3 });
400 histogram.recordValue(100);
401 histogram2.recordValue(42000);
402 // when
403 const before = histogram.summary;
404 histogram.add(histogram2);
405 histogram.subtract(histogram2);
406 // then
407 expect(histogram.summary).toStrictEqual(before);
408 });
409 it("should update percentiles when another histogram of same characteristics is substracted", () => {
410 // given
411 const histogram = _1.build({ numberOfSignificantValueDigits: 3 });
412 const histogram2 = _1.build({ numberOfSignificantValueDigits: 3 });
413 histogram.recordValueWithCount(100, 2);
414 histogram2.recordValueWithCount(100, 1);
415 histogram.recordValueWithCount(200, 2);
416 histogram2.recordValueWithCount(200, 1);
417 histogram.recordValueWithCount(300, 2);
418 histogram2.recordValueWithCount(300, 1);
419 // when
420 histogram.subtract(histogram2);
421 // then
422 expect(histogram.getValueAtPercentile(50)).toBe(200);
423 });
424});
425describe("Histogram clearing support", () => {
426 beforeAll(wasm_1.initWebAssembly);
427 it("should reset data in order to reuse histogram", () => {
428 // given
429 const histogram = _1.build({
430 lowestDiscernibleValue: 1,
431 highestTrackableValue: Number.MAX_SAFE_INTEGER,
432 numberOfSignificantValueDigits: 5,
433 useWebAssembly: true,
434 });
435 histogram.startTimeStampMsec = 42;
436 histogram.endTimeStampMsec = 56;
437 histogram.tag = "blabla";
438 histogram.recordValue(1000);
439 // when
440 histogram.reset();
441 // then
442 expect(histogram.totalCount).toBe(0);
443 expect(histogram.startTimeStampMsec).toBe(0);
444 expect(histogram.endTimeStampMsec).toBe(0);
445 expect(histogram.tag).toBe(Histogram_1.NO_TAG);
446 expect(histogram.maxValue).toBe(0);
447 expect(histogram.minNonZeroValue).toBeGreaterThan(Number.MAX_SAFE_INTEGER);
448 expect(histogram.getValueAtPercentile(99.999)).toBe(0);
449 });
450 it("should behave as new when reseted", () => {
451 // given
452 const histogram = _1.build({
453 lowestDiscernibleValue: 1,
454 highestTrackableValue: 15000,
455 numberOfSignificantValueDigits: 2,
456 });
457 const histogram2 = _1.build({
458 lowestDiscernibleValue: 1,
459 highestTrackableValue: 15000,
460 numberOfSignificantValueDigits: 2,
461 });
462 histogram.recordValue(1);
463 histogram.recordValue(100);
464 histogram.recordValue(2000);
465 histogram.reset();
466 // when
467 histogram.recordValue(1000);
468 histogram2.recordValue(1000);
469 // then
470 expect(histogram.mean).toBe(histogram2.mean);
471 });
472});
473//# sourceMappingURL=Histogram.spec.js.map
Note: See TracBrowser for help on using the repository browser.