zenbench 0.1.9

Interleaved microbenchmarking with paired statistics, CI regression testing, and hardware-adaptive measurement
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
//! Fire-and-forget benchmark daemon.
//!
//! Spawns benchmark processes that run independently, write results to disk,
//! and can be monitored/killed via the CLI or MCP.

use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::SystemTime;

/// State file written by the daemon process.
const STATE_DIR: &str = ".zenbench";
const RUNS_DIR: &str = "runs";
#[allow(dead_code)]
const LOCK_FILE: &str = "bench.lock";

/// Status of a benchmark run.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum RunStatus {
    /// Queued but not started.
    Queued,
    /// Currently running.
    Running,
    /// Completed successfully.
    Completed,
    /// Failed with error.
    Failed(String),
    /// Killed (by user or auto-kill).
    Killed,
}

/// Metadata for a benchmark run (written to disk as JSON).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunState {
    pub id: String,
    pub pid: u32,
    pub git_hash: Option<String>,
    pub command: String,
    pub status: RunStatus,
    pub started_at: u64,
    pub finished_at: Option<u64>,
    pub result_path: Option<PathBuf>,
}

impl RunState {
    /// Create a new run state for a process about to start.
    pub fn new(id: String, command: String, git_hash: Option<String>) -> Self {
        let started_at = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            id,
            pid: std::process::id(),
            git_hash,
            command,
            status: RunStatus::Queued,
            started_at,
            finished_at: None,
            result_path: None,
        }
    }
}

/// The runs directory for storing daemon state.
pub fn runs_dir(project_root: &Path) -> PathBuf {
    project_root.join(STATE_DIR).join(RUNS_DIR)
}

/// Path to the cross-process lock file.
pub fn lock_path(project_root: &Path) -> PathBuf {
    project_root.join(STATE_DIR).join(LOCK_FILE)
}

/// List all known runs, with automatic state reconciliation.
///
/// Dead processes with result files are marked Completed.
/// Dead processes without result files are marked Failed.
pub fn list_runs(project_root: &Path) -> std::io::Result<Vec<RunState>> {
    let dir = runs_dir(project_root);
    if !dir.exists() {
        return Ok(Vec::new());
    }

    let mut runs = Vec::new();
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        let is_json = path.extension().is_some_and(|e| e == "json");
        let is_results = path
            .file_name()
            .is_some_and(|n| n.to_string_lossy().contains(".results."));
        if is_json && !is_results {
            if let Ok(data) = std::fs::read_to_string(&path) {
                if let Ok(mut state) = serde_json::from_str::<RunState>(&data) {
                    // Reconcile: detect processes that finished without updating state
                    if reconcile_state(project_root, &mut state) {
                        // Save the updated state back to disk
                        let _ = save_run_state(project_root, &state);
                    }
                    runs.push(state);
                }
            }
        }
    }

    runs.sort_by_key(|r| r.started_at);
    Ok(runs)
}

/// Save run state to disk.
pub fn save_run_state(project_root: &Path, state: &RunState) -> std::io::Result<()> {
    let dir = runs_dir(project_root);
    std::fs::create_dir_all(&dir)?;

    let path = dir.join(format!("{}.json", state.id));
    let json = serde_json::to_string_pretty(state).map_err(std::io::Error::other)?;
    std::fs::write(path, json)
}

/// Load a specific run state, with automatic reconciliation.
pub fn load_run_state(project_root: &Path, run_id: &str) -> std::io::Result<RunState> {
    let path = runs_dir(project_root).join(format!("{run_id}.json"));
    let data = std::fs::read_to_string(path)?;
    let mut state: RunState = serde_json::from_str(&data).map_err(std::io::Error::other)?;

    if reconcile_state(project_root, &mut state) {
        let _ = save_run_state(project_root, &state);
    }

    Ok(state)
}

/// Reconcile a run's status with reality.
///
/// If the run is marked Running/Queued but the process is dead,
/// check if results exist to determine Completed vs Failed.
/// Returns true if the state was modified.
fn reconcile_state(project_root: &Path, state: &mut RunState) -> bool {
    if state.status != RunStatus::Running && state.status != RunStatus::Queued {
        return false;
    }

    if is_process_alive(state.pid) {
        return false;
    }

    // Process is dead — figure out the outcome
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let has_results = state
        .result_path
        .as_ref()
        .is_some_and(|p| p.exists() && std::fs::metadata(p).is_ok_and(|m| m.len() > 10));

    if has_results {
        state.status = RunStatus::Completed;
    } else {
        // Check if stderr log exists and has error info
        let stderr_path = runs_dir(project_root).join(format!("{}.stderr.log", state.id));
        let error_msg = if stderr_path.exists() {
            // Read last 500 bytes for error context
            std::fs::read_to_string(&stderr_path)
                .ok()
                .and_then(|s| {
                    let trimmed = s.trim();
                    if trimmed.is_empty() {
                        None
                    } else {
                        let tail: String = trimmed
                            .chars()
                            .rev()
                            .take(500)
                            .collect::<String>()
                            .chars()
                            .rev()
                            .collect();
                        Some(tail)
                    }
                })
                .unwrap_or_else(|| "process exited without results".to_string())
        } else {
            "process exited without results".to_string()
        };
        state.status = RunStatus::Failed(error_msg);
    }
    state.finished_at = Some(now);
    true
}

