(key: MetricKey.MetricKey.Histogram)
| 137 | |
| 138 | /** @internal */ |
| 139 | export const histogram = (key: MetricKey.MetricKey.Histogram): MetricHook.MetricHook.Histogram => { |
| 140 | const bounds = key.keyType.boundaries.values |
| 141 | const size = bounds.length |
| 142 | const values = new Uint32Array(size + 1) |
| 143 | // NOTE: while 64-bit floating point precision shoule be enough for any |
| 144 | // practical histogram boundary values, there is still a small chance that |
| 145 | // precision will be lost with very large / very small numbers. If we find |
| 146 | // that is the case, a more complex approach storing the histogram boundary |
| 147 | // values as a tuple of `[original: string, numeric: number]` may be warranted |
| 148 | const boundaries = new Float64Array(size) |
| 149 | let count = 0 |
| 150 | let sum = 0 |
| 151 | let min = Number.MAX_VALUE |
| 152 | let max = Number.MIN_VALUE |
| 153 | |
| 154 | pipe( |
| 155 | bounds, |
| 156 | Arr.sort(number.Order), |
| 157 | Arr.map((n, i) => { |
| 158 | boundaries[i] = n |
| 159 | }) |
| 160 | ) |
| 161 | |
| 162 | // Insert the value into the right bucket with a binary search |
| 163 | const update = (value: number) => { |
| 164 | let from = 0 |
| 165 | let to = size |
| 166 | while (from !== to) { |
| 167 | const mid = Math.floor(from + (to - from) / 2) |
| 168 | const boundary = boundaries[mid] |
| 169 | if (value <= boundary) { |
| 170 | to = mid |
| 171 | } else { |
| 172 | from = mid |
| 173 | } |
| 174 | // The special case when to / from have a distance of one |
| 175 | if (to === from + 1) { |
| 176 | if (value <= boundaries[from]) { |
| 177 | to = from |
| 178 | } else { |
| 179 | from = to |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | values[from] = values[from]! + 1 |
| 184 | count = count + 1 |
| 185 | sum = sum + value |
| 186 | if (value < min) { |
| 187 | min = value |
| 188 | } |
| 189 | if (value > max) { |
| 190 | max = value |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | const getBuckets = (): ReadonlyArray<readonly [number, number]> => { |
| 195 | const builder: Array<readonly [number, number]> = Arr.allocate(size) as any |
| 196 | let cumulated = 0 |
nothing calls this directly
no test coverage detected