-
Notifications
You must be signed in to change notification settings - Fork 34
/
histogram.py
608 lines (531 loc) · 26.9 KB
/
histogram.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
'''
A pure python version of the hdr_histogram code
Ported from
https://github.com/HdrHistogram/HdrHistogram (Java)
https://github.com/HdrHistogram/HdrHistogram_c (C)
Written by Alec Hothan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
from __future__ import division, print_function
from builtins import range
from builtins import object
import math
import sys
from hdrh.iterators import AllValuesIterator
from hdrh.iterators import RecordedIterator
from hdrh.iterators import PercentileIterator
from hdrh.iterators import LinearIterator
from hdrh.iterators import LogIterator
from hdrh.codec import HdrHistogramEncoder
def get_bucket_count(value, subb_count, unit_mag):
smallest_untrackable_value = subb_count << unit_mag
buckets_needed = 1
while smallest_untrackable_value <= value:
if smallest_untrackable_value > sys.maxsize // 2:
return buckets_needed + 1
smallest_untrackable_value <<= 1
buckets_needed += 1
return buckets_needed
class HdrHistogram(object):
'''This class supports the recording and analyzing of sampled data value
counts across a configurable integer value range with configurable value
precision within the range. Value precision is expressed as the number of
significant digits in the value recording, and provides control over value
quantization behavior across the value range and the subsequent value
resolution at any given level.
For example, a Histogram could be configured to track the counts of
observed integer values between 0 and 3,600,000,000 while maintaining a
value precision of 3 significant digits across that range. Value
quantization within the range will thus be no larger than 1/1,000th
(or 0.1%) of any value. This example Histogram could be used to track and
analyze the counts of observed response times ranging between 1 microsecond
and 1 hour in magnitude, while maintaining a value resolution of 1
microsecond up to 1 millisecond, a resolution of 1 millisecond (or better)
up to one second, and a resolution of 1 second (or better) up to 1,000
seconds. At it's maximum tracked value (1 hour), it would still maintain a
resolution of 3.6 seconds (or better).
'''
def __init__(self,
lowest_trackable_value,
highest_trackable_value,
significant_figures,
word_size=8,
b64_wrap=True,
hdr_payload=None):
'''Create a new histogram with given arguments
Params:
lowest_trackable_value The lowest value that can be discerned
(distinguished from 0) by the histogram.
Must be a positive integer that is >= 1.
May be internally rounded down to nearest power of 2.
highest_trackable_value The highest value to be tracked by the
histogram. Must be a positive integer that is >=
(2 * lowest_trackable_value).
significant_figures The number of significant decimal digits to
which the histogram will maintain value resolution and
separation. Must be a non-negative integer between 0 and 5.
word_size size of counters in bytes, only 2, 4, 8-byte counters
are supported (default is 8-byte or 64-bit counters)
b64_wrap specifies if the encoding of this histogram should use
base64 wrapping (only useful if you need to encode the histogram
to save somewhere or send over the wire. By default base64
encoding is assumed
hdr_payload only used for associating an existing payload created
from decoding an encoded histograme
Exceptions:
ValueError if the word_size value is unsupported
if significant_figures is invalid
'''
if significant_figures < 1 or significant_figures > 5:
raise ValueError('Invalid significant_figures')
self.lowest_trackable_value = lowest_trackable_value
self.highest_trackable_value = highest_trackable_value
self.significant_figures = significant_figures
self.unit_magnitude = int(math.floor(math.log(lowest_trackable_value) / math.log(2)))
largest_value_single_unit_res = 2 * math.pow(10, significant_figures)
subb_count_mag = int(math.ceil(math.log(largest_value_single_unit_res) / math.log(2)))
self.sub_bucket_half_count_magnitude = subb_count_mag - 1 if subb_count_mag > 1 else 0
self.sub_bucket_count = int(math.pow(2, self.sub_bucket_half_count_magnitude + 1))
self.sub_bucket_half_count = self.sub_bucket_count // 2
self.sub_bucket_mask = (self.sub_bucket_count - 1) << self.unit_magnitude
self.bucket_count = get_bucket_count(highest_trackable_value,
self.sub_bucket_count,
self.unit_magnitude)
self.min_value = sys.maxsize
self.max_value = 0
self.total_count = 0
self.counts_len = (self.bucket_count + 1) * (self.sub_bucket_count // 2)
self.word_size = word_size
if hdr_payload:
payload = hdr_payload.payload
self.int_to_double_conversion_ratio = payload.conversion_ratio_bits
results = hdr_payload.init_counts(self.counts_len)
if results['total']:
self.set_internal_tacking_values(results['min_nonzero_index'],
results['max_nonzero_index'],
results['total'])
else:
self.int_to_double_conversion_ratio = 1.0
# to encode this histogram into a compressed/base64 format ready
# to be exported
self.b64_wrap = b64_wrap
self.encoder = HdrHistogramEncoder(self, b64_wrap, hdr_payload)
# the counters reside directly in the payload object
# allocated by the encoder
# so that compression for wire transfer can be done without copy
self.counts = self.encoder.get_counts()
self.start_time_stamp_msec = 0
self.end_time_stamp_msec = 0
def _clz(self, value):
"""calculate the leading zeros, equivalent to C __builtin_clzll()
value in hex:
value = 1 clz = 63
value = 2 clz = 62
value = 4 clz = 61
value = 1000 clz = 51
value = 1000000 clz = 39
"""
return 63 - (len(bin(value)) - 3)
def _get_bucket_index(self, value):
# smallest power of 2 containing value
pow2ceiling = 64 - self._clz(int(value) | self.sub_bucket_mask)
return int(pow2ceiling - self.unit_magnitude -
(self.sub_bucket_half_count_magnitude + 1))
def _get_sub_bucket_index(self, value, bucket_index):
return int(value) >> (bucket_index + self.unit_magnitude)
def _counts_index(self, bucket_index, sub_bucket_index):
# Calculate the index for the first entry in the bucket:
# (The following is the equivalent of ((bucket_index + 1) * subBucketHalfCount) ):
bucket_base_index = (bucket_index + 1) << self.sub_bucket_half_count_magnitude
# Calculate the offset in the bucket:
offset_in_bucket = sub_bucket_index - self.sub_bucket_half_count
# The following is the equivalent of
# ((sub_bucket_index - subBucketHalfCount) + bucketBaseIndex
return bucket_base_index + offset_in_bucket
def _counts_index_for(self, value):
bucket_index = self._get_bucket_index(value)
sub_bucket_index = self._get_sub_bucket_index(value, bucket_index)
return self._counts_index(bucket_index, sub_bucket_index)
def record_value(self, value, count=1):
'''Record a new value into the histogram
Args:
value: the value to record (must be in the valid range)
count: incremental count (defaults to 1)
'''
if value < 0:
return False
counts_index = self._counts_index_for(value)
if (counts_index < 0) or (self.counts_len <= counts_index):
return False
self.counts[counts_index] += count
self.total_count += count
self.min_value = min(self.min_value, value)
self.max_value = max(self.max_value, value)
return True
def record_corrected_value(self, value, expected_interval, count=1):
'''Record a new value into the histogram and correct for
coordinated omission if needed
Args:
value: the value to record (must be in the valid range)
expected_interval: the expected interval between 2 value samples
count: incremental count (defaults to 1)
'''
while True:
if not self.record_value(value, count):
return False
if value <= expected_interval or expected_interval <= 0:
return True
value -= expected_interval
def get_count_at_index(self, index):
if index >= self.counts_len:
raise IndexError()
# some decoded (read-only) histograms may have truncated
# counts arrays, we return zero for any index that is passed the array
if index >= self.encoder.payload.counts_len:
return 0
return self.counts[index]
def get_count_at_sub_bucket(self, bucket_index, sub_bucket_index):
# Calculate the index for the first entry in the bucket:
# (The following is the equivalent of ((bucket_index + 1) * subBucketHalfCount) )
bucket_base_index = (bucket_index + 1) << self.sub_bucket_half_count_magnitude
# Calculate the offset in the bucket:
offset_in_bucket = sub_bucket_index - self.sub_bucket_half_count
# The following is the equivalent of
# (sub_bucket_index - subBucketHalfCount) + bucketBaseIndex
counts_index = bucket_base_index + offset_in_bucket
return self.counts[counts_index]
def get_value_from_sub_bucket(self, bucket_index, sub_bucket_index):
return sub_bucket_index << (bucket_index + self.unit_magnitude)
def get_value_from_index(self, index):
bucket_index = (index >> self.sub_bucket_half_count_magnitude) - 1
sub_bucket_index = (index & (self.sub_bucket_half_count - 1)) + \
self.sub_bucket_half_count
if bucket_index < 0:
sub_bucket_index -= self.sub_bucket_half_count
bucket_index = 0
return self.get_value_from_sub_bucket(bucket_index, sub_bucket_index)
def get_lowest_equivalent_value(self, value):
bucket_index = self._get_bucket_index(value)
sub_bucket_index = self._get_sub_bucket_index(value, bucket_index)
lowest_equivalent_value = self.get_value_from_sub_bucket(bucket_index,
sub_bucket_index)
return lowest_equivalent_value
def get_highest_equivalent_value(self, value):
bucket_index = self._get_bucket_index(value)
sub_bucket_index = self._get_sub_bucket_index(value, bucket_index)
lowest_equivalent_value = self.get_value_from_sub_bucket(bucket_index,
sub_bucket_index)
if sub_bucket_index >= self.sub_bucket_count:
bucket_index += 1
size_of_equivalent_value_range = 1 << (self.unit_magnitude + bucket_index)
next_non_equivalent_value = lowest_equivalent_value + size_of_equivalent_value_range
return next_non_equivalent_value - 1
def get_target_count_at_percentile(self, percentile):
requested_percentile = min(percentile, 100.0)
count_at_percentile = int(((requested_percentile * self.total_count / 100)) + 0.5)
return max(count_at_percentile, 1)
def get_value_at_percentile(self, percentile):
'''Get the value for a given percentile
Args:
percentile: a float in [0.0..100.0]
Returns:
the value for the given percentile
'''
count_at_percentile = self.get_target_count_at_percentile(percentile)
total = 0
for index in range(self.counts_len):
total += self.get_count_at_index(index)
if total >= count_at_percentile:
value_at_index = self.get_value_from_index(index)
if percentile:
return self.get_highest_equivalent_value(value_at_index)
return self.get_lowest_equivalent_value(value_at_index)
return 0
def get_percentile_to_value_dict(self, percentile_list):
'''A faster alternative to query values for a list of percentiles.
Args:
percentile_list: a list of percentiles in any order, dups will be ignored
each element in the list must be a float value in [0.0 .. 100.0]
Returns:
a dict of percentile values indexed by the percentile
'''
result = {}
total = 0
percentile_list_index = 0
count_at_percentile = 0
# remove dups and sort
percentile_list = list(set(percentile_list))
percentile_list.sort()
for index in range(self.counts_len):
total += self.get_count_at_index(index)
while True:
# recalculate target based on next requested percentile
if not count_at_percentile:
if percentile_list_index == len(percentile_list):
return result
percentile = percentile_list[percentile_list_index]
percentile_list_index += 1
if percentile > 100:
return result
count_at_percentile = self.get_target_count_at_percentile(percentile)
if total >= count_at_percentile:
value_at_index = self.get_value_from_index(index)
if percentile:
result[percentile] = self.get_highest_equivalent_value(value_at_index)
else:
result[percentile] = self.get_lowest_equivalent_value(value_at_index)
count_at_percentile = 0
else:
break
return result
def get_total_count(self):
return self.total_count
def get_count_at_value(self, value):
counts_index = self._counts_index_for(value)
return self.counts[counts_index]
def values_are_equivalent(self, val1, val2):
'''Check whether 2 values are equivalent (meaning they
are in the same bucket/range)
Returns:
true if the 2 values are equivalent
'''
return self.get_lowest_equivalent_value(val1) == self.get_lowest_equivalent_value(val2)
def get_max_value(self):
if self.max_value == 0:
return 0
return self.get_highest_equivalent_value(self.max_value)
def get_min_value(self):
if self.counts[0] > 0 or self.total_count == 0:
return 0
if sys.maxsize == self.min_value:
return sys.maxsize
return self.get_lowest_equivalent_value(self.min_value)
def _hdr_size_of_equiv_value_range(self, value):
bucket_index = self._get_bucket_index(value)
sub_bucket_index = self._get_sub_bucket_index(value, bucket_index)
if sub_bucket_index >= self.sub_bucket_count:
bucket_index += 1
return 1 << (self.unit_magnitude + bucket_index)
def _hdr_median_equiv_value(self, value):
return self.get_lowest_equivalent_value(value) + \
(self._hdr_size_of_equiv_value_range(value) >> 1)
def get_mean_value(self):
if not self.total_count:
return 0.0
total = 0
itr = self.get_recorded_iterator()
for item in itr:
total += itr.count_at_this_value * self._hdr_median_equiv_value(item.value_iterated_to)
return float(total) / self.total_count
def get_stddev(self):
if not self.total_count:
return 0.0
mean = self.get_mean_value()
geometric_dev_total = 0.0
for item in self.get_recorded_iterator():
dev = (self._hdr_median_equiv_value(item.value_iterated_to) * 1.0) - mean
geometric_dev_total += (dev * dev) * item.count_added_in_this_iter_step
return math.sqrt(geometric_dev_total / self.total_count)
def reset(self):
'''Reset the histogram to a pristine state
'''
for index in range(self.counts_len):
self.counts[index] = 0
self.total_count = 0
self.min_value = sys.maxsize
self.max_value = 0
self.start_time_stamp_msec = sys.maxsize
self.end_time_stamp_msec = 0
def __iter__(self):
'''Returns the recorded iterator if iter(self) is called
'''
return RecordedIterator(self)
def get_all_values_iterator(self):
return AllValuesIterator(self)
def get_recorded_iterator(self):
return RecordedIterator(self)
def get_percentile_iterator(self, ticks_per_half_distance):
return PercentileIterator(self, ticks_per_half_distance)
def get_linear_iterator(self, value_units_per_bucket):
return LinearIterator(self, value_units_per_bucket)
def get_log_iterator(self, value_units_first_bucket, log_base):
return LogIterator(self, value_units_first_bucket, log_base)
def encode(self):
'''Encode this histogram
Return:
a string containing the base64 encoded compressed histogram (V1 format)
'''
return self.encoder.encode()
def adjust_internal_tacking_values(self,
min_non_zero_index,
max_index,
total_added):
'''Called during decoding and add to adjust the new min/max value and
total count
Args:
min_non_zero_index min nonzero index of all added counts (-1 if none)
max_index max index of all added counts (-1 if none)
'''
if max_index >= 0:
max_value = self.get_highest_equivalent_value(self.get_value_from_index(max_index))
self.max_value = max(self.max_value, max_value)
if min_non_zero_index >= 0:
min_value = self.get_value_from_index(min_non_zero_index)
self.min_value = min(self.min_value, min_value)
self.total_count += total_added
def set_internal_tacking_values(self,
min_non_zero_index,
max_index,
total_added):
'''Called during decoding and add to adjust the new min/max value and
total count
Args:
min_non_zero_index min nonzero index of all added counts (-1 if none)
max_index max index of all added counts (-1 if none)
'''
if max_index >= 0:
self.max_value = self.get_highest_equivalent_value(self.get_value_from_index(max_index))
if min_non_zero_index >= 0:
self.min_value = self.get_value_from_index(min_non_zero_index)
self.total_count = total_added
def get_counts_array_index(self, value):
'''Return the index in the counts array for a given value
'''
if value < 0:
raise ValueError("Histogram recorded value cannot be negative.")
bucket_index = self._get_bucket_index(value)
sub_bucket_index = self._get_sub_bucket_index(value, bucket_index)
# Calculate the index for the first entry in the bucket:
bucket_base_index = (bucket_index + 1) << self.sub_bucket_half_count_magnitude
# The following is the equivalent of ((bucket_index + 1) * sub_bucket_half_count)
# Calculate the offset in the bucket (can be negative for first bucket):
offset_in_bucket = sub_bucket_index - self.sub_bucket_half_count
# The following is the equivalent of
# ((sub_bucket_index - sub_bucket_half_count) + bucket_base_index
return bucket_base_index + offset_in_bucket
def get_start_time_stamp(self):
return self.start_time_stamp_msec
def set_start_time_stamp(self, time_stamp_msec):
'''Set the start time stamp value associated with this histogram to a given value.
Params:
time_stamp_msec the value to set the time stamp to,
[by convention] in msec since the epoch.
'''
self.start_time_stamp_msec = time_stamp_msec
def get_end_time_stamp(self):
return self.end_time_stamp_msec
def set_end_time_stamp(self, time_stamp_msec):
'''Set the end time stamp value associated with this histogram to a given value.
Params:
time_stamp_msec the value to set the time stamp to,
[by convention] in msec since the epoch.
'''
self.end_time_stamp_msec = time_stamp_msec
def add(self, other_hist):
highest_recordable_value = \
self.get_highest_equivalent_value(self.get_value_from_index(self.counts_len - 1))
if highest_recordable_value < other_hist.get_max_value():
raise IndexError("The other histogram includes values that do not fit %d < %d" %
(highest_recordable_value, other_hist.get_max_value()))
if (self.bucket_count == other_hist.bucket_count) and \
(self.sub_bucket_count == other_hist.sub_bucket_count) and \
(self.unit_magnitude == other_hist.unit_magnitude) and \
(self.word_size == other_hist.word_size):
# do an in-place addition of one array to another
self.encoder.add(other_hist.encoder)
self.total_count += other_hist.get_total_count()
self.max_value = max(self.max_value, other_hist.get_max_value())
self.min_value = min(self.get_min_value(), other_hist.get_min_value())
else:
# Arrays are not a direct match, so we can't just stream through and add them.
# Instead, go through the array and add each non-zero value found at it's proper value:
for index in range(other_hist.counts_len):
other_count = other_hist.get_count_at_index(index)
if other_count > 0:
self.record_value(other_hist.get_value_from_index(index), other_count)
self.start_time_stamp_msec = \
min(self.start_time_stamp_msec, other_hist.start_time_stamp_msec)
self.end_time_stamp_msec = \
max(self.end_time_stamp_msec, other_hist.end_time_stamp_msec)
def decode_and_add(self, encoded_histogram):
'''Decode an encoded histogram and add it to this histogram
Args:
encoded_histogram (string) an encoded histogram
following the V1 format, such as one returned by the encode() method
Exception:
TypeError in case of base64 decode error
HdrCookieException:
the main header has an invalid cookie
the compressed payload header has an invalid cookie
HdrLengthException:
the decompressed size is too small for the HdrPayload structure
or is not aligned or is too large for the passed payload class
zlib.error:
in case of zlib decompression error
'''
other_hist = HdrHistogram.decode(encoded_histogram, self.b64_wrap)
self.add(other_hist)
@staticmethod
def decode(encoded_histogram, b64_wrap=True):
'''Decode an encoded histogram and return a new histogram instance that
has been initialized with the decoded content
Return:
a new histogram instance representing the decoded content
Exception:
TypeError in case of base64 decode error
HdrCookieException:
the main header has an invalid cookie
the compressed payload header has an invalid cookie
HdrLengthException:
the decompressed size is too small for the HdrPayload structure
or is not aligned or is too large for the passed payload class
zlib.error:
in case of zlib decompression error
'''
hdr_payload = HdrHistogramEncoder.decode(encoded_histogram, b64_wrap)
payload = hdr_payload.payload
histogram = HdrHistogram(payload.lowest_trackable_value,
payload.highest_trackable_value,
payload.significant_figures,
hdr_payload=hdr_payload)
return histogram
def get_word_size(self):
return self.word_size
def output_percentile_distribution(self,
out_file,
output_value_unit_scaling_ratio,
ticks_per_half_distance=5):
out_file.write(b'%12s %14s %10s %14s\n\n' %
(b'Value', b'Percentile', b'TotalCount', b'1/(1-Percentile)'))
percentile_format = '%12.{}f %2.12f %10d %14.2f\n'.format(self.significant_figures)
last_line_percentile_format = '%12.{}f %2.12f %10d\n'.format(self.significant_figures)
for iter_value in self.get_percentile_iterator(ticks_per_half_distance):
value = iter_value.value_iterated_to / output_value_unit_scaling_ratio
percentile = iter_value.percentile_level_iterated_to / 100
total_count = iter_value.total_count_to_this_value
if iter_value.percentile_level_iterated_to != 100:
other = 1 / (1 - iter_value.percentile_level_iterated_to / 100)
out_file.write(percentile_format.encode() % (value, percentile,
total_count, other))
else:
out_file.write(last_line_percentile_format.encode() % (value,
percentile,
total_count))
mean = self.get_mean_value() / output_value_unit_scaling_ratio
stddev = self.get_stddev()
out_file.write('#[Mean = %12.{0}f, StdDeviation = %12.{0}f]\n'.
format(self.significant_figures).encode() % (mean, stddev))
max = self.get_max_value() / output_value_unit_scaling_ratio
total = self.get_total_count()
out_file.write('#[Max = %12.{0}f, TotalCount = %12.{0}f]\n'.format(
self.significant_figures).encode() % (max, total))
out_file.write(b'#[Buckets = %12d, SubBuckets = %12d]\n' % (
self.bucket_count, self.sub_bucket_count))