metrics-lib 0.9.1

The fastest metrics library for Rust. Lock-free 0.6ns gauges, 18ns counters, timers, rate meters, async timing, adaptive sampling, and system health. Cross-platform with minimal dependencies.
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
//! # Atomic Gauge with IEEE 754 Optimization
//!
//! Ultra-fast atomic gauge using bit manipulation for f64 values.
//!
//! ## Features
//!
//! - **Atomic f64 operations** - Using IEEE 754 bit manipulation
//! - **Sub-5ns updates** - Direct bit-level atomic operations
//! - **Zero allocations** - Pure stack operations
//! - **Lock-free** - Never blocks, never waits
//! - **Cache optimized** - Aligned to prevent false sharing

use crate::{MetricsError, Result};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

/// Ultra-fast atomic gauge for f64 values
///
/// Uses IEEE 754 bit manipulation for atomic operations on floating-point values.
/// Cache-line aligned to prevent false sharing.
#[repr(align(64))]
pub struct Gauge {
    /// Gauge value stored as IEEE 754 bits in atomic u64
    value: AtomicU64,
    /// Creation timestamp for statistics
    created_at: Instant,
}

/// Gauge statistics
#[derive(Debug, Clone)]
pub struct GaugeStats {
    /// Current gauge value
    pub value: f64,
    /// Time since gauge creation
    pub age: Duration,
    /// Number of updates (not tracked by default for performance)
    pub updates: Option<u64>,
}

impl Gauge {
    /// Create new gauge starting at zero
    #[inline]
    pub fn new() -> Self {
        Self {
            value: AtomicU64::new(0.0_f64.to_bits()),
            created_at: Instant::now(),
        }
    }

    /// Try to add to current value with validation
    ///
    /// Returns `Err(MetricsError::InvalidValue)` if `delta` is not finite (NaN or ±inf)
    /// or if the resulting value would become non-finite. Returns
    /// `Err(MetricsError::Overflow)` if the sum overflows to a non-finite value.
    ///
    /// Example
    /// ```
    /// use metrics_lib::{Gauge, MetricsError};
    /// let g = Gauge::with_value(1.5);
    /// g.try_add(2.5).unwrap();
    /// assert_eq!(g.get(), 4.0);
    /// assert!(matches!(g.try_add(f64::INFINITY), Err(MetricsError::InvalidValue{..})));
    /// ```
    #[inline]
    pub fn try_add(&self, delta: f64) -> Result<()> {
        if delta == 0.0 {
            return Ok(());
        }
        if !delta.is_finite() {
            return Err(MetricsError::InvalidValue {
                reason: "delta is not finite",
            });
        }

        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);
            let new_value = current_value + delta;
            if !new_value.is_finite() {
                return Err(MetricsError::Overflow);
            }
            let new_bits = new_value.to_bits();

