HistogramLogReader.spec.js 8.8 KB

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