benchmark 0.8.0

Nanosecond-precision benchmarking for dev, testing, and production. Zero-overhead core timing when disabled; optional std-powered collectors and zero-dependency metrics (Watch/Timer) for real service observability.
Documentation
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
//! # High-Performance Histogram
//!
//! A zero-dependency, thread-safe, high-performance histogram implementation optimized
//! for real-time applications requiring nanosecond precision timing measurements.
//!
//! ## Features
//!
//! - **Zero Dependencies**: Pure Rust implementation
//! - **Thread-Safe**: Lock-free atomic operations for maximum concurrency
//! - **High Performance**: O(1) record operations, optimized for CPU cache efficiency
//! - **Memory Efficient**: Fixed ~5KB footprint, no heap allocations after initialization
//! - **Cross-Platform**: Works on all Rust-supported platforms
//! - **Secure**: Overflow protection and comprehensive input validation
//!
//! ## Example
//!
//! ```rust
//! use std::time::Duration;
//! # use benchmark::histogram::Histogram;
//!
//! let histogram = Histogram::new();
//!
//! // Record timing measurements
//! histogram.record(1000); // 1 microsecond in nanoseconds
//! histogram.record_duration(Duration::from_millis(1));
//!
//! // Get statistics
//! println!("Median: {:?}", histogram.median());
//! println!("99th percentile: {:?}", histogram.percentile(0.99));
//! println!("Mean: {:?}", histogram.mean());
//! ```

#[cfg(not(feature = "hdr"))]
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

/// Maximum number of linear buckets for high-precision measurements (0-1023ns)
#[cfg(not(feature = "hdr"))]
const LINEAR_BUCKETS: usize = 1024;

/// Maximum number of logarithmic buckets (covers up to 2^63 nanoseconds)
#[cfg(not(feature = "hdr"))]
const LOG_BUCKETS: usize = 64;

/// Memory ordering for atomic operations - optimized for performance while ensuring correctness
#[cfg(not(feature = "hdr"))]
const MEMORY_ORDER: Ordering = Ordering::Relaxed;

/// A high-performance, thread-safe histogram optimized for timing measurements.
///
/// Uses a hybrid bucketing strategy:
/// - Linear buckets (0-1023ns) for sub-microsecond precision
/// - Logarithmic buckets (1024ns+) for efficient wide-range coverage
///
/// All operations are lock-free and thread-safe using atomic operations.
///
/// # Memory Layout
///
/// - Linear buckets: 1024 × 8 bytes = 8KB
/// - Logarithmic buckets: 64 × 8 bytes = 512 bytes  
/// - Statistics: 4 × 8 bytes = 32 bytes
/// - **Total: ~8.5KB fixed memory footprint**
///
/// # Performance Characteristics
///
/// - **Record**: O(1) - constant time, ~2-5ns overhead
/// - **Percentile**: O(1) - constant time lookup with interpolation
/// - **Statistics**: O(1) - direct atomic reads
/// - **Thread contention**: Minimal due to lock-free design
#[cfg(not(feature = "hdr"))]
#[derive(Debug)]
pub struct FastHistogram {
    /// High-precision linear buckets for 0-1023 nanoseconds
    /// Each bucket represents exactly 1 nanosecond
    linear_buckets: [AtomicU64; LINEAR_BUCKETS],

    /// Logarithmic buckets for values >= 1024 nanoseconds
    /// Bucket i covers range [2^i, 2^(i+1))
    log_buckets: [AtomicU64; LOG_BUCKETS],

    /// Minimum recorded value (nanoseconds)
    min_value: AtomicU64,

    /// Maximum recorded value (nanoseconds)  
    max_value: AtomicU64,

    /// Total count of recorded values
    total_count: AtomicU64,

    /// Sum of all recorded values (with overflow protection)
    sum: AtomicU64,
}

#[cfg(not(feature = "hdr"))]
impl FastHistogram {
    /// Creates a new empty histogram.
    ///
    /// # Performance
    ///
    /// This operation initializes ~1088 atomic values. While not free, it's a one-time
    /// cost typically taking <1μs on modern hardware.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use benchmark::histogram::Histogram;
    /// let histogram = Histogram::new();
    /// assert!(histogram.is_empty());
    /// ```
    pub fn new() -> Self {
        Self {
            linear_buckets: std::array::from_fn(|_| AtomicU64::new(0)),
            log_buckets: std::array::from_fn(|_| AtomicU64::new(0)),
            min_value: AtomicU64::new(u64::MAX),
            max_value: AtomicU64::new(0),
            total_count: AtomicU64::new(0),
            sum: AtomicU64::new(0),
        }
    }