            match self.value.compare_exchange_weak(
                current_bits,
                new_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => return Ok(()),
                Err(_) => continue,
            }
        }
    }

    /// Try to subtract from current value (validated)
    ///
    /// Validation semantics are identical to [`Gauge::try_add`] but apply to
    /// subtraction (`-delta`).
    ///
    /// Example
    /// ```
    /// use metrics_lib::Gauge;
    /// let g = Gauge::with_value(10.0);
    /// g.try_sub(4.0).unwrap();
    /// assert_eq!(g.get(), 6.0);
    /// ```
    #[inline]
    pub fn try_sub(&self, delta: f64) -> Result<()> {
        self.try_add(-delta)
    }

    /// Create gauge with initial value
    #[inline]
    pub fn with_value(initial: f64) -> Self {
        Self {
            value: AtomicU64::new(initial.to_bits()),
            created_at: Instant::now(),
        }
    }

    /// Set gauge value - THE FASTEST PATH
    ///
    /// This is optimized for maximum speed:
    /// - Convert f64 to IEEE 754 bits
    /// - Single atomic store instruction
    /// - Relaxed memory ordering for speed
    /// - Inlined for zero function call overhead
    #[inline(always)]
    pub fn set(&self, value: f64) {
        self.value.store(value.to_bits(), Ordering::Relaxed);
    }

    /// Try to set gauge value with validation
    ///
    /// Returns `Err(MetricsError::InvalidValue)` if `value` is not finite (NaN or ±inf).
    ///
    /// Example
    /// ```
    /// use metrics_lib::{Gauge, MetricsError};
    /// let g = Gauge::new();
    /// assert!(g.try_set(42.0).is_ok());
    /// assert!(matches!(g.try_set(f64::NAN), Err(MetricsError::InvalidValue{..})));
    /// ```
    #[inline]
    pub fn try_set(&self, value: f64) -> Result<()> {
        if !value.is_finite() {
            return Err(MetricsError::InvalidValue {
                reason: "value is not finite",
            });
        }
        self.set(value);
        Ok(())
    }

    /// Get current value - single atomic load
    ///
    /// Note: `#[must_use]`. The returned value represents current gauge state;
    /// ignoring it may indicate a logic bug.
    #[must_use]
    #[inline(always)]
    pub fn get(&self) -> f64 {
        f64::from_bits(self.value.load(Ordering::Relaxed))
    }

    /// Add to current value - atomic compare-and-swap loop
    ///
    /// # Non-finite protection
    /// If `delta` is non-finite (NaN, ±∞), this is a **no-op**.
    /// If the resulting sum would also be non-finite the gauge retains its
    /// current value (silent no-op). Use [`Gauge::try_add`] to receive an
    /// explicit error instead.
    #[inline]
    pub fn add(&self, delta: f64) {
        if delta == 0.0 || !delta.is_finite() {
            return;
        }

        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);
            let new_value = current_value + delta;
            if !new_value.is_finite() {
                return; // Silently no-op — use try_add for error reporting
            }
            let new_bits = new_value.to_bits();

            match self.value.compare_exchange_weak(
                current_bits,
                new_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(_) => continue, // Retry with latest value
            }
        }
    }

    /// Subtract from current value
    ///
    /// Non-finite and overflow protection is identical to [`Gauge::add`].
    #[inline]
    pub fn sub(&self, delta: f64) {
        self.add(-delta);
    }

    /// Set to maximum of current value and new value
    #[inline]
    pub fn set_max(&self, value: f64) {
        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);

            if value <= current_value {
                break; // Current value is already larger
            }

            let new_bits = value.to_bits();
            match self.value.compare_exchange_weak(
                current_bits,
                new_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(_) => continue, // Retry
            }
        }
    }

    /// Try to set to maximum of current value and new value
    ///
    /// Returns `Err(MetricsError::InvalidValue)` if `value` is not finite.
    /// Otherwise sets the gauge to `max(current, value)` and returns `Ok(())`.
    ///
    /// Example
    /// ```
    /// use metrics_lib::{Gauge, MetricsError};
    /// let g = Gauge::with_value(5.0);
    /// g.try_set_max(10.0).unwrap();
    /// assert_eq!(g.get(), 10.0);
    /// assert!(matches!(g.try_set_max(f64::INFINITY), Err(MetricsError::InvalidValue{..})));
    /// ```
    #[inline]
    pub fn try_set_max(&self, value: f64) -> Result<()> {
        if !value.is_finite() {
            return Err(MetricsError::InvalidValue {
                reason: "value is not finite",
            });
        }
        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);
            if value <= current_value {
                return Ok(());
            }
            let new_bits = value.to_bits();
            match self.value.compare_exchange_weak(
                current_bits,
                new_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => return Ok(()),
                Err(_) => continue,
            }
        }
    }

    /// Set to minimum of current value and new value
    #[inline]
    pub fn set_min(&self, value: f64) {
        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);

            if value >= current_value {
                break; // Current value is already smaller
            }

            let new_bits = value.to_bits();
            match self.value.compare_exchange_weak(
                current_bits,
                new_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(_) => continue, // Retry
            }
        }
    }

    /// Try to set to minimum of current value and new value
    ///
    /// Returns `Err(MetricsError::InvalidValue)` if `value` is not finite.
    /// Otherwise sets the gauge to `min(current, value)` and returns `Ok(())`.
    ///
    /// Example
    /// ```
    /// use metrics_lib::{Gauge, MetricsError};
    /// let g = Gauge::with_value(10.0);
    /// g.try_set_min(7.0).unwrap();
    /// assert_eq!(g.get(), 7.0);
    /// assert!(matches!(g.try_set_min(f64::NAN), Err(MetricsError::InvalidValue{..})));
    /// ```
    #[inline]
    pub fn try_set_min(&self, value: f64) -> Result<()> {
        if !value.is_finite() {
            return Err(MetricsError::InvalidValue {
                reason: "value is not finite",
            });
        }
        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);
            if value >= current_value {
                return Ok(());
            }
            let new_bits = value.to_bits();
            match self.value.compare_exchange_weak(
                current_bits,
                new_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => return Ok(()),
                Err(_) => continue,
            }
        }
    }

    /// Atomic compare-and-swap
    ///
    /// Returns Ok(previous_value) if successful, Err(current_value) if failed
    #[inline]
    pub fn compare_and_swap(&self, expected: f64, new: f64) -> core::result::Result<f64, f64> {
        let expected_bits = expected.to_bits();
        let new_bits = new.to_bits();

        match self.value.compare_exchange(
            expected_bits,
            new_bits,
            Ordering::SeqCst,
            Ordering::SeqCst,
        ) {
            Ok(prev_bits) => Ok(f64::from_bits(prev_bits)),
            Err(current_bits) => Err(f64::from_bits(current_bits)),
        }
    }

    /// Reset to zero
    #[inline]
    pub fn reset(&self) {
        self.set(0.0);
    }

    /// Multiply current value by factor
    ///
    /// If `factor` is non-finite, or if the product would be non-finite,
    /// this is a **no-op** (the gauge retains its current value).
    #[inline]
    pub fn multiply(&self, factor: f64) {
        if factor == 1.0 || !factor.is_finite() {
            return;
        }

        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);
            let new_value = current_value * factor;
            if !new_value.is_finite() {
                return; // Guard against overflow to ±Inf or NaN propagation
            }
            let new_bits = new_value.to_bits();

            match self.value.compare_exchange_weak(
                current_bits,
                new_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(_) => continue,
            }
        }
    }

    /// Divide current value by divisor
    ///
    /// If `divisor` is zero, non-finite, or the result would be non-finite,
    /// this is a **no-op** (the gauge retains its current value). Non-finite
    /// product protection is handled transitively by [`Gauge::multiply`].
    #[inline]
    pub fn divide(&self, divisor: f64) {
        if divisor == 0.0 || divisor == 1.0 || !divisor.is_finite() {
            return;
        }
        self.multiply(1.0 / divisor);
    }

    /// Set to absolute value of current value
    #[inline]
    pub fn abs(&self) {
        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);

            if current_value >= 0.0 {
                break; // Already positive
            }

            let abs_value = current_value.abs();
            let abs_bits = abs_value.to_bits();

            match self.value.compare_exchange_weak(
                current_bits,
                abs_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(_) => continue,
            }
        }
    }

    /// Clamp value to range [min, max]
    #[inline]
    pub fn clamp(&self, min: f64, max: f64) {
        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);
            let clamped_value = current_value.clamp(min, max);

            if (current_value - clamped_value).abs() < f64::EPSILON {
                break; // Already in range
            }

            let clamped_bits = clamped_value.to_bits();

            match self.value.compare_exchange_weak(
                current_bits,
                clamped_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(_) => continue,
            }
        }
    }

    /// Exponential moving average update
    ///
    /// new_value = alpha * sample + (1 - alpha) * old_value
    #[inline]
    pub fn update_ema(&self, sample: f64, alpha: f64) {
        let alpha = alpha.clamp(0.0, 1.0);

        loop {
            let current_bits = self.value.load(Ordering::Relaxed);
            let current_value = f64::from_bits(current_bits);
            let new_value = alpha * sample + (1.0 - alpha) * current_value;
            let new_bits = new_value.to_bits();

            match self.value.compare_exchange_weak(
                current_bits,
                new_bits,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(_) => continue,
            }
        }
    }

    /// Get comprehensive statistics
    ///
    /// Note: `#[must_use]`. Statistics summarize current state; dropping the
    /// result may indicate a logic bug.
    #[must_use]
    pub fn stats(&self) -> GaugeStats {
        GaugeStats {
            value: self.get(),
            age: self.created_at.elapsed(),
            updates: None, // Not tracked by default for performance
        }
    }

    /// Get age since creation
    ///
    /// Note: `#[must_use]`. Age is often used for derived metrics; don't call
    /// this for side effects.
    #[must_use]
    #[inline]
    pub fn age(&self) -> Duration {
        self.created_at.elapsed()
    }

    /// Check if gauge is zero
    ///
    /// Note: `#[must_use]`. The boolean result determines control flow.
    #[must_use]
    #[inline]
    pub fn is_zero(&self) -> bool {
        self.get() == 0.0
    }

    /// Check if value is positive
    ///
    /// Note: `#[must_use]`. The boolean result determines control flow.
    #[must_use]
    #[inline]
    pub fn is_positive(&self) -> bool {
        self.get() > 0.0
    }

    /// Check if value is negative
    ///
    /// Note: `#[must_use]`. The boolean result determines control flow.
    #[must_use]
    #[inline]
    pub fn is_negative(&self) -> bool {
        self.get() < 0.0
    }

    /// Check if value is finite (not NaN or infinity)
    ///
    /// Note: `#[must_use]`. The boolean result determines control flow.
    #[must_use]
    #[inline]
    pub fn is_finite(&self) -> bool {
        self.get().is_finite()
    }
}

