open-standards/sandbox/sandbox-rs/crates/sandbox-meter/examples/meter_cmd.rs
Transaction Science 7f1e78577e tooling: meter-cmd (energy bracket) + gen-fixture (review fixtures)
Two small examples used to run and measure a real model-backed review:

- sandbox-meter meter_cmd: a `time`-like ENERGY bracket — measures the
  machine's IOReport energy over an external command's execution, plus an
  idle baseline of equal length so the workload's marginal draw is honest
  (machine-wide counters, the caveat printed not hidden). `cargo run
  --release --example meter_cmd -- <program> [args…]`.

- joule-code-reviewer gen-fixture: writes a `.joule/` review fixture (a
  signed spec + ops, one clean and one with a defect) so joule-review can be
  driven against a real model. `cargo run --example gen-fixture -- <dir>`.

Clippy clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-12 07:04:06 -04:00

46 lines
1.9 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! `meter-cmd` — a `time`-like energy bracket: measure the machine's energy
//! over the execution of an external command, with an idle baseline of equal
//! length so the workload's marginal draw is honest.
//!
//! Counters are machine-wide (not process-scoped) — that caveat is printed,
//! not hidden. On a quiet machine, `work − idle` approximates the workload.
//!
//! Run: `cargo run --release --example meter-cmd -- <program> [args…]`
use sandbox_meter::PowerMeter;
use std::process::Command;
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
let (program, rest) = args.split_first().expect("usage: meter-cmd <program> [args…]");
let mut meter = PowerMeter::detect();
eprintln!(
"meter-cmd: probes [{}]",
if meter.is_hardware() { meter.sources().join(", ") } else { "estimator".into() }
);
// measure the command (the workload inherits this process's environment)
let (status, work) = meter.measure(|| {
Command::new(program).args(rest).status().expect("spawn command")
});
// an idle baseline of the same wall length, for the marginal figure
let dur = work.wall;
let ((), idle) = meter.measure(|| std::thread::sleep(dur));
let work_j = work.total_uj() as f64 / 1e6;
let idle_j = idle.total_uj() as f64 / 1e6;
let marginal = (work_j - idle_j).max(0.0);
println!("\n== meter-cmd result ==");
println!("command exited: {status}");
println!("wall: {:.1} s", work.wall.as_secs_f64());
println!("machine (work): {work_j:.3} J ({:.2} W avg)", work.avg_watts());
println!("machine (idle): {idle_j:.3} J ({:.2} W avg)", idle.avg_watts());
println!("marginal: {marginal:.3} J (work − idle, the workload's honest share)");
println!(
"source: {} ({})",
work.source,
if work.fully_measured() { "hardware-measured" } else { "partly estimated" }
);
}