source: trip-planner-front/node_modules/hdr-histogram-js/src/HistogramLogReader.spec.ts@ 188ee53

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

initial commit

  • Property mode set to 100644
File size: 8.0 KB
Line 
1import * as fs from "fs";
2import Histogram from "./Histogram";
3import HistogramLogReader, { listTags } from "./HistogramLogReader";
4import PackedHistogram from "./PackedHistogram";
5import { initWebAssembly, WasmHistogram } from "./wasm";
6
7const { floor } = Math;
8
9function checkNotNull<T>(actual: T | null): asserts actual is T {
10 expect(actual).not.toBeNull();
11}
12
13describe("Histogram Log Reader", () => {
14 let fileContent: string;
15 let tagFileContent: string;
16 let fileContentWithBaseTime: string;
17 let fileContentWithoutHeader: string;
18 let fileContentWithTrailingWhitespace: string;
19 beforeAll(() => {
20 // when using mutation testing tool stryker, source code
21 // is copied in a sandbox directory without the test_files
22 // directory...
23 const runFromStryker = __dirname.includes("stryker");
24 const prefix = runFromStryker ? "../.." : ".";
25
26 fileContent = fs.readFileSync(
27 `${prefix}/test_files/jHiccup-2.0.7S.logV2.hlog`,
28 "UTF-8"
29 );
30 fileContentWithBaseTime = fs.readFileSync(
31 `${prefix}/test_files/jHiccup-with-basetime-2.0.7S.logV2.hlog`,
32 "UTF-8"
33 );
34 fileContentWithoutHeader = fs.readFileSync(
35 `${prefix}/test_files/jHiccup-no-header-2.0.7S.logV2.hlog`,
36 "UTF-8"
37 );
38 tagFileContent = fs.readFileSync(
39 `${prefix}/test_files/tagged-Log.logV2.hlog`,
40 "UTF-8"
41 );
42 fileContentWithTrailingWhitespace = fs.readFileSync(
43 `${prefix}/test_files/bug-whitespace.hlog`,
44 "UTF-8"
45 );
46 });
47
48 it("should update startTimeSec reading first histogram", () => {
49 // given
50 const reader = new HistogramLogReader(fileContent);
51 // when
52 reader.nextIntervalHistogram();
53 // then
54 expect(reader.startTimeSec).toBe(1441812279.474);
55 });
56
57 it("should read first histogram starting from the beginning", () => {
58 // given
59 const reader = new HistogramLogReader(fileContent);
60 // when
61 const histogram = reader.nextIntervalHistogram();
62 // then
63 checkNotNull(histogram);
64 // if mean is good, strong probability everything else is good as well
65 expect(floor(histogram.mean)).toBe(301998);
66 });
67
68 it("should read encoded histogram and use provided constructor", () => {
69 // given
70 const reader = new HistogramLogReader(fileContent, "packed");
71 // when
72 const histogram = reader.nextIntervalHistogram();
73 // then
74 checkNotNull(histogram);
75 // if mean is good, strong probability everything else is good as well
76 expect(floor(histogram.mean)).toBe(301998);
77 });
78
79 it("should return null if no histogram in the logs", () => {
80 // given
81 const reader = new HistogramLogReader("# empty");
82 // when
83 const histogram = reader.nextIntervalHistogram();
84 // then
85 expect(histogram).toBeNull();
86 });
87
88 it("should return next histogram in the logs", () => {
89 // given
90 const reader = new HistogramLogReader(fileContent);
91 reader.nextIntervalHistogram();
92 // when
93 const histogram = reader.nextIntervalHistogram();
94 // then
95 checkNotNull(histogram);
96 // if mean is good, strong probability everything else is good as well
97 expect(floor(histogram.mean)).toBe(293719);
98 });
99
100 it("should return null if all histograms are after specified time range", () => {
101 // given
102 const reader = new HistogramLogReader(fileContent);
103 // when
104 const histogram = reader.nextIntervalHistogram(0.01, 0.1);
105 // then
106 expect(histogram).toBeNull();
107 });
108
109 it("should return null if all histograms are before specified time range", () => {
110 // given
111 const reader = new HistogramLogReader(fileContent);
112 // when
113 const histogram = reader.nextIntervalHistogram(62, 63);
114 // then
115 expect(histogram).toBeNull();
116 });
117
118 it("should parse histogram even if there are trailing whitespaces", () => {
119 // given
120 const reader = new HistogramLogReader(fileContentWithTrailingWhitespace);
121 // when
122 const histogram = reader.nextIntervalHistogram();
123 // then
124 // no error
125 });
126
127 it("should return histograms within specified time range", () => {
128 // given
129 const reader = new HistogramLogReader(fileContent);
130 // when
131 const firstHistogram = reader.nextIntervalHistogram(0, 2);
132 const secondHistogram = reader.nextIntervalHistogram(0, 2);
133 const thirdHistogram = reader.nextIntervalHistogram(0, 2);
134 // then
135 checkNotNull(firstHistogram);
136 checkNotNull(secondHistogram);
137 expect(thirdHistogram).toBeNull();
138 // if mean is good, strong probability everything else is good as well
139 expect(floor(firstHistogram.mean)).toBe(301998);
140 expect(floor(secondHistogram.mean)).toBe(293719);
141 });
142
143 it("should set start timestamp on histogram", () => {
144 // given
145 const reader = new HistogramLogReader(fileContent);
146 // when
147 const histogram = reader.nextIntervalHistogram();
148 // then
149 checkNotNull(histogram);
150 expect(histogram.startTimeStampMsec).toBe(1441812279601);
151 });
152
153 it("should set end timestamp on histogram", () => {
154 // given
155 const reader = new HistogramLogReader(fileContent);
156 // when
157 const histogram = reader.nextIntervalHistogram();
158 // then
159 checkNotNull(histogram);
160 expect(histogram.endTimeStampMsec).toBe(1441812280608);
161 });
162
163 it("should parse tagged histogram", () => {
164 // given
165 const reader = new HistogramLogReader(tagFileContent);
166 reader.nextIntervalHistogram();
167 // when
168 const histogram = reader.nextIntervalHistogram();
169 // then
170 checkNotNull(histogram);
171 expect(histogram.tag).toBe("A");
172 expect(floor(histogram.mean)).toBe(301998);
173 });
174
175 it("should use basetime to set timestamps on histogram", () => {
176 // given
177 const reader = new HistogramLogReader(fileContentWithBaseTime);
178 // when
179 const histogram = reader.nextIntervalHistogram();
180 // then
181 checkNotNull(histogram);
182 expect(histogram.startTimeStampMsec).toBe(1441812123250);
183 expect(histogram.endTimeStampMsec).toBe(1441812124257);
184 });
185
186 it("should default startTime using 1st observed time", () => {
187 // given
188 const reader = new HistogramLogReader(fileContentWithoutHeader);
189 // when
190 const histogram = reader.nextIntervalHistogram();
191 // then
192 checkNotNull(histogram);
193 expect(histogram.startTimeStampMsec).toBe(127);
194 expect(histogram.endTimeStampMsec).toBe(1134);
195 });
196
197 it("should list all the tags of a log file", () => {
198 // given
199 // when
200 const tags = listTags(tagFileContent);
201 // then
202 expect(tags).toEqual(["NO TAG", "A"]);
203 });
204
205 it("should list all the tags of a log file where all histograms are tagged", () => {
206 // given
207 const content = `#[Fake log chunk]
208#[Histogram log format version 1.2]
209#[StartTime: 1441812279.474 (seconds since epoch), Wed Sep 09 08:24:39 PDT 2015]
210"StartTimestamp","Interval_Length","Interval_Max","Interval_Compressed_Histogram"
211Tag=NOT-EMPTY,0.127,1.007,2.769,HISTFAAAAEV42pNpmSzMwMCgyAABTBDKT4GBgdnNYMcCBvsPEBEJISEuATEZMQ4uASkhIR4nrxg9v2lMaxhvMekILGZkKmcCAEf2CsI=
212Tag=A,0.127,1.007,2.769,HISTFAAAAEV42pNpmSzMwMCgyAABTBDKT4GBgdnNYMcCBvsPEBEJISEuATEZMQ4uASkhIR4nrxg9v2lMaxhvMekILGZkKmcCAEf2CsI=
213`;
214 // when
215 const tags = listTags(content);
216 // then
217 expect(tags).toEqual(["NOT-EMPTY", "A"]);
218 });
219
220 describe("with WASM", () => {
221 let accumulatedHistogram: Histogram;
222 beforeAll(initWebAssembly);
223 afterEach(() => {
224 accumulatedHistogram.destroy();
225 });
226
227 it("should do the whole 9 yards just like the original Java version :-)", () => {
228 // given
229 const reader = new HistogramLogReader(fileContent, 32, true);
230 accumulatedHistogram = WasmHistogram.build();
231 let histogram: Histogram | null;
232 let histogramCount = 0;
233 let totalCount = 0;
234
235 // when
236 while ((histogram = reader.nextIntervalHistogram()) != null) {
237 histogramCount++;
238 totalCount += histogram.totalCount;
239 accumulatedHistogram.add(histogram as any);
240 histogram.destroy();
241 }
242
243 // then
244 expect(histogramCount).toBe(62);
245 expect(totalCount).toBe(48761);
246 expect(accumulatedHistogram.getValueAtPercentile(99.9)).toBe(1745879039);
247 expect(reader.startTimeSec).toBe(1441812279.474);
248 });
249 });
250});
Note: See TracBrowser for help on using the repository browser.