-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhistogram.js
197 lines (184 loc) · 6.38 KB
/
histogram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict'
const path = require('path')
const { HdrHistogram, HdrHistogramIterator } = require('node-gyp-build')(path.join(__dirname, '.'))
const isNumber = value => typeof value === 'number' && value !== Infinity && value !== -Infinity
class Histogram extends HdrHistogram {
/**
* Record a value in the histogram.
*
* @param {number} value the value to record.
* @param {number} count the number of times the value is to be recorded (default 1).
* @returns {boolean} true if the recording was successful, false otherwise.
* @memberof Histogram
*/
record (value, count) {
if (isNumber(value)) {
if (typeof count === 'undefined') {
return super.recordValue(value)
} else {
return super.recordValues(value, count)
}
} else {
return false
}
}
/**
* Serialize the histogram.
*
* @returns {Buffer} a Buffer containing a serialized version of the histogram.
* @memberof Histogram
*/
encode () {
return super.getEncoded()
}
/**
* Deserialize a histogram.
*
* @static
* @param {Buffer} encoded a Buffer containing a previously serialized Histogram.
* @returns {Histogram} a new populated Histogram.
* @memberof Histogram
*/
static decode (encoded) {
const histogram = new Histogram(1, 10) // the values here are immaterial
histogram.setEncoded(encoded)
return histogram
}
/**
* Get the value at a given percentile.
*
* @param {number} percentile must be > 0 and <= 100, otherwise it will throw.
* @returns {number} the value at the given percentile.
* @memberof Histogram
*/
percentile (percentile) {
if (!isNumber(percentile)) {
throw (Error('No percentile specified'))
} else if (percentile <= 0 || percentile > 100) {
throw (Error('percentile must be > 0 and <= 100'))
} else {
return super.percentile(percentile)
}
}
/**
* Get all the percentiles.
*
* Used for iterating through histogram values according to percentile levels.
* The iteration is performed in steps that start at 0% and reduce their distance
* to 100% according to the `ticksPerHalfDistance` parameter,
* ultimately reaching 100% when all recorded histogram values are exhausted.
*
* @param {number} ticksPerHalfDistance the number of iteration steps per half-distance to 100% (default 1).
* @returns {<Array>Object} an array of objects with `percentile` and `value` numeric properties.
* @memberof Histogram
*/
percentiles (ticksPerHalfDistance) {
const result = []
const iter = new HdrHistogramIterator(this)
iter.initPercentile(ticksPerHalfDistance || 1)
while (iter.next()) {
result.push({ percentile: iter.getPercentile(), value: iter.getValue() })
}
return result
}
/**
* Get the linear counts.
*
* Used for iterating through histogram values in linear steps. The iteration is performed in steps of
* `valueUnitsPerBucket` in size, terminating when all recorded histogram values are exhausted.
*
* Note that each iteration "bucket" includes values up to and including the next bucket boundary value.
*
* @param {number} valueUnitsPerBucket the bucket size as described above.
* @returns {<Array>Object} an array of objects with `count` and `value` numeric properties.
* @memberof Histogram
*/
linearcounts (valueUnitsPerBucket) {
const result = []
const iter = new HdrHistogramIterator(this)
iter.initLinear(valueUnitsPerBucket)
while (iter.next()) {
result.push({ count: iter.getCountLinear(), value: iter.getValue() })
}
return result
}
/**
* Get the logarithmic counts.
*
* Used for iterating through histogram values in logarithmically increasing levels.
* The iteration is performed in steps that start at `valueUnitsFirstBucket` and
* increase exponentially according to `logBase`, terminating when all recorded histogram
* values are exhausted.
*
* Note that each iteration "bucket" includes values up to and including the next bucket boundary value.
*
* @param {number} valueUnitsFirstBucket the value units for the first bucket as described above.
* @param {number} logBase the logarithmic base as described above.
* @returns {<Array>Object} an array of objects with `count` and `value` numeric properties.
* @memberof Histogram
*/
logcounts (valueUnitsFirstBucket, logBase) {
const result = []
const iter = new HdrHistogramIterator(this)
iter.initLog(valueUnitsFirstBucket, logBase)
while (iter.next()) {
result.push({ count: iter.getCountLog(), value: iter.getValue() })
}
return result
}
/**
* Get the recorded counts.
*
* Used for iterating through all recorded histogram values using the finest granularity
* steps supported by the underlying representation. The iteration steps through all non-zero
* recorded value counts, and terminates when all recorded histogram values are exhausted.
*
* @returns {<Array>Object} an array of objects with `count` and `value` numeric properties.
* @memberof Histogram
*/
recordedcounts () {
const result = []
const iter = new HdrHistogramIterator(this)
iter.initRecorded()
while (iter.next()) {
result.push({ count: iter.getCountRecorded(), value: iter.getValue() })
}
return result
}
/**
* Reset the Histogram so it can be reused.
*
* @returns {Histogram} the empty Histogram.
* @memberof Histogram
*/
reset () {
super.reset()
return this
}
/**
* Add values from another Histogram.
*
* @param {Histogram} histogram the Histogram containing the values to be added.
* @param {number} expectedInterval the delay between recording values (optional).
* @returns {number} the number of dropped values.
* @memberof Histogram
*/
add (histogram, expectedInterval) {
if (typeof expectedInterval === 'undefined') {
return super.add(histogram)
} else {
return super.addWhileCorrectingForCoordinatedOmission(histogram, expectedInterval)
}
}
/**
* Get the highest value that is equivalent to the given value within the histogram's resolution.
*
* @param {number} value the value for which the highest equivalent value is to be determined.
* @returns {number} the highest equivalent value.
* @memberof Histogram
*/
highestEquivalentValue (value) {
return super.nextNonEquivalentValue(value) - 1
}
}
module.exports = Histogram