Files
codex/codex-rs/diagnostics/src/tests.rs
jif a7dcd20d38 Add process diagnostics snapshots (#37434)
## What changed

- Add a `codex-diagnostics` crate that snapshots the process ID, available
  resident-memory measurements, and registered process-wide gauges.
- Provide guards that update gauges for the lifetime of measured objects.
- Track live `CodexThread` instances with the `core.threads.live` gauge.

## Testing

- Add unit coverage for gauge registration, guard lifetimes, process memory
  snapshots, and live-thread reporting.

GitOrigin-RevId: 3236b086bd4ebe31ed4768ab87a5fa288b0891b0
2026-08-07 11:30:17 +00:00

59 lines
1.6 KiB
Rust

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);
}