source: trip-planner-front/node_modules/hdr-histogram-js/src/HistogramLogWriter.ts@ 6c1585f

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

initial commit

  • Property mode set to 100644
File size: 3.1 KB
Line 
1import { NO_TAG } from "./Histogram";
2import { encodeIntoCompressedBase64 } from "./encoding";
3import { floatFormatter } from "./formatters";
4import Histogram from "./Histogram";
5
6export interface Writable {
7 (c: string): void;
8}
9
10const HISTOGRAM_LOG_FORMAT_VERSION = "1.3";
11const timeFormatter = floatFormatter(5, 3);
12
13class HistogramLogWriter {
14 /**
15 * Base time to subtract from supplied histogram start/end timestamps when
16 * logging based on histogram timestamps.
17 * Base time is expected to be in msec since the epoch, as histogram start/end times
18 * are typically stamped with absolute times in msec since the epoch.
19 */
20 baseTime = 0;
21
22 constructor(private log: Writable) {}
23
24 /**
25 * Output an interval histogram, with the given timestamp information and the [optional] tag
26 * associated with the histogram, using a configurable maxValueUnitRatio. (note that the
27 * specified timestamp information will be used, and the timestamp information in the actual
28 * histogram will be ignored).
29 * The max value reported with the interval line will be scaled by the given maxValueUnitRatio.
30 * @param startTimeStampSec The start timestamp to log with the interval histogram, in seconds.
31 * @param endTimeStampSec The end timestamp to log with the interval histogram, in seconds.
32 * @param histogram The interval histogram to log.
33 * @param maxValueUnitRatio The ratio by which to divide the histogram's max value when reporting on it.
34 */
35 outputIntervalHistogram(
36 histogram: Histogram,
37 startTimeStampSec = (histogram.startTimeStampMsec - this.baseTime) / 1000,
38 endTimeStampSec = (histogram.endTimeStampMsec - this.baseTime) / 1000,
39 maxValueUnitRatio = 1000
40 ) {
41 const base64 = encodeIntoCompressedBase64(histogram);
42 const start = timeFormatter(startTimeStampSec);
43 const duration = timeFormatter(endTimeStampSec - startTimeStampSec);
44 const max = timeFormatter(histogram.maxValue / maxValueUnitRatio);
45 const lineContent = `${start},${duration},${max},${base64}\n`;
46 if (histogram.tag && histogram.tag !== NO_TAG) {
47 this.log(`Tag=${histogram.tag},${lineContent}`);
48 } else {
49 this.log(lineContent);
50 }
51 }
52
53 /**
54 * Log a comment to the log.
55 * Comments will be preceded with with the '#' character.
56 * @param comment the comment string.
57 */
58 outputComment(comment: string) {
59 this.log(`#${comment}\n`);
60 }
61
62 /**
63 * Log a start time in the log.
64 * @param startTimeMsec time (in milliseconds) since the absolute start time (the epoch)
65 */
66 outputStartTime(startTimeMsec: number) {
67 this.outputComment(
68 `[StartTime: ${floatFormatter(
69 5,
70 3
71 )(startTimeMsec / 1000)} (seconds since epoch), ${new Date(
72 startTimeMsec
73 )}]\n`
74 );
75 }
76
77 /**
78 * Output a legend line to the log.
79 */
80 outputLegend() {
81 this.log(
82 '"StartTimestamp","Interval_Length","Interval_Max","Interval_Compressed_Histogram"\n'
83 );
84 }
85
86 /**
87 * Output a log format version to the log.
88 */
89 outputLogFormatVersion() {
90 this.outputComment(
91 "[Histogram log format version " + HISTOGRAM_LOG_FORMAT_VERSION + "]"
92 );
93 }
94}
95
96export default HistogramLogWriter;
Note: See TracBrowser for help on using the repository browser.