/// Kill stale runs that were started with a different git hash.
///
/// Returns the number of runs killed.
pub fn kill_stale_runs(project_root: &Path, current_hash: &str) -> std::io::Result<usize> {
    let runs = list_runs(project_root)?;
    let mut killed = 0;

    for run in runs {
        if run.status == RunStatus::Running || run.status == RunStatus::Queued {
            let is_stale = run.git_hash.as_deref().is_some_and(|h| h != current_hash);

            if is_stale {
                // Try to kill the process
                if kill_process(run.pid) {
                    let mut updated = run;
                    updated.status = RunStatus::Killed;
                    updated.finished_at = Some(
                        SystemTime::now()
                            .duration_since(SystemTime::UNIX_EPOCH)
                            .unwrap_or_default()
                            .as_secs(),
                    );
                    save_run_state(project_root, &updated)?;
                    killed += 1;
                }
            }
        }
    }

    Ok(killed)
}

/// Kill a specific run by ID.
pub fn kill_run(project_root: &Path, run_id: &str) -> std::io::Result<bool> {
    let mut state = load_run_state(project_root, run_id)?;
    if (state.status == RunStatus::Running || state.status == RunStatus::Queued)
        && kill_process(state.pid)
    {
        state.status = RunStatus::Killed;
        state.finished_at = Some(
            SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        );
        save_run_state(project_root, &state)?;
        return Ok(true);
    }
    Ok(false)
}

/// Attempt to kill a process by PID. Returns true if the signal was sent.
fn kill_process(pid: u32) -> bool {
    // Cross-platform process kill
    #[cfg(unix)]
    {
        use std::process::Command;
        Command::new("kill")
            .args(["-TERM", &pid.to_string()])
            .status()
            .is_ok_and(|s| s.success())
    }

    #[cfg(windows)]
    {
        use std::process::Command;
        Command::new("taskkill")
            .args(["/PID", &pid.to_string(), "/F"])
            .status()
            .is_ok_and(|s| s.success())
    }

    #[cfg(not(any(unix, windows)))]
    {
        let _ = pid;
        false
    }
}

/// Check if a process is still running (not a zombie, not dead).
pub fn is_process_alive(pid: u32) -> bool {
    #[cfg(target_os = "linux")]
    {
        // On Linux, check /proc/<pid>/stat to detect zombies.
        // kill -0 succeeds for zombies, so we can't rely on it alone.
        let stat_path = format!("/proc/{pid}/stat");
        match std::fs::read_to_string(&stat_path) {
            Ok(stat) => {
                // Format: "pid (comm) state ..."
                // The state field is after the closing paren of comm
                if let Some(pos) = stat.rfind(')') {
                    let after = stat[pos + 1..].trim_start();
                    // State 'Z' = zombie, 'X'/'x' = dead
                    !after.starts_with('Z') && !after.starts_with('X') && !after.starts_with('x')
                } else {
                    false
                }
            }
            Err(_) => false, // /proc/<pid> doesn't exist → process is gone
        }
    }

    #[cfg(all(unix, not(target_os = "linux")))]
    {
        use std::process::Command;
        // On macOS/BSD, use ps to check state. 'Z' = zombie.
        let output = Command::new("ps")
            .args(["-p", &pid.to_string(), "-o", "state="])
            .output();
        match output {
            Ok(o) if o.status.success() => {
                let state = String::from_utf8_lossy(&o.stdout);
                let state = state.trim();
                !state.is_empty() && !state.starts_with('Z')
            }
            _ => false,
        }
    }

    #[cfg(windows)]
    {
        use std::process::Command;
        let output = Command::new("tasklist")
            .args(["/FI", &format!("PID eq {pid}"), "/NH"])
            .output();
        output.is_ok_and(|o| String::from_utf8_lossy(&o.stdout).contains(&pid.to_string()))
    }

    #[cfg(not(any(unix, windows)))]
    {
        let _ = pid;
        false
    }
}

