1 | /*
|
---|
2 | * This is a AssemblyScript 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 |
|
---|
9 | import Histogram from "./Histogram";
|
---|
10 | import HistogramIterationValue from "./HistogramIterationValue";
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Used for iterating through all recorded histogram values using the finest granularity steps supported by the
|
---|
14 | * underlying representation. The iteration steps through all non-zero recorded value counts, and terminates when
|
---|
15 | * all recorded histogram values are exhausted.
|
---|
16 | */
|
---|
17 | class RecordedValuesIterator<T, U> {
|
---|
18 | visitedIndex: i32;
|
---|
19 | histogram: Histogram<T, U>;
|
---|
20 | savedHistogramTotalRawCount: u64;
|
---|
21 | currentIndex: i32;
|
---|
22 | currentValueAtIndex: u64;
|
---|
23 | nextValueAtIndex: u64;
|
---|
24 | prevValueIteratedTo: u64;
|
---|
25 | totalCountToPrevIndex: u64;
|
---|
26 | totalCountToCurrentIndex: u64;
|
---|
27 | totalValueToCurrentIndex: u64;
|
---|
28 | arrayTotalCount: u64;
|
---|
29 | countAtThisValue: u64;
|
---|
30 |
|
---|
31 | private freshSubBucket: boolean;
|
---|
32 |
|
---|
33 | currentIterationValue: HistogramIterationValue = new HistogramIterationValue();
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @param histogram The histogram this iterator will operate on
|
---|
37 | */
|
---|
38 | constructor(histogram: Histogram<T, U>) {
|
---|
39 | this.doReset(histogram);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Reset iterator for re-use in a fresh iteration over the same histogram data set.
|
---|
44 | */
|
---|
45 | public reset(): void {
|
---|
46 | this.doReset(this.histogram);
|
---|
47 | }
|
---|
48 |
|
---|
49 | private doReset(histogram: Histogram<T, U>): void {
|
---|
50 | this.resetIterator(histogram);
|
---|
51 | this.visitedIndex = -1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | incrementIterationLevel(): void {
|
---|
55 | this.visitedIndex = this.currentIndex;
|
---|
56 | }
|
---|
57 |
|
---|
58 | reachedIterationLevel(): bool {
|
---|
59 | const currentCount = this.histogram.getCountAtIndex(this.currentIndex);
|
---|
60 | return currentCount != 0 && this.visitedIndex !== this.currentIndex;
|
---|
61 | }
|
---|
62 |
|
---|
63 | resetIterator(histogram: Histogram<T, U>): void {
|
---|
64 | this.histogram = histogram;
|
---|
65 | this.savedHistogramTotalRawCount = histogram.totalCount;
|
---|
66 | this.arrayTotalCount = histogram.totalCount;
|
---|
67 | this.currentIndex = 0;
|
---|
68 | this.currentValueAtIndex = 0;
|
---|
69 | this.nextValueAtIndex = 1 << histogram.unitMagnitude;
|
---|
70 | this.prevValueIteratedTo = 0;
|
---|
71 | this.totalCountToPrevIndex = 0;
|
---|
72 | this.totalCountToCurrentIndex = 0;
|
---|
73 | this.totalValueToCurrentIndex = 0;
|
---|
74 | this.countAtThisValue = 0;
|
---|
75 | this.freshSubBucket = true;
|
---|
76 | this.currentIterationValue.reset();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Returns true if the iteration has more elements. (In other words, returns true if next would return an
|
---|
81 | * element rather than throwing an exception.)
|
---|
82 | *
|
---|
83 | * @return true if the iterator has more elements.
|
---|
84 | */
|
---|
85 | public hasNext(): boolean {
|
---|
86 | if (this.histogram.totalCount !== this.savedHistogramTotalRawCount) {
|
---|
87 | throw "Concurrent Modification Exception";
|
---|
88 | }
|
---|
89 | return this.totalCountToCurrentIndex < this.arrayTotalCount;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Returns the next element in the iteration.
|
---|
94 | *
|
---|
95 | * @return the {@link HistogramIterationValue} associated with the next element in the iteration.
|
---|
96 | */
|
---|
97 | public next(): HistogramIterationValue {
|
---|
98 | // Move through the sub buckets and buckets until we hit the next reporting level:
|
---|
99 | while (!this.exhaustedSubBuckets()) {
|
---|
100 | this.countAtThisValue = this.histogram.getCountAtIndex(this.currentIndex);
|
---|
101 | if (this.freshSubBucket) {
|
---|
102 | // Don't add unless we've incremented since last bucket...
|
---|
103 | this.totalCountToCurrentIndex += this.countAtThisValue;
|
---|
104 | this.totalValueToCurrentIndex +=
|
---|
105 | this.countAtThisValue *
|
---|
106 | this.histogram.highestEquivalentValue(this.currentValueAtIndex);
|
---|
107 | this.freshSubBucket = false;
|
---|
108 | }
|
---|
109 | if (this.reachedIterationLevel()) {
|
---|
110 | const valueIteratedTo = this.getValueIteratedTo();
|
---|
111 |
|
---|
112 | //Object.assign(this.currentIterationValue, {
|
---|
113 | this.currentIterationValue.valueIteratedTo = valueIteratedTo;
|
---|
114 | this.currentIterationValue.valueIteratedFrom = this.prevValueIteratedTo;
|
---|
115 | this.currentIterationValue.countAtValueIteratedTo = this.countAtThisValue;
|
---|
116 | this.currentIterationValue.countAddedInThisIterationStep =
|
---|
117 | this.totalCountToCurrentIndex - this.totalCountToPrevIndex;
|
---|
118 | this.currentIterationValue.totalCountToThisValue = this.totalCountToCurrentIndex;
|
---|
119 | this.currentIterationValue.totalValueToThisValue = this.totalValueToCurrentIndex;
|
---|
120 | this.currentIterationValue.percentile =
|
---|
121 | (<f64>100 * <f64>this.totalCountToCurrentIndex) /
|
---|
122 | <f64>this.arrayTotalCount;
|
---|
123 | this.currentIterationValue.percentileLevelIteratedTo = this.getPercentileIteratedTo();
|
---|
124 | this.prevValueIteratedTo = valueIteratedTo;
|
---|
125 | this.totalCountToPrevIndex = this.totalCountToCurrentIndex;
|
---|
126 | this.incrementIterationLevel();
|
---|
127 | if (this.histogram.totalCount !== this.savedHistogramTotalRawCount) {
|
---|
128 | throw new Error("Concurrent Modification Exception");
|
---|
129 | }
|
---|
130 | return this.currentIterationValue;
|
---|
131 | }
|
---|
132 | this.incrementSubBucket();
|
---|
133 | }
|
---|
134 | throw new Error("Index Out Of Bounds Exception");
|
---|
135 | }
|
---|
136 |
|
---|
137 | getPercentileIteratedTo(): f64 {
|
---|
138 | return (
|
---|
139 | (<f64>100 * <f64>this.totalCountToCurrentIndex) /
|
---|
140 | <f64>this.arrayTotalCount
|
---|
141 | );
|
---|
142 | }
|
---|
143 |
|
---|
144 | getPercentileIteratedFrom(): f64 {
|
---|
145 | return (
|
---|
146 | (<f64>100 * <f64>this.totalCountToPrevIndex) / <f64>this.arrayTotalCount
|
---|
147 | );
|
---|
148 | }
|
---|
149 |
|
---|
150 | getValueIteratedTo(): u64 {
|
---|
151 | return this.histogram.highestEquivalentValue(this.currentValueAtIndex);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private exhaustedSubBuckets(): boolean {
|
---|
155 | return this.currentIndex >= this.histogram.countsArrayLength;
|
---|
156 | }
|
---|
157 |
|
---|
158 | incrementSubBucket(): void {
|
---|
159 | this.freshSubBucket = true;
|
---|
160 | this.currentIndex++;
|
---|
161 | this.currentValueAtIndex = this.histogram.valueFromIndex(this.currentIndex);
|
---|
162 | this.nextValueAtIndex = this.histogram.valueFromIndex(
|
---|
163 | this.currentIndex + 1
|
---|
164 | );
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | export default RecordedValuesIterator;
|
---|