1 | import Histogram from "./PackedHistogram";
|
---|
2 |
|
---|
3 | describe("Packed histogram", () => {
|
---|
4 | it("should compute median value in first bucket", () => {
|
---|
5 | // given
|
---|
6 | const histogram = new Histogram(1, Number.MAX_SAFE_INTEGER, 3);
|
---|
7 | histogram.recordValue(123456);
|
---|
8 | histogram.recordValue(127);
|
---|
9 | histogram.recordValue(42);
|
---|
10 | // when
|
---|
11 | const medianValue = histogram.getValueAtPercentile(50);
|
---|
12 | // then
|
---|
13 | expect(medianValue).toBe(127);
|
---|
14 | });
|
---|
15 |
|
---|
16 | it("should compute same values when new or reseted", () => {
|
---|
17 | // given
|
---|
18 | const histogram = new Histogram(1, 2, 3);
|
---|
19 | const histogram2 = new Histogram(1, 2, 3);
|
---|
20 | histogram.autoResize = true;
|
---|
21 | histogram2.autoResize = true;
|
---|
22 |
|
---|
23 | [1, 49026].forEach((v) => histogram.recordValue(v));
|
---|
24 |
|
---|
25 | // when
|
---|
26 | histogram.reset();
|
---|
27 | [7, 67, 42357, 805017].forEach((v) => {
|
---|
28 | histogram.recordValue(v);
|
---|
29 | histogram2.recordValue(v);
|
---|
30 | });
|
---|
31 | // then
|
---|
32 | expect(histogram.getValueAtPercentile(5)).toBe(
|
---|
33 | histogram2.getValueAtPercentile(5)
|
---|
34 | );
|
---|
35 | });
|
---|
36 |
|
---|
37 | it("should compute value outside first bucket with an error less than 1000", () => {
|
---|
38 | // given
|
---|
39 | const histogram = new Histogram(1, 123456, 2);
|
---|
40 | histogram.recordValue(5234);
|
---|
41 | histogram.recordValue(127);
|
---|
42 | histogram.recordValue(42);
|
---|
43 | // when
|
---|
44 | const percentileValue = histogram.getValueAtPercentile(90);
|
---|
45 | // then
|
---|
46 | expect(Math.abs(percentileValue - 5234)).toBeLessThan(100);
|
---|
47 | });
|
---|
48 |
|
---|
49 | it("should resize underlying packed array when recording an out of bound value", () => {
|
---|
50 | // given
|
---|
51 | const histogram = new Histogram(1, 2, 3);
|
---|
52 | histogram.autoResize = true;
|
---|
53 | // when
|
---|
54 | histogram.recordValue(123456);
|
---|
55 | // then
|
---|
56 | expect(histogram.totalCount).toBe(1);
|
---|
57 | });
|
---|
58 | });
|
---|
59 |
|
---|
60 | describe("Histogram clearing support", () => {
|
---|
61 | it("should reset data in order to reuse histogram", () => {
|
---|
62 | // given
|
---|
63 | const histogram = new Histogram(1, Number.MAX_SAFE_INTEGER, 2);
|
---|
64 | const histogram2 = new Histogram(1, Number.MAX_SAFE_INTEGER, 2);
|
---|
65 | histogram.startTimeStampMsec = 42;
|
---|
66 | histogram.endTimeStampMsec = 56;
|
---|
67 | histogram.tag = "blabla";
|
---|
68 | histogram.recordValue(10);
|
---|
69 | histogram.recordValue(1000);
|
---|
70 | histogram.recordValue(10000000);
|
---|
71 | // when
|
---|
72 | histogram.reset();
|
---|
73 | // then
|
---|
74 | expect(histogram.totalCount).toBe(0);
|
---|
75 | expect(histogram.startTimeStampMsec).toBe(0);
|
---|
76 | expect(histogram.endTimeStampMsec).toBe(0);
|
---|
77 | //expect(histogram.tag).toBe(NO_TAG);
|
---|
78 | expect(histogram.maxValue).toBe(0);
|
---|
79 | expect(histogram.minNonZeroValue).toBe(Number.MAX_SAFE_INTEGER);
|
---|
80 | expect(histogram.getValueAtPercentile(99.999)).toBe(0);
|
---|
81 | });
|
---|
82 | });
|
---|