impl Default for Gauge {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for Gauge {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Gauge({})", self.get())
    }
}

impl std::fmt::Debug for Gauge {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Gauge")
            .field("value", &self.get())
            .field("age", &self.age())
            .field("is_finite", &self.is_finite())
            .finish()
    }
}

// Thread safety - Gauge is Send + Sync
// Gauge is composed of `AtomicU64` (Send + Sync) and `Instant` (Send + Sync).
// The compiler derives Send + Sync automatically; no explicit unsafe impl needed.

/// Specialized gauge types for common use cases
pub mod specialized {
    use super::*;

    /// Percentage gauge (0.0 to 100.0)
    #[repr(align(64))]
    pub struct PercentageGauge {
        inner: Gauge,
    }

    impl PercentageGauge {
        /// Create new percentage gauge
        #[inline]
        pub fn new() -> Self {
            Self {
                inner: Gauge::new(),
            }
        }

        /// Set percentage (automatically clamped to 0.0-100.0)
        #[inline(always)]
        pub fn set_percentage(&self, percentage: f64) {
            let clamped = percentage.clamp(0.0, 100.0);
            self.inner.set(clamped);
        }

        /// Get percentage
        #[inline(always)]
        pub fn get_percentage(&self) -> f64 {
            self.inner.get()
        }

