[6a3a178] | 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 {
|
---|
| 11 | Uint8Storage,
|
---|
| 12 | Uint16Storage,
|
---|
| 13 | Uint32Storage,
|
---|
| 14 | Uint64Storage,
|
---|
| 15 | } from "./Histogram";
|
---|
| 16 | import { decodeFromByteBuffer } from "./encoding";
|
---|
| 17 | import ByteBuffer from "./ByteBuffer";
|
---|
| 18 | import { PackedArray } from "./packedarray/PackedArray";
|
---|
| 19 |
|
---|
| 20 | export const UINT8ARRAY_ID = idof<Uint8Array>();
|
---|
| 21 |
|
---|
| 22 | class HistogramAdapter<T, U> {
|
---|
| 23 | private _histogram: Histogram<T, U>;
|
---|
| 24 | constructor(
|
---|
| 25 | lowestDiscernibleValue: f64,
|
---|
| 26 | highestTrackableValue: f64,
|
---|
| 27 | numberOfSignificantValueDigits: f64,
|
---|
| 28 | autoResize: boolean,
|
---|
| 29 | histogram: Histogram<T, U> | null = null
|
---|
| 30 | ) {
|
---|
| 31 | if (histogram) {
|
---|
| 32 | this._histogram = histogram;
|
---|
| 33 | } else {
|
---|
| 34 | this._histogram = new Histogram<T, U>(
|
---|
| 35 | <u64>lowestDiscernibleValue,
|
---|
| 36 | <u64>highestTrackableValue,
|
---|
| 37 | <u8>numberOfSignificantValueDigits
|
---|
| 38 | );
|
---|
| 39 | this._histogram.autoResize = autoResize;
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public get autoResize(): boolean {
|
---|
| 44 | return this._histogram.autoResize;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public set autoResize(resize: boolean) {
|
---|
| 48 | this._histogram.autoResize = resize;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public get highestTrackableValue(): f64 {
|
---|
| 52 | return <f64>this._histogram.highestTrackableValue;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public set highestTrackableValue(value: f64) {
|
---|
| 56 | this._histogram.highestTrackableValue = <u64>value;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public get startTimeStampMsec(): f64 {
|
---|
| 60 | return <f64>this._histogram.startTimeStampMsec;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public set startTimeStampMsec(value: f64) {
|
---|
| 64 | this._histogram.startTimeStampMsec = <u64>value;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public get endTimeStampMsec(): f64 {
|
---|
| 68 | return <f64>this._histogram.endTimeStampMsec;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public set endTimeStampMsec(value: f64) {
|
---|
| 72 | this._histogram.endTimeStampMsec = <u64>value;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public get minNonZeroValue(): f64 {
|
---|
| 76 | return <f64>this._histogram.minNonZeroValue;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public get maxValue(): f64 {
|
---|
| 80 | return <f64>this._histogram.maxValue;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public get totalCount(): f64 {
|
---|
| 84 | return <f64>this._histogram.totalCount;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public get stdDeviation(): f64 {
|
---|
| 88 | return <f64>this._histogram.getStdDeviation();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public get mean(): f64 {
|
---|
| 92 | return <f64>this._histogram.getMean();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public get estimatedFootprintInBytes(): f64 {
|
---|
| 96 | return <f64>(
|
---|
| 97 | (offsetof<HistogramAdapter<T, U>>() +
|
---|
| 98 | this._histogram.estimatedFootprintInBytes)
|
---|
| 99 | );
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | recordValue(value: f64): void {
|
---|
| 103 | this._histogram.recordSingleValue(<u64>value);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | recordValueWithCount(value: f64, count: f64): void {
|
---|
| 107 | this._histogram.recordCountAtValue(<u64>count, <u64>value);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | recordValueWithExpectedInterval(
|
---|
| 111 | value: f64,
|
---|
| 112 | expectedIntervalBetweenValueSamples: f64
|
---|
| 113 | ): void {
|
---|
| 114 | this._histogram.recordSingleValueWithExpectedInterval(
|
---|
| 115 | <u64>value,
|
---|
| 116 | <u64>expectedIntervalBetweenValueSamples
|
---|
| 117 | );
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | getValueAtPercentile(percentile: f64): f64 {
|
---|
| 121 | return <f64>this._histogram.getValueAtPercentile(percentile);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | outputPercentileDistribution(
|
---|
| 125 | percentileTicksPerHalfDistance: f64,
|
---|
| 126 | outputValueUnitScalingRatio: f64
|
---|
| 127 | ): string {
|
---|
| 128 | return this._histogram.outputPercentileDistribution(
|
---|
| 129 | <i32>percentileTicksPerHalfDistance,
|
---|
| 130 | outputValueUnitScalingRatio
|
---|
| 131 | );
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | copyCorrectedForCoordinatedOmission(
|
---|
| 135 | expectedIntervalBetweenValueSamples: f64
|
---|
| 136 | ): HistogramAdapter<T, U> {
|
---|
| 137 | const copy = this._histogram.copyCorrectedForCoordinatedOmission(
|
---|
| 138 | <u64>expectedIntervalBetweenValueSamples
|
---|
| 139 | );
|
---|
| 140 |
|
---|
| 141 | return new HistogramAdapter<T, U>(0, 0, 0, false, copy);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | addHistogram8(otherHistogram: Histogram8): void {
|
---|
| 145 | this._histogram.add(otherHistogram._histogram);
|
---|
| 146 | }
|
---|
| 147 | addHistogram16(otherHistogram: Histogram16): void {
|
---|
| 148 | this._histogram.add(otherHistogram._histogram);
|
---|
| 149 | }
|
---|
| 150 | addHistogram32(otherHistogram: Histogram32): void {
|
---|
| 151 | this._histogram.add(otherHistogram._histogram);
|
---|
| 152 | }
|
---|
| 153 | addHistogram64(otherHistogram: Histogram64): void {
|
---|
| 154 | this._histogram.add(otherHistogram._histogram);
|
---|
| 155 | }
|
---|
| 156 | addPackedHistogram(otherHistogram: PackedHistogram): void {
|
---|
| 157 | this._histogram.add(otherHistogram._histogram);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | subtractHistogram8(otherHistogram: Histogram8): void {
|
---|
| 161 | this._histogram.subtract(otherHistogram._histogram);
|
---|
| 162 | }
|
---|
| 163 | subtractHistogram16(otherHistogram: Histogram16): void {
|
---|
| 164 | this._histogram.subtract(otherHistogram._histogram);
|
---|
| 165 | }
|
---|
| 166 | subtractHistogram32(otherHistogram: Histogram32): void {
|
---|
| 167 | this._histogram.subtract(otherHistogram._histogram);
|
---|
| 168 | }
|
---|
| 169 | subtractHistogram64(otherHistogram: Histogram64): void {
|
---|
| 170 | this._histogram.subtract(otherHistogram._histogram);
|
---|
| 171 | }
|
---|
| 172 | subtractPackedHistogram(otherHistogram: PackedHistogram): void {
|
---|
| 173 | this._histogram.subtract(otherHistogram._histogram);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | encode(): Uint8Array {
|
---|
| 177 | return this._histogram.encode();
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | reset(): void {
|
---|
| 181 | this._histogram.reset();
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | export class Histogram8 extends HistogramAdapter<Uint8Storage, u8> {}
|
---|
| 186 | export class Histogram16 extends HistogramAdapter<Uint16Storage, u16> {}
|
---|
| 187 | export class Histogram32 extends HistogramAdapter<Uint32Storage, u32> {}
|
---|
| 188 | export class Histogram64 extends HistogramAdapter<Uint64Storage, u64> {}
|
---|
| 189 | export class PackedHistogram extends HistogramAdapter<PackedArray, u64> {}
|
---|
| 190 |
|
---|
| 191 | function decodeHistogram<T, U>(
|
---|
| 192 | data: Uint8Array,
|
---|
| 193 | minBarForHighestTrackableValue: f64
|
---|
| 194 | ): HistogramAdapter<T, U> {
|
---|
| 195 | const buffer = new ByteBuffer(data);
|
---|
| 196 | const histogram = decodeFromByteBuffer<T, U>(
|
---|
| 197 | buffer,
|
---|
| 198 | <u64>minBarForHighestTrackableValue
|
---|
| 199 | );
|
---|
| 200 | return new HistogramAdapter<T, U>(0, 0, 0, false, histogram);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | export function decodeHistogram8(
|
---|
| 204 | data: Uint8Array,
|
---|
| 205 | minBarForHighestTrackableValue: f64
|
---|
| 206 | ): HistogramAdapter<Uint8Storage, u8> {
|
---|
| 207 | return decodeHistogram<Uint8Storage, u8>(
|
---|
| 208 | data,
|
---|
| 209 | minBarForHighestTrackableValue
|
---|
| 210 | );
|
---|
| 211 | }
|
---|
| 212 | export function decodeHistogram16(
|
---|
| 213 | data: Uint8Array,
|
---|
| 214 | minBarForHighestTrackableValue: f64
|
---|
| 215 | ): HistogramAdapter<Uint16Storage, u16> {
|
---|
| 216 | return decodeHistogram<Uint16Storage, u16>(
|
---|
| 217 | data,
|
---|
| 218 | minBarForHighestTrackableValue
|
---|
| 219 | );
|
---|
| 220 | }
|
---|
| 221 | export function decodeHistogram32(
|
---|
| 222 | data: Uint8Array,
|
---|
| 223 | minBarForHighestTrackableValue: f64
|
---|
| 224 | ): HistogramAdapter<Uint32Storage, u32> {
|
---|
| 225 | return decodeHistogram<Uint32Storage, u32>(
|
---|
| 226 | data,
|
---|
| 227 | minBarForHighestTrackableValue
|
---|
| 228 | );
|
---|
| 229 | }
|
---|
| 230 | export function decodeHistogram64(
|
---|
| 231 | data: Uint8Array,
|
---|
| 232 | minBarForHighestTrackableValue: f64
|
---|
| 233 | ): HistogramAdapter<Uint64Storage, u64> {
|
---|
| 234 | return decodeHistogram<Uint64Storage, u64>(
|
---|
| 235 | data,
|
---|
| 236 | minBarForHighestTrackableValue
|
---|
| 237 | );
|
---|
| 238 | }
|
---|
| 239 | export function decodePackedHistogram(
|
---|
| 240 | data: Uint8Array,
|
---|
| 241 | minBarForHighestTrackableValue: f64
|
---|
| 242 | ): HistogramAdapter<PackedArray, u64> {
|
---|
| 243 | return decodeHistogram<PackedArray, u64>(
|
---|
| 244 | data,
|
---|
| 245 | minBarForHighestTrackableValue
|
---|
| 246 | );
|
---|
| 247 | }
|
---|