    /// Records a timing value in nanoseconds.
    ///
    /// This is the core hot-path method optimized for maximum performance.
    /// Uses lock-free atomic operations for thread safety.
    ///
    /// # Performance
    ///
    /// - **Latency**: ~2-5ns on modern `x86_64` hardware
    /// - **Throughput**: >200M records/second/core
    /// - **Scalability**: Linear with core count (lock-free)
    ///
    /// # Arguments
    ///
    /// * `value_ns` - The value to record in nanoseconds
    ///
    /// # Overflow Behavior
    ///
    /// - Values >= 2^63 nanoseconds (~292 years) are clamped to the highest bucket
    /// - Sum overflow is handled via saturation (won't panic)
    /// - Count overflow is extremely unlikely (2^64 samples) but handled gracefully
    ///
    /// # Example
    ///
    /// ```rust
    /// # use benchmark::histogram::Histogram;
    /// let histogram = Histogram::new();
    /// histogram.record(1000); // Record 1 microsecond
    /// histogram.record(500);  // Record 500 nanoseconds
    /// ```
    #[inline]
    pub fn record(&self, value_ns: u64) {
        // Update statistics atomically
        self.update_min(value_ns);
        self.update_max(value_ns);
        self.total_count.fetch_add(1, MEMORY_ORDER);
        self.sum
            .fetch_add(value_ns.min(u64::MAX - 1000), MEMORY_ORDER); // Overflow protection

        // Record in appropriate bucket
        if value_ns < LINEAR_BUCKETS as u64 {
            // High-precision linear bucket
            #[allow(clippy::cast_possible_truncation)]
            {
                self.linear_buckets[value_ns as usize].fetch_add(1, MEMORY_ORDER);
            }
        } else {
            // Logarithmic bucket - find the highest bit position
            let bucket_index = Self::log_bucket_index(value_ns);
            if bucket_index < LOG_BUCKETS {
                self.log_buckets[bucket_index].fetch_add(1, MEMORY_ORDER);
            }
        }
    }

    /// Records a Duration value.
    ///
    /// Convenience method that converts Duration to nanoseconds and records it.
    /// Handles Duration overflow gracefully by clamping to `u64::MAX`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::time::Duration;
    /// # use benchmark::histogram::Histogram;
    ///
    /// let histogram = Histogram::new();
    /// histogram.record_duration(Duration::from_millis(1));
    /// histogram.record_duration(Duration::from_nanos(500));
    /// ```
    #[inline]
    pub fn record_duration(&self, duration: Duration) {
        // Handle potential overflow from Duration::as_nanos() (returns u128)
        let nanos = duration.as_nanos();
        let clamped_nanos = if nanos > u128::from(u64::MAX) {
            u64::MAX
        } else {
            // safe due to the guard above; fall back to MAX if ever exceeded
            u64::try_from(nanos).unwrap_or(u64::MAX)
        };
        self.record(clamped_nanos);
    }

    /// Returns the minimum recorded value in nanoseconds.
    ///
    /// # Returns
    ///
    /// - `Some(min)` if values have been recorded
    /// - `None` if histogram is empty
    ///
    /// # Performance
    ///
    /// O(1) - single atomic read
    #[inline]
    pub fn min(&self) -> Option<u64> {
        let min = self.min_value.load(MEMORY_ORDER);
        if min == u64::MAX {
            None
        } else {
            Some(min)
        }
    }

    /// Returns the maximum recorded value in nanoseconds.
    ///
    /// # Returns
    ///
    /// - `Some(max)` if values have been recorded  
    /// - `None` if histogram is empty
    ///
    /// # Performance
    ///
    /// O(1) - single atomic read
    #[inline]
    pub fn max(&self) -> Option<u64> {
        let count = self.total_count.load(MEMORY_ORDER);
        if count == 0 {
            None
        } else {
            Some(self.max_value.load(MEMORY_ORDER))
        }
    }

    /// Returns the arithmetic mean of recorded values.
    ///
    /// # Returns
    ///
    /// - `Some(mean)` if values have been recorded
    /// - `None` if histogram is empty
    ///
    /// # Performance
    ///
    /// O(1) - two atomic reads and one division
    ///
    /// # Precision
    ///
    /// Returns f64 for maximum precision. For integer nanosecond precision,
    /// consider using `median()` instead.
    #[inline]
    pub fn mean(&self) -> Option<f64> {
        let count = self.total_count.load(MEMORY_ORDER);
        if count == 0 {
            None
        } else {
            let sum = self.sum.load(MEMORY_ORDER);
            #[allow(clippy::cast_precision_loss)]
            {
                Some(sum as f64 / count as f64)
            }
        }
    }

    /// Returns the total number of recorded values.
    ///
    /// # Performance
    ///
    /// O(1) - single atomic read
    #[inline]
    pub fn count(&self) -> u64 {
        self.total_count.load(MEMORY_ORDER)
    }