        /// Set from ratio (0.0-1.0 becomes 0.0-100.0)
        #[inline(always)]
        pub fn set_ratio(&self, ratio: f64) {
            let percentage = (ratio * 100.0).clamp(0.0, 100.0);
            self.inner.set(percentage);
        }

        /// Get as ratio (0.0-1.0)
        #[inline(always)]
        pub fn get_ratio(&self) -> f64 {
            self.inner.get() / 100.0
        }

        /// Check if at maximum (100%)
        #[inline]
        pub fn is_full(&self) -> bool {
            (self.inner.get() - 100.0).abs() < f64::EPSILON
        }

        /// Check if at minimum (0%)
        #[inline]
        pub fn is_empty(&self) -> bool {
            self.inner.get() < f64::EPSILON
        }

        /// Add percentage (clamped to valid range)
        #[inline]
        pub fn add_percentage(&self, delta: f64) {
            loop {
                let current = self.get_percentage();
                let new_value = (current + delta).clamp(0.0, 100.0);

                if (current - new_value).abs() < f64::EPSILON {
                    break; // No change needed
                }

                match self.inner.compare_and_swap(current, new_value) {
                    Ok(_) => break,
                    Err(_) => continue, // Retry
                }
            }
        }

        /// Get gauge statistics
        pub fn stats(&self) -> GaugeStats {
            self.inner.stats()
        }
    }

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

