p99-ruby 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,666 @@
1
+
2
+ # ######################################################################## #
3
+ # File: p99/histogram/pure.rb
4
+ #
5
+ # Purpose: Pure-Ruby Histogram implementation for p99.Ruby
6
+ #
7
+ # Created: 4th August 2026
8
+ # Updated: 30th August 2026
9
+ #
10
+ # Home: http://github.com/synesissoftware/p99.Ruby
11
+ #
12
+ # Author: Matthew Wilson
13
+ #
14
+ # Copyright (c) 2026, Matthew Wilson and Synesis Information Systems
15
+ # All rights reserved.
16
+ #
17
+ # Redistribution and use in source and binary forms, with or without
18
+ # modification, are permitted provided that the following conditions are
19
+ # met:
20
+ #
21
+ # * Redistributions of source code must retain the above copyright
22
+ # notice, this list of conditions and the following disclaimer.
23
+ #
24
+ # * Redistributions in binary form must reproduce the above copyright
25
+ # notice, this list of conditions and the following disclaimer in the
26
+ # documentation and/or other materials provided with the distribution.
27
+ #
28
+ # * Neither the names of the copyright holder nor the names of its
29
+ # contributors may be used to endorse or promote products derived from
30
+ # this software without specific prior written permission.
31
+ #
32
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
33
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
34
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
36
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
+ #
44
+ # ######################################################################## #
45
+
46
+
47
+ =begin
48
+ =end
49
+
50
+ module P99
51
+
52
+ # Number of logarithmic buckets in a {Histogram}.
53
+ BUCKET_COUNT = 64
54
+
55
+ # Maximum value representable as an unsigned 64-bit integer.
56
+ UINT64_MAX = (1 << 64) - 1
57
+
58
+ # Calculates the bucket index for a duration in nanoseconds.
59
+ #
60
+ # @param time_in_ns [Integer];
61
+ #
62
+ # @return [Integer]
63
+ def self.bucket_index(time_in_ns)
64
+
65
+ time_in_ns = Integer(time_in_ns)
66
+
67
+ return 0 if time_in_ns <= 1
68
+
69
+ floor_log2_u64_(time_in_ns)
70
+ end
71
+
72
+ # Attempts to obtain the inclusive nanosecond range for +index+.
73
+ #
74
+ # @param index [Integer];
75
+ #
76
+ # @return [Array(Integer, Integer), nil] +[lower, upper]+, or +nil+ if
77
+ # +index+ is out of range
78
+ def self.bucket_range(index)
79
+
80
+ index = Integer(index)
81
+
82
+ return nil if index < 0 || index >= BUCKET_COUNT
83
+
84
+ bucket_range_(index)
85
+ end
86
+
87
+ # Low-cost performance percentile histogram (pure-Ruby backend).
88
+ #
89
+ # Tracks event durations with nanosecond precision across 64 logarithmic
90
+ # power-of-two buckets and approximates percentiles via linear
91
+ # interpolation within buckets.
92
+ class Histogram
93
+
94
+ # Constructs an empty histogram.
95
+ #
96
+ # @return [Histogram] a newly constructed histogram
97
+ def initialize
98
+
99
+ clear
100
+ end
101
+
102
+ # Resets the histogram to the equivalent of a newly constructed
103
+ # instance.
104
+ #
105
+ # @return [Histogram] +self+
106
+ def clear
107
+
108
+ @has_overflowed = false
109
+ @event_count = 0
110
+ @event_time_total = 0
111
+ @min_event_time = 0
112
+ @max_event_time = 0
113
+ @buckets = Array.new(BUCKET_COUNT, 0)
114
+
115
+ self
116
+ end
117
+
118
+ # Records an event duration in nanoseconds.
119
+ #
120
+ # @param time_in_ns [Integer];
121
+ #
122
+ # @return [Boolean] +true+ on success; +false+ if overflow has already
123
+ # occurred or the running total would overflow
124
+ def push_event_time_ns(time_in_ns)
125
+
126
+ time_in_ns = coerce_u64_(time_in_ns)
127
+ return false if time_in_ns.nil?
128
+
129
+ bucket_index = P99.bucket_index(time_in_ns)
130
+
131
+ return false unless try_add_ns_to_total_and_update_minmax_(time_in_ns)
132
+
133
+ @event_count += 1
134
+ @buckets[bucket_index] += 1
135
+
136
+ true
137
+ end
138
+
139
+ # Records an event duration in microseconds.
140
+ #
141
+ # @param time_in_us [Integer];
142
+ #
143
+ # @return [Boolean]
144
+ def push_event_time_us(time_in_us)
145
+
146
+ time_in_us = coerce_u64_(time_in_us)
147
+ return false if time_in_us.nil?
148
+
149
+ if time_in_us > UINT64_MAX / 1000
150
+
151
+ @has_overflowed = true
152
+
153
+ return false
154
+ end
155
+
156
+ push_event_time_ns(time_in_us * 1000)
157
+ end
158
+
159
+ # Records an event duration in milliseconds.
160
+ #
161
+ # @param time_in_ms [Integer]
162
+ # @return [Boolean]
163
+ def push_event_time_ms(time_in_ms)
164
+
165
+ time_in_ms = coerce_u64_(time_in_ms)
166
+ return false if time_in_ms.nil?
167
+
168
+ if time_in_ms > UINT64_MAX / 1_000_000
169
+
170
+ @has_overflowed = true
171
+
172
+ return false
173
+ end
174
+
175
+ push_event_time_ns(time_in_ms * 1_000_000)
176
+ end
177
+
178
+ # Records an event duration in seconds.
179
+ #
180
+ # @param time_in_s [Integer];
181
+ #
182
+ # @return [Boolean]
183
+ def push_event_time_s(time_in_s)
184
+
185
+ time_in_s = coerce_u64_(time_in_s)
186
+ return false if time_in_s.nil?
187
+
188
+ if time_in_s > UINT64_MAX / 1_000_000_000
189
+
190
+ @has_overflowed = true
191
+
192
+ return false
193
+ end
194
+
195
+ push_event_time_ns(time_in_s * 1_000_000_000)
196
+ end
197
+
198
+ # Number of recorded events.
199
+ #
200
+ # @return [Integer]
201
+ attr_reader :event_count
202
+
203
+ # Whether an arithmetic overflow has occurred.
204
+ #
205
+ # @return [Boolean]
206
+ def has_overflowed?
207
+
208
+ @has_overflowed
209
+ end
210
+
211
+ # Total event time in nanoseconds, or +nil+ if overflow has occurred.
212
+ #
213
+ # @return [Integer, nil]
214
+ def event_time_total
215
+
216
+ return nil if @has_overflowed
217
+
218
+ event_time_total_raw
219
+ end
220
+
221
+ # Total event time in nanoseconds regardless of overflow status.
222
+ #
223
+ # @return [Integer]
224
+ def event_time_total_raw
225
+
226
+ @event_time_total
227
+ end
228
+
229
+ # Minimum observed event time in nanoseconds, or +nil+ if empty.
230
+ #
231
+ # @return [Integer, nil]
232
+ def min_event_time
233
+
234
+ return nil if @event_count == 0
235
+
236
+ @min_event_time
237
+ end
238
+
239
+ # Maximum observed event time in nanoseconds, or +nil+ if empty.
240
+ #
241
+ # @return [Integer, nil]
242
+ def max_event_time
243
+
244
+ return nil if @event_count == 0
245
+
246
+ @max_event_time
247
+ end
248
+
249
+ # Count of events in bucket +index+, or +nil+ if out of range.
250
+ #
251
+ # @param index [Integer];
252
+ #
253
+ # @return [Integer, nil]
254
+ def bucket_value(index)
255
+
256
+ index = Integer(index)
257
+
258
+ return nil if index < 0 || index >= BUCKET_COUNT
259
+
260
+ @buckets[index]
261
+ end
262
+
263
+ # Copy of all bucket counts.
264
+ #
265
+ # @return [Array<Integer>]
266
+ def buckets
267
+
268
+ @buckets.dup
269
+ end
270
+
271
+ # Approximated duration in nanoseconds at +percentile+.
272
+ #
273
+ # +percentile+ is clamped to +[0.0, 100.0]+.
274
+ #
275
+ # @param percentile [Numeric];
276
+ #
277
+ # @return [Integer, nil] +nil+ if the histogram is empty
278
+ def value_at_percentile(percentile)
279
+
280
+ return nil if @event_count == 0
281
+
282
+ p = clamp_percentile_(percentile)
283
+
284
+ return min_event_time if p <= 0.0
285
+ return max_event_time if p >= 100.0
286
+
287
+ target_rank = @event_count.to_f * (p / 100.0)
288
+ accumulated = 0
289
+
290
+ BUCKET_COUNT.times do |i|
291
+
292
+ count = @buckets[i]
293
+
294
+ next if count == 0
295
+
296
+ prev_accumulated = accumulated
297
+ accumulated += count
298
+
299
+ if accumulated.to_f >= target_rank
300
+
301
+ return value_at_percentile_in_bucket_(
302
+ i, count, prev_accumulated, target_rank
303
+ )
304
+ end
305
+ end
306
+
307
+ max_event_time
308
+ end
309
+
310
+ # Approximated duration at p50.
311
+ #
312
+ # @return [Integer, nil]
313
+ def value_at_p50
314
+
315
+ value_at_target_rank_(u64_mul_div_(@event_count, 1, 2))
316
+ end
317
+
318
+ # Approximated duration at p75.
319
+ #
320
+ # @return [Integer, nil]
321
+ def value_at_p75
322
+
323
+ value_at_target_rank_(u64_mul_div_(@event_count, 3, 4))
324
+ end
325
+
326
+ # Approximated duration at p90.
327
+ #
328
+ # @return [Integer, nil]
329
+ def value_at_p90
330
+
331
+ value_at_target_rank_(u64_mul_div_(@event_count, 90, 100))
332
+ end
333
+
334
+ # Approximated duration at p95.
335
+ #
336
+ # @return [Integer, nil]
337
+ def value_at_p95
338
+
339
+ value_at_target_rank_(u64_mul_div_(@event_count, 95, 100))
340
+ end
341
+
342
+ # Approximated duration at p99.
343
+ #
344
+ # @return [Integer, nil]
345
+ def value_at_p99
346
+
347
+ value_at_target_rank_(u64_mul_div_(@event_count, 99, 100))
348
+ end
349
+
350
+ # Approximated duration at p99.5.
351
+ #
352
+ # @return [Integer, nil]
353
+ def value_at_p99_5
354
+
355
+ value_at_target_rank_(u64_mul_div_(@event_count, 995, 1000))
356
+ end
357
+
358
+ # Approximated duration at p99.9.
359
+ #
360
+ # @return [Integer, nil]
361
+ def value_at_p99_9
362
+
363
+ value_at_target_rank_(u64_mul_div_(@event_count, 999, 1000))
364
+ end
365
+
366
+ # Approximated duration at p99.99.
367
+ #
368
+ # @return [Integer, nil]
369
+ def value_at_p99_99
370
+
371
+ value_at_target_rank_(u64_mul_div_(@event_count, 9999, 10000))
372
+ end
373
+
374
+ # Approximated duration at p99.999.
375
+ #
376
+ # @return [Integer, nil]
377
+ def value_at_p99_999
378
+
379
+ value_at_target_rank_(u64_mul_div_(@event_count, 99999, 100000))
380
+ end
381
+
382
+ # Approximated duration at p99.9999.
383
+ #
384
+ # @return [Integer, nil]
385
+ def value_at_p99_999_9
386
+
387
+ value_at_target_rank_(u64_mul_div_(@event_count, 999999, 1000000))
388
+ end
389
+
390
+ # Approximated durations at multiple floating-point percentiles.
391
+ #
392
+ # @param levels [Array<Numeric>];
393
+ #
394
+ # @return [Array<Array(Numeric, Integer)>, nil] +nil+ if empty; otherwise
395
+ # an array of +[level, value]+ pairs
396
+ def values_at_percentiles(levels)
397
+
398
+ return nil if @event_count == 0
399
+
400
+ levels = Array(levels)
401
+
402
+ return [] if levels.empty?
403
+
404
+ results = []
405
+
406
+ levels.each do |level|
407
+
408
+ value = value_at_percentile(level)
409
+
410
+ return nil if value.nil?
411
+
412
+ results << [ level, value ]
413
+ end
414
+
415
+ results
416
+ end
417
+
418
+ # Approximated durations at the fixed percentile set.
419
+ #
420
+ # @return [Hash{String=>Integer}, nil] keys +"p50"+ … +"p99.9999"+, or
421
+ # +nil+ if empty
422
+ def fixed_percentiles
423
+
424
+ return nil if @event_count == 0
425
+
426
+ getters = [
427
+ [ 'p50', :value_at_p50 ],
428
+ [ 'p75', :value_at_p75 ],
429
+ [ 'p90', :value_at_p90 ],
430
+ [ 'p95', :value_at_p95 ],
431
+ [ 'p99', :value_at_p99 ],
432
+ [ 'p99.5', :value_at_p99_5 ],
433
+ [ 'p99.9', :value_at_p99_9 ],
434
+ [ 'p99.99', :value_at_p99_99 ],
435
+ [ 'p99.999', :value_at_p99_999 ],
436
+ [ 'p99.9999', :value_at_p99_999_9 ],
437
+ ]
438
+
439
+ results = {}
440
+
441
+ getters.each do |key, method_name|
442
+
443
+ value = send(method_name)
444
+
445
+ return nil if value.nil?
446
+
447
+ results[key] = value
448
+ end
449
+
450
+ results
451
+ end
452
+
453
+ private
454
+
455
+ def coerce_u64_(value)
456
+
457
+ value = Integer(value)
458
+
459
+ if value < 0 || value > UINT64_MAX
460
+
461
+ @has_overflowed = true
462
+
463
+ return nil
464
+ end
465
+
466
+ value
467
+ end
468
+
469
+ def try_add_ns_to_total_and_update_minmax_(time_in_ns)
470
+
471
+ return false if @has_overflowed
472
+
473
+ if time_in_ns > UINT64_MAX - @event_time_total
474
+
475
+ @has_overflowed = true
476
+
477
+ return false
478
+ end
479
+
480
+ @event_time_total += time_in_ns
481
+
482
+ if @event_count == 0
483
+
484
+ @min_event_time = time_in_ns
485
+ @max_event_time = time_in_ns
486
+ else
487
+
488
+ @min_event_time = time_in_ns if time_in_ns < @min_event_time
489
+ @max_event_time = time_in_ns if time_in_ns > @max_event_time
490
+ end
491
+
492
+ true
493
+ end
494
+
495
+ def value_at_target_rank_(target_rank)
496
+
497
+ return nil if @event_count == 0
498
+
499
+ accumulated = 0
500
+
501
+ BUCKET_COUNT.times do |i|
502
+
503
+ count = @buckets[i]
504
+
505
+ next if count == 0
506
+
507
+ prev_accumulated = accumulated
508
+ accumulated += count
509
+
510
+ if accumulated >= target_rank
511
+
512
+ return value_at_target_rank_in_bucket_(
513
+ i, count, prev_accumulated, target_rank
514
+ )
515
+ end
516
+ end
517
+
518
+ @max_event_time
519
+ end
520
+
521
+ def value_at_target_rank_in_bucket_(
522
+ bucket_index,
523
+ count,
524
+ prev_accumulated,
525
+ target_rank
526
+ )
527
+
528
+ range = P99.bucket_range(bucket_index)
529
+
530
+ if range.nil?
531
+
532
+ lower = 0
533
+ upper = UINT64_MAX
534
+ else
535
+
536
+ lower, upper = range
537
+ end
538
+
539
+ if target_rank <= prev_accumulated
540
+
541
+ interpolated = lower
542
+ else
543
+
544
+ target_offset = target_rank - prev_accumulated
545
+
546
+ if bucket_index == BUCKET_COUNT - 1
547
+
548
+ range_width = UINT64_MAX - lower
549
+ else
550
+
551
+ range_width = upper - lower
552
+ end
553
+
554
+ interpolated = lower + u64_mul_div_(range_width, target_offset, count)
555
+ end
556
+
557
+ clamp_to_minmax_(interpolated)
558
+ end
559
+
560
+ def value_at_percentile_in_bucket_(
561
+ bucket_index,
562
+ count,
563
+ prev_accumulated,
564
+ target_rank
565
+ )
566
+
567
+ range = P99.bucket_range(bucket_index)
568
+
569
+ if range.nil?
570
+
571
+ lower = 0
572
+ upper = UINT64_MAX
573
+ else
574
+
575
+ lower, upper = range
576
+ end
577
+
578
+ target_offset = target_rank - prev_accumulated.to_f
579
+
580
+ if bucket_index == BUCKET_COUNT - 1
581
+
582
+ range_width = (UINT64_MAX - lower).to_f
583
+ else
584
+
585
+ range_width = (upper - lower).to_f
586
+ end
587
+
588
+ fraction = target_offset / count.to_f
589
+ interpolated = lower.to_f + (range_width * fraction)
590
+
591
+ clamp_to_minmax_(llround_(interpolated))
592
+ end
593
+
594
+ def clamp_to_minmax_(value)
595
+
596
+ value = @min_event_time if value < @min_event_time
597
+ value = @max_event_time if value > @max_event_time
598
+
599
+ value
600
+ end
601
+
602
+ def clamp_percentile_(percentile)
603
+
604
+ percentile = Float(percentile)
605
+
606
+ return 0.0 if percentile < 0.0
607
+ return 100.0 if percentile > 100.0
608
+
609
+ percentile
610
+ end
611
+
612
+ def u64_mul_div_(multiplicand, multiplier, divisor)
613
+
614
+ (multiplicand * multiplier) / divisor
615
+ end
616
+
617
+ def llround_(value)
618
+
619
+ if value >= 0.0
620
+
621
+ (value + 0.5).floor
622
+ else
623
+
624
+ (value - 0.5).ceil
625
+ end
626
+ end
627
+ end # class Histogram
628
+
629
+ class << self
630
+
631
+ private
632
+
633
+ def floor_log2_u64_(value)
634
+
635
+ return value.bit_length - 1 if value.respond_to?(:bit_length)
636
+
637
+ result = -1
638
+
639
+ while value > 0
640
+
641
+ value >>= 1
642
+ result += 1
643
+ end
644
+
645
+ result
646
+ end
647
+
648
+ def bucket_range_(index)
649
+
650
+ return [ 0, 1 ] if index == 0
651
+
652
+ lower = 1 << index
653
+
654
+ if index == BUCKET_COUNT - 1
655
+
656
+ [ lower, UINT64_MAX ]
657
+ else
658
+
659
+ [ lower, (1 << (index + 1)) - 1 ]
660
+ end
661
+ end
662
+ end
663
+ end # module P99
664
+
665
+
666
+ # ############################## end of file ############################# #