    /// Returns true if no values have been recorded.
    ///
    /// # Performance
    ///
    /// O(1) - single atomic read
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.total_count.load(MEMORY_ORDER) == 0
    }

    /// Returns the value at the specified percentile.
    ///
    /// This is the primary method for extracting distribution statistics.
    /// Uses linear interpolation within buckets for smooth, accurate results.
    ///
    /// # Arguments
    ///
    /// * `percentile` - Value between 0.0 and 1.0 (inclusive)
    ///   - 0.0 = minimum value
    ///   - 0.5 = median  
    ///   - 0.99 = 99th percentile
    ///   - 1.0 = maximum value
    ///
    /// # Returns
    ///
    /// - `Some(value)` - the interpolated value at the percentile
    /// - `None` - if histogram is empty or percentile is invalid
    ///
    /// # Performance
    ///
    /// O(1) - constant time regardless of data size due to fixed bucket count
    /// Typical latency: ~100-500ns
    ///
    /// # Accuracy
    ///
    /// - Linear buckets (0-1023ns): Exact to the nanosecond
    /// - Log buckets: ~1-3% error due to interpolation, decreasing with more samples
    ///
    /// # Example
    ///
    /// ```rust
    /// # use benchmark::histogram::Histogram;
    /// let histogram = Histogram::new();
    /// for i in 1..=100 {
    ///     histogram.record(i * 1000); // 1μs, 2μs, ..., 100μs
    /// }
    ///
    /// assert_eq!(histogram.percentile(0.0), histogram.min());   // Min
    /// // Median uses nearest-rank with interpolation; allow a small tolerance.
    /// let p50 = histogram.percentile(0.5).unwrap();
    /// assert!(p50 >= 49000 && p50 <= 51000, "p50={}", p50);
    /// assert_eq!(histogram.percentile(1.0), histogram.max()); // Max
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic. Internal uses of `unwrap()` are guarded by an
    /// early return when the histogram is empty (`total_count == 0`), ensuring
    /// that `min()` and `max()` return `Some` values before unwrapping.
    #[inline]
    pub fn percentile(&self, percentile: f64) -> Option<u64> {
        // Input validation
        let total_count = self.total_count.load(MEMORY_ORDER);
        if total_count == 0 {
            return None;
        }

        // Handle edge cases
        let p = percentile.clamp(0.0, 1.0);
        #[allow(clippy::float_cmp)]
        if p == 0.0 {
            return self.min();
        }
        #[allow(clippy::float_cmp)]
        if p == 1.0 {
            return self.max();
        }

        // Compute 1-based rank using the nearest-rank method
        // rank = ceil(p * n), with p in (0,1), so rank in [1, n-1]
        let target_count = {
            #[allow(
                clippy::cast_possible_truncation,
                clippy::cast_sign_loss,
                clippy::cast_precision_loss
            )]
            {
                (p * total_count as f64).ceil() as u64
            }
        };
        let mut current_count = 0u64;

        // Pre-compute observed range for clamping
        let min_v = self.min()?;
        let max_v = self.max()?;

        // Scan linear buckets (0-1023ns) for exact nanosecond precision
        for (value, bucket) in self.linear_buckets.iter().enumerate() {
            let count = bucket.load(MEMORY_ORDER);
            if count == 0 {
                continue;
            }

            current_count += count;
            if current_count >= target_count {
                let v = value as u64;
                return Some(v.clamp(min_v, max_v));
            }
        }

        // Scan logarithmic buckets with interpolation
        for (bucket_idx, bucket) in self.log_buckets.iter().enumerate() {
            let count = bucket.load(MEMORY_ORDER);
            if count == 0 {
                continue;
            }

            let bucket_start = Self::bucket_start(bucket_idx);
            let bucket_end = Self::bucket_end(bucket_idx);

            if current_count + count >= target_count {
                // Target percentile is within this bucket - interpolate
                let position_in_bucket = target_count.saturating_sub(current_count);
                let bucket_width = bucket_end.saturating_sub(bucket_start);

                if count > 0 && bucket_width > 0 {
                    // Use 1-based rank inside the bucket: offset should be (k-1)
                    // Compute with u128 to avoid intermediate overflow
                    let num = (u128::from(position_in_bucket.saturating_sub(1)))
                        * u128::from(bucket_width);
                    let den = u128::from(count);
                    let interpolated_offset = u64::try_from(num / den).unwrap_or(u64::MAX);
                    let v = bucket_start.saturating_add(interpolated_offset);
                    return Some(v.clamp(min_v, max_v));
                }
                return Some(bucket_start.clamp(min_v, max_v));
            }

            current_count += count;
        }

        // Fallback: return maximum
        self.max()
    }

    /// Returns the median value (50th percentile).
    ///
    /// Convenience method equivalent to `percentile(0.5)`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use benchmark::histogram::Histogram;
    /// let histogram = Histogram::new();
    /// histogram.record(100);
    /// histogram.record(200);  
    /// histogram.record(300);
    ///
    /// // Median uses nearest-rank: ceil(0.5 * 3) = 2nd element -> 200
    /// assert_eq!(histogram.median(), Some(200));
    /// ```
    #[inline]
    pub fn median(&self) -> Option<u64> {
        self.percentile(0.5)
    }

    /// Returns the median as a Duration.
    ///
    /// Convenience method for Duration-based APIs.
    #[inline]
    pub fn median_duration(&self) -> Option<Duration> {
        self.median().map(Duration::from_nanos)
    }

    /// Returns the percentile as a Duration.
    ///
    /// Convenience method for Duration-based APIs.
    #[inline]
    pub fn percentile_duration(&self, percentile: f64) -> Option<Duration> {
        self.percentile(percentile).map(Duration::from_nanos)
    }

    /// Returns multiple percentiles efficiently in a single pass.
    ///
    /// More efficient than calling `percentile()` multiple times when you need
    /// several percentiles, as it only scans the buckets once.
    ///
    /// # Arguments
    ///
    /// * `percentiles` - Slice of percentile values (0.0 to 1.0)
    ///
    /// # Returns
    ///
    /// Vector of `Option<u64>` values corresponding to input percentiles.
    /// Invalid percentiles return `None`.
    ///
    /// # Performance
    ///
    /// O(n + k) where n is bucket count (constant) and k is percentile count.
    /// Significantly faster than k separate `percentile()` calls.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use benchmark::histogram::Histogram;
    /// let histogram = Histogram::new();
    /// for i in 1..=1000 {
    ///     histogram.record(i);
    /// }
    ///
    /// let percentiles = histogram.percentiles(&[0.5, 0.95, 0.99, 0.999]);
    /// println!("P50: {:?}, P95: {:?}, P99: {:?}, P99.9: {:?}",
    ///          percentiles[0], percentiles[1], percentiles[2], percentiles[3]);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic. Internal unwraps on `min()`/`max()` are only
    /// reached after confirming the histogram is non-empty, ensuring those values exist.
    #[inline]
    pub fn percentiles(&self, percentiles: &[f64]) -> Vec<Option<u64>> {
        let total_count = self.total_count.load(MEMORY_ORDER);
        if total_count == 0 {
            return vec![None; percentiles.len()];
        }

        let mut results = vec![None; percentiles.len()];

        // Create sorted list of (index, target_count) for efficient processing
        let mut targets: Vec<(usize, u64)> = percentiles
            .iter()
            .enumerate()
            .map(|(i, &p_in)| {
                let p = p_in.clamp(0.0, 1.0);
                let target = if p == 0.0 {
                    0
                } else if (p - 1.0).abs() < f64::EPSILON {
                    total_count
                } else {
                    #[allow(
                        clippy::cast_possible_truncation,
                        clippy::cast_sign_loss,
                        clippy::cast_precision_loss
                    )]
                    {
                        (p * total_count as f64).ceil() as u64
                    }
                };
                (i, target)
            })
            .collect();

        // Sort by target count for single-pass processing
        targets.sort_by_key(|&(_, count)| count);

        let mut current_count = 0u64;
        let mut target_idx = 0;

        // Handle percentile 0.0 (minimum)
        while target_idx < targets.len() && targets[target_idx].1 == 0 {
            results[targets[target_idx].0] = self.min();
            target_idx += 1;
        }

        // Pre-compute observed range for clamping
        let min_v = self.min();
        let max_v = self.max();

        // Process linear buckets
        for (value, bucket) in self.linear_buckets.iter().enumerate() {
            let count = bucket.load(MEMORY_ORDER);
            if count == 0 || target_idx >= targets.len() {
                continue;
            }

            current_count += count;

            while target_idx < targets.len() && current_count >= targets[target_idx].1 {
                let v = value as u64;
                results[targets[target_idx].0] = Some(v.clamp(min_v.unwrap(), max_v.unwrap()));
                target_idx += 1;
            }
        }

        // Process logarithmic buckets
        for (bucket_idx, bucket) in self.log_buckets.iter().enumerate() {
            let count = bucket.load(MEMORY_ORDER);
            if count == 0 || target_idx >= targets.len() {
                continue;
            }

            let bucket_start = Self::bucket_start(bucket_idx);
            let bucket_end = Self::bucket_end(bucket_idx);

            while target_idx < targets.len() && current_count + count >= targets[target_idx].1 {
                let position_in_bucket = targets[target_idx].1.saturating_sub(current_count);
                let bucket_width = bucket_end.saturating_sub(bucket_start);

                let interpolated_value = if count > 0 && bucket_width > 0 {
                    // 1-based rank; use (k-1) for offset. Do math in u128 to avoid overflow.
                    let num = (u128::from(position_in_bucket.saturating_sub(1)))
                        * u128::from(bucket_width);
                    let den = u128::from(count);
                    let interpolated_offset = u64::try_from(num / den).unwrap_or(u64::MAX);
                    bucket_start.saturating_add(interpolated_offset)
                } else {
                    bucket_start
                };

                let v = interpolated_value.clamp(min_v.unwrap(), max_v.unwrap());
                results[targets[target_idx].0] = Some(v);
                target_idx += 1;
            }

            current_count += count;
        }

        // Handle percentile 1.0 (maximum) and any remaining
        while target_idx < targets.len() {
            results[targets[target_idx].0] = self.max();
            target_idx += 1;
        }

        // Ensure any input with p clamped to approximately 1.0 returns true max (not interpolated)
        for (i, &p_in) in percentiles.iter().enumerate() {
            let p = p_in.clamp(0.0, 1.0);
            if (p - 1.0).abs() < f64::EPSILON {
                results[i] = self.max();
            }
        }

        results
    }

    /// Resets the histogram to empty state.
    ///
    /// **Warning**: This operation is NOT atomic. If called concurrently with
    /// `record()` operations, the histogram may be left in an inconsistent state.
    /// Ensure exclusive access when calling this method.
    ///
    /// # Performance
    ///
    /// O(1) - resets fixed number of atomic values (~1μs typical)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use benchmark::histogram::Histogram;
    /// let histogram = Histogram::new();
    /// histogram.record(1000);
    /// assert!(!histogram.is_empty());
    ///
    /// histogram.reset();
    /// assert!(histogram.is_empty());
    /// ```
    pub fn reset(&self) {
        // Reset all buckets
        for bucket in &self.linear_buckets {
            bucket.store(0, MEMORY_ORDER);
        }
        for bucket in &self.log_buckets {
            bucket.store(0, MEMORY_ORDER);
        }

        // Reset statistics
        self.min_value.store(u64::MAX, MEMORY_ORDER);
        self.max_value.store(0, MEMORY_ORDER);
        self.total_count.store(0, MEMORY_ORDER);
        self.sum.store(0, MEMORY_ORDER);
    }

    // Private helper methods

    /// Atomically updates minimum value using compare-and-swap loop
    #[inline]
    fn update_min(&self, value: u64) {
        let mut current_min = self.min_value.load(MEMORY_ORDER);
        while value < current_min {
            match self.min_value.compare_exchange_weak(
                current_min,
                value,
                MEMORY_ORDER,
                MEMORY_ORDER,
            ) {
                Ok(_) => break,
                Err(actual) => current_min = actual,
            }
        }
    }

    /// Atomically updates maximum value using compare-and-swap loop  
    #[inline]
    fn update_max(&self, value: u64) {
        let mut current_max = self.max_value.load(MEMORY_ORDER);
        while value > current_max {
            match self.max_value.compare_exchange_weak(
                current_max,
                value,
                MEMORY_ORDER,
                MEMORY_ORDER,
            ) {
                Ok(_) => break,
                Err(actual) => current_max = actual,
            }
        }
    }

    /// Calculates the logarithmic bucket index for a given value
    #[inline]
    fn log_bucket_index(value: u64) -> usize {
        if value < LINEAR_BUCKETS as u64 {
            0 // Should not happen, but safe fallback
        } else {
            // Find the position of the highest set bit
            // This gives us log2(value) which determines the bucket
            63 - value.leading_zeros() as usize
        }
    }

    /// Returns the start value for a logarithmic bucket
    #[inline]
    fn bucket_start(bucket_idx: usize) -> u64 {
        if bucket_idx == 0 {
            LINEAR_BUCKETS as u64
        } else {
            (1u64 << bucket_idx).max(LINEAR_BUCKETS as u64)
        }
    }

    /// Returns the end value for a logarithmic bucket (exclusive)
    #[inline]
    fn bucket_end(bucket_idx: usize) -> u64 {
        if bucket_idx >= 63 {
            u64::MAX
        } else {
            1u64 << (bucket_idx + 1)
        }
    }
}