/// Clean up finished runs older than the given age.
pub fn cleanup_old_runs(project_root: &Path, max_age_secs: u64) -> std::io::Result<usize> {
    let runs = list_runs(project_root)?;
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let mut cleaned = 0;
    for run in runs {
        if run.status != RunStatus::Running
            && run.status != RunStatus::Queued
            && now.saturating_sub(run.started_at) > max_age_secs
        {
            let state_path = runs_dir(project_root).join(format!("{}.json", run.id));
            let stderr_path = runs_dir(project_root).join(format!("{}.stderr.log", run.id));
            let _ = std::fs::remove_file(state_path);
            let _ = std::fs::remove_file(stderr_path);
            // Optionally remove the results file too
            if let Some(ref result_path) = run.result_path {
                let _ = std::fs::remove_file(result_path);
            }
            cleaned += 1;
        }
    }

    Ok(cleaned)
}

/// Spawn a benchmark binary as a detached background process.
///
/// The benchmark is expected to be a `cargo bench` target or a standalone binary
/// that writes results to a JSON file. The daemon records the run state and
/// auto-kills stale runs from previous git commits.
///
/// Returns the run ID and the Child handle. The caller SHOULD call
/// `child.wait()` to prevent zombie processes. If the caller exits
/// without waiting, the OS will reparent and reap the zombie.
pub fn spawn_detached(
    project_root: &Path,
    command: &str,
    args: &[&str],
) -> std::io::Result<(String, std::process::Child)> {
    let git_hash = crate::platform::git_commit_hash();

    // Auto-kill stale runs before spawning
    if let Some(ref hash) = git_hash {
        let killed = kill_stale_runs(project_root, hash)?;
        if killed > 0 {
            eprintln!("[zenbench] killed {killed} stale run(s) from previous commits");
        }
    }

    let run_id = format!(
        "{}-{:x}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs(),
        std::process::id()
    );

    // Build absolute paths so they work regardless of cwd
    let abs_project = std::fs::canonicalize(project_root)?;
    let result_dir = runs_dir(&abs_project);
    std::fs::create_dir_all(&result_dir)?;
    let result_path = result_dir.join(format!("{run_id}.results.json"));
    let stderr_path = result_dir.join(format!("{run_id}.stderr.log"));

    // Open stderr log file for the child process
    let stderr_file = std::fs::File::create(&stderr_path)?;

    // Spawn the process with stderr going to a log file (not a pipe!)
    // Piping stderr and not consuming it causes the child to hang when the buffer fills.
    let child = std::process::Command::new(command)
        .args(args)
        .current_dir(&abs_project)
        .env("ZENBENCH_RUN_ID", &run_id)
        .env("ZENBENCH_RESULT_PATH", &result_path)
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::from(stderr_file))
        .spawn()?;

    let mut state = RunState::new(
        run_id.clone(),
        format!("{command} {}", args.join(" ")),
        git_hash,
    );
    state.pid = child.id();
    state.status = RunStatus::Running;
    state.result_path = Some(result_path);
    save_run_state(&abs_project, &state)?;

    eprintln!("[zenbench] spawned run {run_id} (PID {})", child.id());
    Ok((run_id, child))
}

/// Spawn a benchmark and discard the child handle (fire-and-forget).
///
/// Use this when you don't need to wait for the process.
/// The zombie will be reaped when this process exits.
pub fn spawn_fire_and_forget(
    project_root: &Path,
    command: &str,
    args: &[&str],
) -> std::io::Result<String> {
    let (run_id, _child) = spawn_detached(project_root, command, args)?;
    Ok(run_id)
}

/// Wait for a specific run to complete.
///
/// Polls the process at the given interval. Returns the final RunState.
/// Times out after `timeout` duration.
pub fn wait_for_run(
    project_root: &Path,
    run_id: &str,
    poll_interval: std::time::Duration,
    timeout: std::time::Duration,
) -> std::io::Result<RunState> {
    let start = std::time::Instant::now();

    loop {
        let state = load_run_state(project_root, run_id)?;

        match &state.status {
            RunStatus::Completed | RunStatus::Killed => return Ok(state),
            RunStatus::Failed(_) => return Ok(state),
            RunStatus::Running | RunStatus::Queued => {
                if start.elapsed() >= timeout {
                    return Err(std::io::Error::other(format!(
                        "timed out waiting for run {run_id} after {:.0}s",
                        timeout.as_secs_f64()
                    )));
                }
                std::thread::sleep(poll_interval);
            }
        }
    }
}

/// Find the most recent run that has completed with results.
pub fn find_latest_with_results(project_root: &Path) -> std::io::Result<Option<RunState>> {
    let runs = list_runs(project_root)?;
    Ok(runs
        .into_iter()
        .rev()
        .find(|r| r.status == RunStatus::Completed && r.result_path.is_some()))
}

/// Check the environment for fire-and-forget mode.
///
/// If `ZENBENCH_RESULT_PATH` is set, the benchmark should save results
/// to that path when complete. Returns the path if set.
pub fn result_path_from_env() -> Option<PathBuf> {
    std::env::var("ZENBENCH_RESULT_PATH")
        .ok()
        .map(PathBuf::from)
}