source: trip-planner-front/node_modules/hdr-histogram-js/src/Recorder.spec.ts@ 1ad8e64

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

initial commit

  • Property mode set to 100644
File size: 6.4 KB
Line 
1import Recorder from "./Recorder";
2import Int32Histogram from "./Int32Histogram";
3import PackedHistogram from "./PackedHistogram";
4import JsHistogram from "./JsHistogram";
5import { initWebAssembly, WasmHistogram } from "./wasm";
6
7describe("Recorder", () => {
8 beforeAll(initWebAssembly);
9
10 it("should record value", () => {
11 // given
12 const recorder = new Recorder();
13 // when
14 recorder.recordValue(123);
15 // then
16 const histogram = recorder.getIntervalHistogram();
17 expect(histogram.totalCount).toBe(1);
18 });
19
20 it("should record value in a packed histogram", () => {
21 // given
22 const recorder = new Recorder({
23 numberOfSignificantValueDigits: 5,
24 bitBucketSize: "packed",
25 });
26 // when
27 recorder.recordValue(123);
28 // then
29 expect(recorder.getIntervalHistogram() instanceof PackedHistogram).toBe(
30 true
31 );
32 expect(recorder.getIntervalHistogram() instanceof PackedHistogram).toBe(
33 true
34 );
35 });
36
37 it("should record value in a WASM histogram", () => {
38 // given
39 const recorder = new Recorder({
40 numberOfSignificantValueDigits: 5,
41 bitBucketSize: "packed",
42 useWebAssembly: true,
43 });
44 try {
45 // when
46 recorder.recordValue(123);
47 // then
48 expect(recorder.getIntervalHistogram() instanceof WasmHistogram).toBe(
49 true
50 );
51 } finally {
52 recorder.destroy();
53 }
54 });
55
56 it("should record value with count", () => {
57 // given
58 const recorder = new Recorder();
59 // when
60 recorder.recordValueWithCount(123, 3);
61 // then
62 const histogram = recorder.getIntervalHistogram();
63 expect(histogram.totalCount).toBe(3);
64 });
65
66 it("should record value with expected interval", () => {
67 // given
68 const recorder = new Recorder();
69 // when
70 recorder.recordValueWithExpectedInterval(223, 100);
71 // then
72 const histogram = recorder.getIntervalHistogram();
73 expect(histogram.totalCount).toBe(2);
74 });
75
76 it("should record value in a packed histogram", () => {
77 // given
78 const recorder = new Recorder({ bitBucketSize: "packed" });
79 recorder.recordValue(42);
80 // when
81 const histogram = recorder.getIntervalHistogram();
82 // then
83 expect(histogram instanceof PackedHistogram).toBe(true);
84 });
85
86 it("should record value only on one interval histogram", () => {
87 // given
88 const recorder = new Recorder();
89 // when
90 recorder.recordValue(123);
91 const firstHistogram = recorder.getIntervalHistogram();
92 // then
93 const secondHistogram = recorder.getIntervalHistogram();
94 expect(secondHistogram.totalCount).toBe(0);
95 });
96
97 it("should not record value on returned interval histogram", () => {
98 // given
99 const recorder = new Recorder();
100 const firstHistogram = recorder.getIntervalHistogram();
101 const secondHistogram = recorder.getIntervalHistogram();
102 // when
103 firstHistogram.recordValue(42); // should have 0 impact on recorder
104 const thirdHistogram = recorder.getIntervalHistogram();
105 // then
106 expect(thirdHistogram.totalCount).toBe(0);
107 });
108
109 it("should return interval histograms with expected significant digits", () => {
110 // given
111 const recorder = new Recorder({ numberOfSignificantValueDigits: 4 });
112 const firstHistogram = recorder.getIntervalHistogram();
113 const secondHistogram = recorder.getIntervalHistogram();
114 // when
115 const thirdHistogram = recorder.getIntervalHistogram();
116 // then
117 expect((thirdHistogram as JsHistogram).numberOfSignificantValueDigits).toBe(
118 4
119 );
120 });
121
122 it("should return recycled histograms when asking for interval histogram", () => {
123 // given
124 const recorder = new Recorder();
125 const firstHistogram = recorder.getIntervalHistogram();
126 // when
127 const secondHistogram = recorder.getIntervalHistogram(firstHistogram);
128 const thirdHistogram = recorder.getIntervalHistogram();
129 // then
130 expect(thirdHistogram === firstHistogram).toBe(true);
131 });
132
133 it("should throw an error when trying to recycle an histogram not created by the recorder", () => {
134 // given
135 const recorder = new Recorder();
136 const somehistogram = new Int32Histogram(1, 2, 3);
137 // when & then
138 expect(() => recorder.getIntervalHistogram(somehistogram)).toThrowError();
139 });
140
141 it("should reset histogram when recycling", () => {
142 // given
143 const recorder = new Recorder();
144 recorder.recordValue(42);
145 const firstHistogram = recorder.getIntervalHistogram();
146 // when
147 const secondHistogram = recorder.getIntervalHistogram(firstHistogram);
148 const thirdHistogram = recorder.getIntervalHistogram();
149 // then
150 expect(thirdHistogram.totalCount).toBe(0);
151 });
152
153 it("should set timestamps on first interval histogram", () => {
154 // given
155 let currentTime = 42;
156 let clock = () => currentTime;
157 const recorder = new Recorder({}, clock);
158 // when
159 currentTime = 123;
160 const histogram = recorder.getIntervalHistogram();
161 // then
162 expect(histogram.startTimeStampMsec).toBe(42);
163 expect(histogram.endTimeStampMsec).toBe(123);
164 });
165
166 it("should set timestamps on any interval histogram", () => {
167 // given
168 let currentTime = 42;
169 let clock = () => currentTime;
170 const recorder = new Recorder({}, clock);
171 currentTime = 51;
172 const firstHistogram = recorder.getIntervalHistogram();
173 // when
174 currentTime = 56;
175 const secondHistogram = recorder.getIntervalHistogram();
176 // then
177 expect(secondHistogram.startTimeStampMsec).toBe(51);
178 expect(secondHistogram.endTimeStampMsec).toBe(56);
179 });
180
181 it("should copy interval histogram", () => {
182 // given
183 let currentTime = 42;
184 let clock = () => currentTime;
185 const recorder = new Recorder({ numberOfSignificantValueDigits: 4 }, clock);
186 recorder.recordValue(123);
187 // when
188 const histogram = new Int32Histogram(1, Number.MAX_SAFE_INTEGER, 3);
189 currentTime = 51;
190 recorder.getIntervalHistogramInto(histogram);
191 // then
192 expect(histogram.totalCount).toBe(1);
193 expect(histogram.startTimeStampMsec).toBe(42);
194 expect(histogram.endTimeStampMsec).toBe(51);
195 });
196
197 it("should reset values and timestamp", () => {
198 // given
199 let currentTime = 42;
200 let clock = () => currentTime;
201 const recorder = new Recorder({ numberOfSignificantValueDigits: 4 }, clock);
202 recorder.recordValue(123);
203 // when
204 currentTime = 55;
205 recorder.reset();
206 const histogram = recorder.getIntervalHistogram();
207 // then
208 expect(histogram.totalCount).toBe(0);
209 expect(histogram.startTimeStampMsec).toBe(55);
210 });
211});
Note: See TracBrowser for help on using the repository browser.