#[cfg(not(feature = "hdr"))]
impl Default for FastHistogram {
    fn default() -> Self {
        Self::new()
    }
}

// Select backend implementation
#[cfg(feature = "hdr")]
type BackendHistogram = crate::hist_hdr::Histogram;
#[cfg(not(feature = "hdr"))]
type BackendHistogram = FastHistogram;

/// Public wrapper that delegates to the selected backend (default: `FastHistogram`; with `hdr`: HDR backend).
#[derive(Debug)]
pub struct Histogram {
    inner: BackendHistogram,
}

impl Histogram {
    /// Creates a new empty histogram.
    #[inline]
    pub fn new() -> Self {
        Self {
            inner: BackendHistogram::new(),
        }
    }

    /// Records a timing value in nanoseconds.
    #[inline]
    pub fn record(&self, value_ns: u64) {
        self.inner.record(value_ns);
    }

    /// Records a Duration value.
    #[inline]
    pub fn record_duration(&self, duration: Duration) {
        self.inner.record_duration(duration);
    }

    /// Returns the minimum recorded value in nanoseconds.
    #[inline]
    pub fn min(&self) -> Option<u64> {
        self.inner.min()
    }

    /// Returns the maximum recorded value in nanoseconds.
    #[inline]
    pub fn max(&self) -> Option<u64> {
        self.inner.max()
    }

