File size: 1,689 Bytes
9ebf6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::Gauge;
use super::snapshot;
use pretty_assertions::assert_eq;

static GUARD_GAUGE: Gauge = Gauge::new("diagnostics.tests.guard");
static REGISTERED_GAUGE: Gauge = Gauge::new("diagnostics.tests.registered");

#[test]
fn gauge_guards_follow_the_measured_lifetime() {
    let first = GUARD_GAUGE.track();
    let second = GUARD_GAUGE.track();
    let value = || {
        snapshot()
            .gauges
            .into_iter()
            .find(|gauge| gauge.name == "diagnostics.tests.guard")
            .expect("used gauge should be registered")
            .value
    };

    assert_eq!(value(), 2);
    drop(first);
    assert_eq!(value(), 1);
    drop(second);
    assert_eq!(value(), 0);
}

#[test]
fn snapshot_includes_process_memory_and_registers_gauges_once() {
    REGISTERED_GAUGE.increment();
    REGISTERED_GAUGE.increment();
    let diagnostics = snapshot();
    let registered = diagnostics
        .gauges
        .iter()
        .filter(|gauge| gauge.name == "diagnostics.tests.registered")
        .collect::<Vec<_>>();

    assert_eq!(diagnostics.process.id, std::process::id());
    assert_eq!(registered.len(), 1);
    assert_eq!(registered[0].value, 2);
    #[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
    assert!(
        diagnostics
            .process
            .resident_memory_bytes
            .is_some_and(|bytes| bytes > 0)
    );
    #[cfg(target_os = "macos")]
    assert!(
        diagnostics
            .process
            .physical_footprint_bytes
            .is_some_and(|bytes| bytes > 0)
    );
    #[cfg(not(target_os = "macos"))]
    assert_eq!(diagnostics.process.physical_footprint_bytes, None);
}