1 | import Int8Histogram from "./Int8Histogram";
|
---|
2 | import Int16Histogram from "./Int16Histogram";
|
---|
3 | import Int32Histogram from "./Int32Histogram";
|
---|
4 | import Float64Histogram from "./Float64Histogram";
|
---|
5 |
|
---|
6 | [Int8Histogram, Int16Histogram, Int32Histogram, Float64Histogram].forEach(
|
---|
7 | (Histogram) => {
|
---|
8 | describe(`${Histogram} histogram`, () => {
|
---|
9 | it("should record a value", () => {
|
---|
10 | // given
|
---|
11 | const histogram = new Histogram(1, Number.MAX_SAFE_INTEGER, 3);
|
---|
12 | // when
|
---|
13 | histogram.recordValue(123456);
|
---|
14 | // then
|
---|
15 | expect(histogram.getCountAtIndex(8073)).toBe(1);
|
---|
16 | });
|
---|
17 |
|
---|
18 | it("should compute median value in first bucket", () => {
|
---|
19 | // given
|
---|
20 | const histogram = new Histogram(1, Number.MAX_SAFE_INTEGER, 3);
|
---|
21 | histogram.recordValue(123456);
|
---|
22 | histogram.recordValue(127);
|
---|
23 | histogram.recordValue(42);
|
---|
24 | // when
|
---|
25 | const medianValue = histogram.getValueAtPercentile(50);
|
---|
26 | // then
|
---|
27 | expect(medianValue).toBe(127);
|
---|
28 | });
|
---|
29 |
|
---|
30 | it("should compute value outside first bucket with an error less than 1000", () => {
|
---|
31 | // given
|
---|
32 | const histogram = new Histogram(1, Number.MAX_SAFE_INTEGER, 3);
|
---|
33 | histogram.recordValue(123456);
|
---|
34 | histogram.recordValue(122777);
|
---|
35 | histogram.recordValue(127);
|
---|
36 | histogram.recordValue(42);
|
---|
37 | // when
|
---|
38 | const percentileValue = histogram.getValueAtPercentile(99.9);
|
---|
39 | // then
|
---|
40 | expect(Math.abs(percentileValue - 123456)).toBeLessThan(1000);
|
---|
41 | // TODO the value is 123519 > max, ask Gil if it is a bug
|
---|
42 | });
|
---|
43 |
|
---|
44 | it("should resize recording values above max", () => {
|
---|
45 | // given
|
---|
46 | const histogram = new Histogram(1, 2, 3);
|
---|
47 | histogram.autoResize = true;
|
---|
48 | // when
|
---|
49 | histogram.recordValue(123456);
|
---|
50 | histogram.recordValue(127000);
|
---|
51 | histogram.recordValue(420000);
|
---|
52 | // then
|
---|
53 | const medianValue = histogram.getValueAtPercentile(50);
|
---|
54 | expect(Math.abs(medianValue - 127000)).toBeLessThan(1000);
|
---|
55 | });
|
---|
56 |
|
---|
57 | it("should compute proper value at percentile even with rounding issues", () => {
|
---|
58 | // given
|
---|
59 | const histogram = new Histogram(1, Number.MAX_SAFE_INTEGER, 3);
|
---|
60 | histogram.recordValue(1);
|
---|
61 | histogram.recordValue(2);
|
---|
62 | // when & then
|
---|
63 | expect(histogram.getValueAtPercentile(50.0)).toBe(1);
|
---|
64 | expect(histogram.getValueAtPercentile(50.00000000000001)).toBe(1);
|
---|
65 | expect(histogram.getValueAtPercentile(50.0000000000001)).toBe(2);
|
---|
66 | });
|
---|
67 | });
|
---|
68 | }
|
---|
69 | );
|
---|