    /// Returns the arithmetic mean of recorded values.
    #[inline]
    pub fn mean(&self) -> Option<f64> {
        self.inner.mean()
    }

    /// Returns the total number of recorded values.
    #[inline]
    pub fn count(&self) -> u64 {
        self.inner.count()
    }

    /// Returns true if no values have been recorded.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Returns the value at the specified percentile.
    #[inline]
    pub fn percentile(&self, percentile: f64) -> Option<u64> {
        self.inner.percentile(percentile)
    }

    /// Returns the median value (50th percentile).
    #[inline]
    pub fn median(&self) -> Option<u64> {
        self.inner.median()
    }

    /// Returns the median as a Duration.
    #[inline]
    pub fn median_duration(&self) -> Option<Duration> {
        self.inner.median_duration()
    }

    /// Returns the percentile as a Duration.
    #[inline]
    pub fn percentile_duration(&self, percentile: f64) -> Option<Duration> {
        self.inner.percentile_duration(percentile)
    }

    /// Returns multiple percentiles efficiently in a single pass.
    #[inline]
    pub fn percentiles(&self, percentiles: &[f64]) -> Vec<Option<u64>> {
        self.inner.percentiles(percentiles)
    }

    /// Resets the histogram to empty state.
    pub fn reset(&self) {
        self.inner.reset();
    }
}

