1 | /*
|
---|
2 | * This is a TypeScript port of the original Java version, which was written by
|
---|
3 | * Gil Tene as described in
|
---|
4 | * https://github.com/HdrHistogram/HdrHistogram
|
---|
5 | * and released to the public domain, as explained at
|
---|
6 | * http://creativecommons.org/publicdomain/zero/1.0/
|
---|
7 | */
|
---|
8 | import JsHistogram from "./JsHistogram";
|
---|
9 | import { PackedArray } from "./packedarray/PackedArray";
|
---|
10 | /**
|
---|
11 | * <h3>A High Dynamic Range (HDR) Histogram that uses a packed internal representation</h3>
|
---|
12 | * <p>
|
---|
13 | * {@link PackedHistogram} supports the recording and analyzing sampled data value counts across a configurable
|
---|
14 | * integer value range with configurable value precision within the range. Value precision is expressed as the
|
---|
15 | * number of significant digits in the value recording, and provides control over value quantization behavior
|
---|
16 | * across the value range and the subsequent value resolution at any given level.
|
---|
17 | * <p>
|
---|
18 | * {@link PackedHistogram} tracks value counts in a packed internal representation optimized
|
---|
19 | * for typical histogram recoded values are sparse in the value range and tend to be incremented in small unit counts.
|
---|
20 | * This packed representation tends to require significantly smaller amounts of stoarge when compared to unpacked
|
---|
21 | * representations, but can incur additional recording cost due to resizing and repacking operations that may
|
---|
22 | * occur as previously unrecorded values are encountered.
|
---|
23 | * <p>
|
---|
24 | * For example, a {@link PackedHistogram} could be configured to track the counts of observed integer values between 0 and
|
---|
25 | * 3,600,000,000,000 while maintaining a value precision of 3 significant digits across that range. Value quantization
|
---|
26 | * within the range will thus be no larger than 1/1,000th (or 0.1%) of any value. This example Histogram could
|
---|
27 | * be used to track and analyze the counts of observed response times ranging between 1 nanosecond and 1 hour
|
---|
28 | * in magnitude, while maintaining a value resolution of 1 microsecond up to 1 millisecond, a resolution of
|
---|
29 | * 1 millisecond (or better) up to one second, and a resolution of 1 second (or better) up to 1,000 seconds. At its
|
---|
30 | * maximum tracked value (1 hour), it would still maintain a resolution of 3.6 seconds (or better).
|
---|
31 | * <p>
|
---|
32 | * Auto-resizing: When constructed with no specified value range range (or when auto-resize is turned on with {@link
|
---|
33 | * Histogram#setAutoResize}) a {@link PackedHistogram} will auto-resize its dynamic range to include recorded values as
|
---|
34 | * they are encountered. Note that recording calls that cause auto-resizing may take longer to execute, as resizing
|
---|
35 | * incurs allocation and copying of internal data structures.
|
---|
36 | * <p>
|
---|
37 | */
|
---|
38 | class PackedHistogram extends JsHistogram {
|
---|
39 | packedCounts: PackedArray;
|
---|
40 |
|
---|
41 | constructor(
|
---|
42 | lowestDiscernibleValue: number,
|
---|
43 | highestTrackableValue: number,
|
---|
44 | numberOfSignificantValueDigits: number
|
---|
45 | ) {
|
---|
46 | super(
|
---|
47 | lowestDiscernibleValue,
|
---|
48 | highestTrackableValue,
|
---|
49 | numberOfSignificantValueDigits
|
---|
50 | );
|
---|
51 | this._totalCount = 0;
|
---|
52 | this.packedCounts = new PackedArray(this.countsArrayLength);
|
---|
53 | }
|
---|
54 |
|
---|
55 | clearCounts() {
|
---|
56 | this.packedCounts.clear();
|
---|
57 | }
|
---|
58 |
|
---|
59 | incrementCountAtIndex(index: number) {
|
---|
60 | this.packedCounts.increment(index);
|
---|
61 | }
|
---|
62 |
|
---|
63 | addToCountAtIndex(index: number, value: number) {
|
---|
64 | this.packedCounts.add(index, value);
|
---|
65 | }
|
---|
66 |
|
---|
67 | setCountAtIndex(index: number, value: number) {
|
---|
68 | this.packedCounts.set(index, value);
|
---|
69 | }
|
---|
70 |
|
---|
71 | resize(newHighestTrackableValue: number) {
|
---|
72 | this.establishSize(newHighestTrackableValue);
|
---|
73 | this.packedCounts.setVirtualLength(this.countsArrayLength);
|
---|
74 | }
|
---|
75 |
|
---|
76 | getCountAtIndex(index: number) {
|
---|
77 | return this.packedCounts.get(index);
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected _getEstimatedFootprintInBytes() {
|
---|
81 | return 192 + 8 * this.packedCounts.getPhysicalLength();
|
---|
82 | }
|
---|
83 |
|
---|
84 | copyCorrectedForCoordinatedOmission(
|
---|
85 | expectedIntervalBetweenValueSamples: number
|
---|
86 | ) {
|
---|
87 | const copy = new PackedHistogram(
|
---|
88 | this.lowestDiscernibleValue,
|
---|
89 | this.highestTrackableValue,
|
---|
90 | this.numberOfSignificantValueDigits
|
---|
91 | );
|
---|
92 | copy.addWhileCorrectingForCoordinatedOmission(
|
---|
93 | this,
|
---|
94 | expectedIntervalBetweenValueSamples
|
---|
95 | );
|
---|
96 | return copy;
|
---|
97 | }
|
---|
98 |
|
---|
99 | toString() {
|
---|
100 | return `PackedHistogram ${JSON.stringify(this, null, 2)}`;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | export default PackedHistogram;
|
---|