    impl std::fmt::Display for PercentageGauge {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "PercentageGauge({}%)", self.get_percentage())
        }
    }

    /// CPU usage gauge (specialized percentage gauge)
    pub type CpuGauge = PercentageGauge;

    /// Memory gauge for byte values
    #[repr(align(64))]
    pub struct MemoryGauge {
        bytes: Gauge,
    }

    impl MemoryGauge {
        /// Create new memory gauge
        #[inline]
        pub fn new() -> Self {
            Self {
                bytes: Gauge::new(),
            }
        }

        /// Set memory usage in bytes
        #[inline(always)]
        pub fn set_bytes(&self, bytes: u64) {
            self.bytes.set(bytes as f64);
        }

        /// Get memory usage in bytes
        #[inline]
        pub fn get_bytes(&self) -> u64 {
            self.bytes.get() as u64
        }

        /// Get memory usage in KB
        #[inline]
        pub fn get_kb(&self) -> f64 {
            self.bytes.get() / 1024.0
        }

        /// Get memory usage in MB
        #[inline]
        pub fn get_mb(&self) -> f64 {
            self.bytes.get() / (1024.0 * 1024.0)
        }

        /// Get memory usage in GB
        #[inline]
        pub fn get_gb(&self) -> f64 {
            self.bytes.get() / (1024.0 * 1024.0 * 1024.0)
        }

        /// Add bytes
        #[inline]
        pub fn add_bytes(&self, bytes: i64) {
            self.bytes.add(bytes as f64);
        }

        /// Get gauge statistics
        pub fn stats(&self) -> GaugeStats {
            self.bytes.stats()
        }
    }

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

    impl std::fmt::Display for MemoryGauge {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            let mb = self.get_mb();
            if mb >= 1024.0 {
                write!(f, "MemoryGauge({:.2} GB)", self.get_gb())
            } else {
                write!(f, "MemoryGauge({mb:.2} MB)")
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::thread;

    #[test]
    fn test_basic_operations() {
        let gauge = Gauge::new();

        assert_eq!(gauge.get(), 0.0);
        assert!(gauge.is_zero());
        assert!(gauge.is_finite());

        gauge.set(42.5);
        assert_eq!(gauge.get(), 42.5);
        assert!(!gauge.is_zero());
        assert!(gauge.is_positive());

        gauge.add(7.5);
        assert_eq!(gauge.get(), 50.0);

        gauge.sub(10.0);
        assert_eq!(gauge.get(), 40.0);

        gauge.reset();
        assert_eq!(gauge.get(), 0.0);
    }

    #[test]
    fn test_mathematical_operations() {
        let gauge = Gauge::with_value(10.0);

        gauge.multiply(2.0);
        assert_eq!(gauge.get(), 20.0);

        gauge.divide(4.0);
        assert_eq!(gauge.get(), 5.0);

        gauge.set(-15.0);
        assert!(gauge.is_negative());

        gauge.abs();
        assert_eq!(gauge.get(), 15.0);
        assert!(gauge.is_positive());

        gauge.clamp(5.0, 10.0);
        assert_eq!(gauge.get(), 10.0);
    }

    #[test]
    fn test_min_max_operations() {
        let gauge = Gauge::with_value(10.0);

        gauge.set_max(15.0);
        assert_eq!(gauge.get(), 15.0);

        gauge.set_max(12.0); // Should not change
        assert_eq!(gauge.get(), 15.0);

        gauge.set_min(8.0);
        assert_eq!(gauge.get(), 8.0);

        gauge.set_min(12.0); // Should not change
        assert_eq!(gauge.get(), 8.0);
    }

    #[test]
    fn test_compare_and_swap() {
        let gauge = Gauge::with_value(10.0);

        // Successful swap
        assert_eq!(gauge.compare_and_swap(10.0, 20.0), Ok(10.0));
        assert_eq!(gauge.get(), 20.0);

        // Failed swap
        assert_eq!(gauge.compare_and_swap(10.0, 30.0), Err(20.0));
        assert_eq!(gauge.get(), 20.0);
    }

    #[test]
    fn test_ema_update() {
        let gauge = Gauge::with_value(10.0);

        // EMA with alpha = 0.5: 0.5 * 20 + 0.5 * 10 = 15
        gauge.update_ema(20.0, 0.5);
        assert_eq!(gauge.get(), 15.0);

        // EMA with alpha = 0.3: 0.3 * 30 + 0.7 * 15 = 19.5
        gauge.update_ema(30.0, 0.3);
        assert_eq!(gauge.get(), 19.5);
    }

    #[test]
    fn test_percentage_gauge() {
        let gauge = specialized::PercentageGauge::new();

        gauge.set_percentage(75.5);
        assert_eq!(gauge.get_percentage(), 75.5);
        assert!((gauge.get_ratio() - 0.755).abs() < f64::EPSILON);

        gauge.set_ratio(0.9);
        assert_eq!(gauge.get_percentage(), 90.0);

        // Test clamping
        gauge.set_percentage(150.0);
        assert_eq!(gauge.get_percentage(), 100.0);
        assert!(gauge.is_full());

        gauge.set_percentage(-10.0);
        assert_eq!(gauge.get_percentage(), 0.0);
        assert!(gauge.is_empty());

        // Test add with clamping
        gauge.set_percentage(95.0);
        gauge.add_percentage(10.0);
        assert_eq!(gauge.get_percentage(), 100.0);
    }

    #[test]
    fn test_memory_gauge() {
        let gauge = specialized::MemoryGauge::new();

        gauge.set_bytes(1024 * 1024 * 1024); // 1GB

        assert_eq!(gauge.get_bytes(), 1024 * 1024 * 1024);
        assert!((gauge.get_mb() - 1024.0).abs() < 0.1);
        assert!((gauge.get_gb() - 1.0).abs() < 0.001);

        gauge.add_bytes(1024 * 1024); // Add 1MB
        assert!(gauge.get_mb() > 1024.0);
    }

    #[test]
    fn test_statistics() {
        let gauge = Gauge::with_value(42.0);

        let stats = gauge.stats();
        assert_eq!(stats.value, 42.0);
        assert!(stats.age <= gauge.age());
        assert!(stats.updates.is_none()); // Not tracked by default
    }

    #[test]
    fn test_high_concurrency() {
        let gauge = Arc::new(Gauge::new());
        let num_threads = 100;
        let operations_per_thread = 1000;

        let handles: Vec<_> = (0..num_threads)
            .map(|thread_id| {
                let gauge = Arc::clone(&gauge);
                thread::spawn(move || {
                    for i in 0..operations_per_thread {
                        let value = (thread_id * operations_per_thread + i) as f64;
                        gauge.set(value);
                        gauge.add(0.1);
                        gauge.multiply(1.001);
                    }
                })
            })
            .collect();

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

        // Just check that we can read the final value without panicking
        let final_value = gauge.get();
        assert!(final_value.is_finite());

        let stats = gauge.stats();
        assert!(stats.age <= gauge.age());
    }

    #[test]
    fn test_special_values() {
        let gauge = Gauge::new();

        // Test infinity handling
        gauge.set(f64::INFINITY);
        assert!(!gauge.is_finite());

        // Test NaN handling
        gauge.set(f64::NAN);
        assert!(!gauge.is_finite());

        // Reset to normal value
        gauge.set(42.0);
        assert!(gauge.is_finite());
    }

    #[test]
    fn test_display_and_debug() {
        let gauge = Gauge::with_value(42.5);

        let display_str = format!("{gauge}");
        assert!(display_str.contains("42.5"));

        let debug_str = format!("{gauge:?}");
        assert!(debug_str.contains("Gauge"));
        assert!(debug_str.contains("42.5"));
    }

    #[test]
    fn test_try_add_validation_and_overflow() {
        let gauge = Gauge::new();

        // Invalid deltas
        assert!(matches!(
            gauge.try_add(f64::NAN),
            Err(MetricsError::InvalidValue { .. })
        ));
        assert!(matches!(
            gauge.try_add(f64::INFINITY),
            Err(MetricsError::InvalidValue { .. })
        ));

        // Overflow on result becoming non-finite
        let gauge2 = Gauge::with_value(f64::MAX / 2.0);
        assert!(matches!(
            gauge2.try_add(f64::MAX),
            Err(MetricsError::Overflow)
        ));
    }

    #[test]
    fn test_try_set_and_min_max_validation() {
        let gauge = Gauge::new();
        assert!(matches!(
            gauge.try_set(f64::NAN),
            Err(MetricsError::InvalidValue { .. })
        ));

        // try_set_max invalid
        assert!(matches!(
            gauge.try_set_max(f64::INFINITY),
            Err(MetricsError::InvalidValue { .. })
        ));

        // try_set_min invalid
        assert!(matches!(
            gauge.try_set_min(f64::NAN),
            Err(MetricsError::InvalidValue { .. })
        ));
    }

    #[test]
    fn test_ema_alpha_boundaries() {
        let gauge = Gauge::with_value(10.0);

        // alpha < 0 should clamp to 0 -> unchanged
        gauge.update_ema(100.0, -1.0);
        assert_eq!(gauge.get(), 10.0);

        // alpha > 1 should clamp to 1 -> equals sample
        gauge.update_ema(100.0, 2.0);
        assert_eq!(gauge.get(), 100.0);

        // alpha 0 -> unchanged; alpha 1 -> exact sample
        gauge.set(5.0);
        gauge.update_ema(20.0, 0.0);
        assert_eq!(gauge.get(), 5.0);
        gauge.update_ema(20.0, 1.0);
        assert_eq!(gauge.get(), 20.0);
    }

    #[test]
    fn test_non_finite_math_helpers_are_noops() {
        let gauge = Gauge::with_value(12.0);

        gauge.add(f64::NAN);
        assert_eq!(gauge.get(), 12.0);

        gauge.multiply(f64::INFINITY);
        assert_eq!(gauge.get(), 12.0);

        gauge.divide(0.0);
        assert_eq!(gauge.get(), 12.0);

        let huge = Gauge::with_value(f64::MAX / 2.0);
        huge.multiply(4.0);
        assert_eq!(huge.get(), f64::MAX / 2.0);
    }
}

#[cfg(all(test, feature = "bench-tests", not(tarpaulin)))]
#[allow(unused_imports)]
mod benchmarks {
    use super::*;
    use std::time::Instant;

    #[cfg_attr(not(feature = "bench-tests"), ignore)]
    #[test]
    fn bench_gauge_set() {
        let gauge = Gauge::new();
        let iterations = 10_000_000;

        let start = Instant::now();
        for i in 0..iterations {
            gauge.set(i as f64);
        }
        let elapsed = start.elapsed();

        println!(
            "Gauge set: {:.2} ns/op",
            elapsed.as_nanos() as f64 / iterations as f64
        );

        // Should be under 100ns per set operation (relaxed from 50ns)
        assert!(elapsed.as_nanos() / iterations < 100);
        assert_eq!(gauge.get(), (iterations - 1) as f64);
    }

    #[cfg_attr(not(feature = "bench-tests"), ignore)]
    #[test]
    fn bench_gauge_add() {
        let gauge = Gauge::new();
        let iterations = 1_000_000;

        let start = Instant::now();
        for _ in 0..iterations {
            gauge.add(1.0);
        }
        let elapsed = start.elapsed();

        println!(
            "Gauge add: {:.2} ns/op",
            elapsed.as_nanos() as f64 / iterations as f64
        );

        // Should be reasonably fast (CAS loop is more expensive - relaxed from 200ns to 300ns)
        assert!(elapsed.as_nanos() / iterations < 300);
        assert_eq!(gauge.get(), iterations as f64);
    }

    #[cfg_attr(not(feature = "bench-tests"), ignore)]
    #[test]
    fn bench_gauge_get() {
        let gauge = Gauge::with_value(42.5);
        let iterations = 100_000_000;

        let start = Instant::now();
        let mut sum = 0.0;
        for _ in 0..iterations {
            sum += gauge.get();
        }
        let elapsed = start.elapsed();

        println!(
            "Gauge get: {:.2} ns/op",
            elapsed.as_nanos() as f64 / iterations as f64
        );

        // Prevent optimization
        assert_eq!(sum, 42.5 * iterations as f64);

        // Should be under 50ns per get (relaxed from 20ns)
        assert!(elapsed.as_nanos() / iterations < 50);
    }
}