impl Default for Histogram {
    fn default() -> Self {
        Self::new()
    }
}

// `Histogram` is composed entirely of atomic primitives and thus is `Send` and `Sync`
// by default. No explicit unsafe impls are required.

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(not(feature = "hdr"))]
    use std::sync::Arc;
    #[cfg(not(feature = "hdr"))]
    use std::thread;

    #[inline]
    fn perf_enabled() -> bool {
        // Opt-in perf tests via environment to avoid CI/host variance
        std::env::var_os("PERF_TESTS").is_some()
    }

    #[test]
    fn test_empty_histogram() {
        let hist = Histogram::new();
        assert!(hist.is_empty());
        assert_eq!(hist.count(), 0);
        assert_eq!(hist.min(), None);
        assert_eq!(hist.max(), None);
        assert_eq!(hist.mean(), None);
        assert_eq!(hist.percentile(0.5), None);
        assert_eq!(hist.median(), None);
    }

    #[test]
    fn test_basic_statistics() {
        let hist = Histogram::new();

        hist.record(100);
        hist.record(200);
        hist.record(300);

        assert!(!hist.is_empty());
        assert_eq!(hist.count(), 3);
        assert_eq!(hist.min(), Some(100));
        assert_eq!(hist.max(), Some(300));
        assert_eq!(hist.mean(), Some(200.0));
        assert_eq!(hist.median(), Some(200));
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_percentiles() {
        let hist = Histogram::new();

        // Record values 1-100
        for i in 1..=100 {
            hist.record(i);
        }

        assert_eq!(hist.percentile(0.0), Some(1));
        assert_eq!(hist.percentile(0.5), Some(50));
        assert_eq!(hist.percentile(0.99), Some(99));
        assert_eq!(hist.percentile(1.0), Some(100));

        // Inputs out of range are clamped to [0.0, 1.0]
        assert_eq!(hist.percentile(-0.1), Some(1));
        assert_eq!(hist.percentile(1.1), Some(100));
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_multiple_percentiles() {
        let hist = Histogram::new();

        for i in 1..=1000 {
            hist.record(i);
        }

        let percentiles = hist.percentiles(&[0.0, 0.25, 0.5, 0.75, 0.95, 0.99, 1.0]);

        assert_eq!(percentiles[0], Some(1)); // 0th percentile
        assert_eq!(percentiles[1], Some(250)); // 25th percentile
        assert_eq!(percentiles[2], Some(500)); // 50th percentile
        assert_eq!(percentiles[3], Some(750)); // 75th percentile
        assert_eq!(percentiles[4], Some(950)); // 95th percentile
        assert_eq!(percentiles[5], Some(990)); // 99th percentile
        assert_eq!(percentiles[6], Some(1000)); // 100th percentile
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_large_values() {
        let hist = Histogram::new();

        hist.record(1_000_000); // 1ms
        hist.record(1_000_000_000); // 1s
        hist.record(500); // 500ns

        assert_eq!(hist.min(), Some(500));
        assert_eq!(hist.max(), Some(1_000_000_000));
        assert_eq!(hist.count(), 3);
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_duration_api() {
        let hist = Histogram::new();

        hist.record_duration(Duration::from_nanos(100));
        hist.record_duration(Duration::from_micros(1)); // 1000ns
        hist.record_duration(Duration::from_millis(1)); // 1_000_000ns

        assert_eq!(hist.count(), 3);
        assert_eq!(hist.min(), Some(100));
        assert_eq!(hist.max(), Some(1_000_000));

        let median_duration = hist.median_duration().unwrap();
        assert_eq!(median_duration, Duration::from_nanos(1000));
    }

    #[test]
    fn test_reset() {
        let hist = Histogram::new();

        hist.record(100);
        hist.record(200);
        assert_eq!(hist.count(), 2);

        hist.reset();
        assert!(hist.is_empty());
        assert_eq!(hist.count(), 0);
        assert_eq!(hist.min(), None);
        assert_eq!(hist.max(), None);
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_overflow_protection() {
        let hist = Histogram::new();

        // Test with maximum values
        hist.record(u64::MAX);
        hist.record(u64::MAX - 1);

        assert_eq!(hist.count(), 2);
        assert_eq!(hist.max(), Some(u64::MAX));

        // Should not panic on overflow
        let mean = hist.mean().unwrap();
        assert!(mean > 0.0);
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_thread_safety() {
        let hist = Arc::new(Histogram::new());
        let mut handles = vec![];

        // Spawn multiple threads recording values concurrently
        for thread_id in 0..10 {
            let hist_clone = Arc::clone(&hist);
            let handle = thread::spawn(move || {
                for i in 0..1000 {
                    hist_clone.record(thread_id * 1000 + i);
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }

        // Verify all values were recorded
        assert_eq!(hist.count(), 10_000);
        assert_eq!(hist.min(), Some(0));
        assert_eq!(hist.max(), Some(9_999));
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_concurrent_statistics() {
        let hist = Arc::new(Histogram::new());
        let mut handles = vec![];

        // One thread recording values
        let hist_writer = Arc::clone(&hist);
        let writer_handle = thread::spawn(move || {
            for i in 1..=10000 {
                hist_writer.record(i);
                if i % 100 == 0 {
                    thread::yield_now(); // Allow readers to interleave
                }
            }
        });

        // Multiple threads reading statistics concurrently
        for _ in 0..5 {
            let hist_reader = Arc::clone(&hist);
            let reader_handle = thread::spawn(move || {
                for _ in 0..100 {
                    let _ = hist_reader.count();
                    let _ = hist_reader.min();
                    let _ = hist_reader.max();
                    let _ = hist_reader.mean();
                    let _ = hist_reader.median();
                    let _ = hist_reader.percentile(0.95);
                    thread::yield_now();
                }
            });
            handles.push(reader_handle);
        }

        // Wait for writer to complete
        writer_handle.join().unwrap();

        // Wait for all readers to complete
        for handle in handles {
            handle.join().unwrap();
        }

        // Verify final state
        assert_eq!(hist.count(), 10_000);
        assert_eq!(hist.min(), Some(1));
        assert_eq!(hist.max(), Some(10_000));
        assert_eq!(hist.median(), Some(5_000));
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_precision_linear_buckets() {
        let hist = Histogram::new();

        // Test exact precision in linear range (0-1023ns)
        for i in 0u32..1024 {
            hist.record(u64::from(i));
        }

        // Should have exact precision for each nanosecond
        for i in 0u32..1024 {
            let percentile = f64::from(i) / 1023.0;
            let value = hist.percentile(percentile).unwrap();
            assert!(value <= u64::from(i), "Value {value} should be <= {i}");
        }
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_logarithmic_buckets() {
        let hist = Histogram::new();

        // Test values that fall into logarithmic buckets
        let large_values = vec![
            2_000,      // 2μs
            10_000,     // 10μs
            100_000,    // 100μs
            1_000_000,  // 1ms
            10_000_000, // 10ms
        ];

        for &value in &large_values {
            hist.record(value);
        }

        assert_eq!(hist.count(), 5);
        assert_eq!(hist.min(), Some(2_000));
        assert_eq!(hist.max(), Some(10_000_000));

        // Median should be approximately the middle value
        let median = hist.median().unwrap();
        assert!((50_000..=150_000).contains(&median));
    }

    #[cfg(not(feature = "hdr"))]
    #[test]
    fn test_edge_cases() {
        let hist = Histogram::new();

        // Test recording zero
        hist.record(0);
        assert_eq!(hist.min(), Some(0));
        assert_eq!(hist.max(), Some(0));
        assert_eq!(hist.median(), Some(0));

        hist.reset();

        // Test single value
        hist.record(42);
        assert_eq!(hist.count(), 1);
        assert_eq!(hist.percentile(0.0), Some(42));
        assert_eq!(hist.percentile(0.5), Some(42));
        assert_eq!(hist.percentile(1.0), Some(42));
    }

    #[cfg_attr(
        not(feature = "perf-tests"),
        ignore = "perf tests are opt-in; set PERF_TESTS=1 to enable"
    )]
    #[test]
    fn test_performance_characteristics() {
        if !perf_enabled() {
            eprintln!("skipping perf test: set PERF_TESTS=1 to enable");
            return;
        }
        let hist = Histogram::new();

        // Record a large number of values to test performance
        let start = std::time::Instant::now();
        for i in 0..100_000 {
            hist.record(i % 10_000);
        }
        let record_duration = start.elapsed();

        // Should be very fast - less than 10ms for 100k records
        assert!(
            record_duration.as_millis() < 10,
            "Recording 100k values took {}ms, expected <10ms",
            record_duration.as_millis()
        );

        // Test percentile calculation performance
        let start = std::time::Instant::now();
        for _ in 0..1000 {
            let _ = hist.percentile(0.95);
        }
        let percentile_duration = start.elapsed();

        // Should be very fast - less than 1ms for 1k percentile calculations
        assert!(
            percentile_duration.as_millis() < 1,
            "1000 percentile calculations took {}ms, expected <1ms",
            percentile_duration.as_millis()
        );
    }

    #[cfg_attr(
        not(feature = "perf-tests"),
        ignore = "perf tests are opt-in; set PERF_TESTS=1 to enable"
    )]
    #[test]
    fn test_memory_efficiency() {
        // Test that histogram has reasonable memory footprint
        let hist = Histogram::new();

        // Record many values - memory usage should remain constant
        for i in 0..1_000_000 {
            hist.record(i);
        }

        // Memory usage is fixed regardless of number of samples
        assert_eq!(hist.count(), 1_000_000);

        // Verify we can still get accurate statistics
        #[cfg(not(feature = "hdr"))]
        {
            assert_eq!(hist.min(), Some(0));
        }
        #[cfg(feature = "hdr")]
        {
            // HDR backend may clamp inputs; allow 0 or 1 as minimum
            assert!(matches!(hist.min(), Some(0 | 1)));
        }
        #[cfg(not(feature = "hdr"))]
        {
            assert_eq!(hist.max(), Some(999_999));
        }
        #[cfg(feature = "hdr")]
        {
            // HDR backend may round up due to bucket resolution; ensure it's at least input max
            assert!(matches!(hist.max(), Some(m) if m >= 999_999));
        }
        let median = hist.median().unwrap();
        assert!((400_000..=600_000).contains(&median));
    }
}

// Benchmark tests (compile only during tests with the `benchmark` feature)
#[cfg(all(feature = "benchmark", test))]
mod benches {
    use super::*;
    use std::sync::Arc;
    use std::thread;

    #[inline]
    fn perf_enabled() -> bool {
        std::env::var_os("PERF_TESTS").is_some()
    }

    #[cfg_attr(
        not(feature = "perf-tests"),
        ignore = "perf tests are opt-in; set PERF_TESTS=1 to enable"
    )]
    #[test]
    fn bench_record_single_thread() {
        if !perf_enabled() {
            eprintln!("skipping perf bench: set PERF_TESTS=1 to enable");
            return;
        }
        let hist = Histogram::new();
        let iterations: u64 = 10_000_000;

        let start = std::time::Instant::now();
        for i in 0..iterations {
            hist.record(i % 100_000);
        }
        let duration = start.elapsed();

        let ns_per_op = duration.as_nanos() / u128::from(iterations);
        println!("Single-thread record: {ns_per_op} ns/op");

        // Should be under 10ns per operation on modern hardware
        assert!(
            ns_per_op < 20,
            "Record operation too slow: {ns_per_op} ns/op",
        );
    }

    #[cfg_attr(
        not(feature = "perf-tests"),
        ignore = "perf tests are opt-in; set PERF_TESTS=1 to enable"
    )]
    #[test]
    fn bench_record_multi_thread() {
        if !perf_enabled() {
            eprintln!("skipping perf bench: set PERF_TESTS=1 to enable");
            return;
        }
        let hist = Arc::new(Histogram::new());
        let threads: u64 = 8;
        let iterations_per_thread: u64 = 1_000_000;

        let start = std::time::Instant::now();

        let handles: Vec<_> = (0..threads)
            .map(|thread_id| {
                let hist_clone = Arc::clone(&hist);
                thread::spawn(move || {
                    for i in 0..iterations_per_thread {
                        hist_clone.record(thread_id * 1_000_000 + i);
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        let duration = start.elapsed();
        let total_ops: u64 = threads * iterations_per_thread;
        let ns_per_op = duration.as_nanos() / u128::from(total_ops);

        println!("Multi-thread record ({threads} threads): {ns_per_op} ns/op");

        // Should scale reasonably with multiple threads
        assert!(
            ns_per_op < 50,
            "Multi-thread record too slow: {ns_per_op} ns/op",
        );
        assert_eq!(hist.count(), total_ops);
    }

    #[cfg_attr(
        not(feature = "perf-tests"),
        ignore = "perf tests are opt-in; set PERF_TESTS=1 to enable"
    )]
    #[test]
    fn bench_percentile_calculation() {
        if !perf_enabled() {
            eprintln!("skipping perf bench: set PERF_TESTS=1 to enable");
            return;
        }
        let hist = Histogram::new();

        // Populate with realistic timing data
        for i in 0..100_000 {
            hist.record(i % 10_000);
        }

        let iterations: u64 = 100_000;
        let start = std::time::Instant::now();

        for i in 0..iterations {
            let percentile = f64::from((i % 100) as u32) / 100.0;
            let _ = hist.percentile(percentile);
        }

        let duration = start.elapsed();
        let ns_per_op = duration.as_nanos() / u128::from(iterations);

        println!("Percentile calculation: {ns_per_op} ns/op");

        // Should be under 1000ns per percentile calculation
        assert!(
            ns_per_op < 1000,
            "Percentile calculation too slow: {ns_per_op} ns/op",
        );
    }
}