statumen 0.1.0

Statumen whole-slide image reader
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
//! `release_gate` — runs parity + bench_driver across the frozen V1 corpus and
//! writes canonical scoreboard artifacts.
//!
//! Usage:
//!   release_gate [manifest-path]

#[allow(dead_code)]
mod bench_common;

use bench_common::workload_specs;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

const DEFAULT_MANIFEST: &str = "docs/superpowers/perf/v1-corpus.json";
const SCOREBOARD_JSON: &str = "docs/superpowers/perf/release-scoreboard.json";
const SCOREBOARD_MD: &str = "docs/superpowers/perf/release-scoreboard.md";
const RAW_DIR: &str = "docs/superpowers/perf/raw/release-gate";
const DEBUG_BENCH_OVERRIDE_ENV: &str = "WSI_ALLOW_DEBUG_BENCH";

#[derive(Debug, Deserialize)]
struct CorpusManifest {
    version: u32,
    slides: Vec<CorpusSlide>,
}

#[derive(Debug, Deserialize)]
struct CorpusSlide {
    id: String,
    format: String,
    path: String,
}

#[derive(Debug, Serialize)]
struct ParitySummary {
    status: String,
    command: Vec<String>,
    output_path: String,
}

#[derive(Debug, Serialize)]
struct WorkloadSummary {
    name: String,
    status: String,
    output_path: String,
    summary: Value,
}

#[derive(Debug, Serialize)]
struct SlideSummary {
    id: String,
    format: String,
    resolved_path: String,
    parity: ParitySummary,
    workloads: Vec<WorkloadSummary>,
}

#[derive(Debug, Serialize)]
struct ReleaseSummary {
    version: u32,
    manifest_path: String,
    overall_status: String,
    slides: Vec<SlideSummary>,
}

fn main() {
    if let Err(err) = run() {
        eprintln!("{err}");
        std::process::exit(1);
    }
}

fn run() -> Result<(), String> {
    ensure_release_profile("release_gate")?;

    let args: Vec<String> = std::env::args().collect();
    if args.len() > 2 {
        return Err("usage: release_gate [manifest-path]".into());
    }

    let workspace_root = workspace_root()?;
    let manifest_path = if let Some(arg) = args.get(1) {
        workspace_root.join(arg)
    } else {
        workspace_root.join(DEFAULT_MANIFEST)
    };
    let manifest: CorpusManifest = serde_json::from_slice(
        &fs::read(&manifest_path)
            .map_err(|err| format!("failed to read {}: {err}", manifest_path.display()))?,
    )
    .map_err(|err| format!("failed to parse {}: {err}", manifest_path.display()))?;

    let raw_dir = workspace_root.join(RAW_DIR);
    fs::create_dir_all(&raw_dir)
        .map_err(|err| format!("failed to create {}: {err}", raw_dir.display()))?;

    let bench_driver = ensure_sibling_binary(&workspace_root, "bench_driver")?;
    let mut slides = Vec::with_capacity(manifest.slides.len());
    let mut overall_ok = true;

    for slide in &manifest.slides {
        let resolved = resolve_manifest_path(&workspace_root, &slide.path)?;
        let parity_output_path = raw_dir.join(format!("{}-parity.txt", slide.id));
        let parity = run_parity_check(&workspace_root, &resolved, &parity_output_path)?;
        overall_ok &= parity.status == "pass";

        let mut workloads = Vec::with_capacity(workload_specs().len());
        for spec in workload_specs() {
            let output_path = raw_dir.join(format!("{}-{}.json", slide.id, spec.name));
            let summary = run_bench_driver(&bench_driver, &resolved, spec.name, &output_path)?;
            overall_ok &= summary.status == "pass";
            workloads.push(summary);
        }

        slides.push(SlideSummary {
            id: slide.id.clone(),
            format: slide.format.clone(),
            resolved_path: resolved.display().to_string(),
            parity,
            workloads,
        });
    }

    let summary = ReleaseSummary {
        version: manifest.version,
        manifest_path: manifest_path.display().to_string(),
        overall_status: if overall_ok { "pass" } else { "fail" }.into(),
        slides,
    };

    let json_path = workspace_root.join(SCOREBOARD_JSON);
    let md_path = workspace_root.join(SCOREBOARD_MD);
    fs::write(
        &json_path,
        serde_json::to_vec_pretty(&summary).map_err(|err| err.to_string())?,
    )
    .map_err(|err| format!("failed to write {}: {err}", json_path.display()))?;
    fs::write(&md_path, render_markdown(&summary))
        .map_err(|err| format!("failed to write {}: {err}", md_path.display()))?;

    println!(
        "{}",
        serde_json::to_string_pretty(&summary).map_err(|err| err.to_string())?
    );
    if overall_ok {
        Ok(())
    } else {
        Err("release gate failed".into())
    }
}

fn ensure_release_profile(tool: &str) -> Result<(), String> {
    if cfg!(debug_assertions) && std::env::var_os(DEBUG_BENCH_OVERRIDE_ENV).is_none() {
        return Err(format!(
            "{tool} must be run with --release for meaningful perf results; set {DEBUG_BENCH_OVERRIDE_ENV}=1 only for local smoke tests"
        ));
    }
    Ok(())
}

fn workspace_root() -> Result<PathBuf, String> {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("..")
        .canonicalize()
        .map_err(|err| format!("failed to resolve workspace root: {err}"))
}

fn ensure_sibling_binary(workspace_root: &Path, name: &str) -> Result<PathBuf, String> {
    let exe = std::env::current_exe().map_err(|err| format!("current_exe failed: {err}"))?;
    let dir = exe
        .parent()
        .ok_or_else(|| format!("current_exe has no parent: {}", exe.display()))?;
    let binary_name = if cfg!(windows) {
        format!("{name}.exe")
    } else {
        name.to_string()
    };
    let path = dir.join(binary_name);
    if path.is_file() {
        return Ok(path);
    }

    let status = Command::new("cargo")
        .current_dir(workspace_root)
        .args({
            let mut args = vec![
                "build",
                "-p",
                "statumen",
                "--features",
                "bench openslide-bench",
            ];
            if !cfg!(debug_assertions) {
                args.push("--release");
            }
            args.push("--bin");
            args.push(name);
            args
        })
        .status()
        .map_err(|err| format!("failed to build {name}: {err}"))?;
    if !status.success() {
        return Err(format!("cargo build failed for {name}"));
    }
    if path.is_file() {
        Ok(path)
    } else {
        Err(format!("missing benchmark binary {}", path.display()))
    }
}

fn resolve_manifest_path(workspace_root: &Path, raw: &str) -> Result<PathBuf, String> {
    let path = if let Some(downloads_rel) = raw.strip_prefix("downloads/") {
        let home = std::env::var_os("HOME").ok_or("HOME is not set")?;
        PathBuf::from(home).join("Downloads").join(downloads_rel)
    } else {
        workspace_root.join(raw)
    };
    let canonical = fs::canonicalize(&path)
        .map_err(|err| format!("failed to resolve {}: {err}", path.display()))?;
    if !canonical.is_file() {
        return Err(format!(
            "resolved path is not a file: {}",
            canonical.display()
        ));
    }
    Ok(canonical)
}

fn run_parity_check(
    workspace_root: &Path,
    slide_path: &Path,
    output_path: &Path,
) -> Result<ParitySummary, String> {
    let joined = std::env::join_paths([slide_path])
        .map_err(|err| format!("failed to join parity path list: {err}"))?;
    let output = Command::new("cargo")
        .current_dir(workspace_root)
        .args([
            "test",
            "-p",
            "statumen",
            "--test",
            "openslide_compare",
            "--",
            "--nocapture",
        ])
        .env("STATUMEN_OPENSLIDE_COMPARE_PATHS", joined)
        .output()
        .map_err(|err| format!("failed to run openslide_compare: {err}"))?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let mut combined = String::new();
    if !stdout.is_empty() {
        combined.push_str(&stdout);
        if !stderr.is_empty() {
            combined.push('\n');
        }
    }
    if !stderr.is_empty() {
        combined.push_str(&stderr);
    }
    fs::write(output_path, combined)
        .map_err(|err| format!("failed to write {}: {err}", output_path.display()))?;

    Ok(ParitySummary {
        status: if output.status.success() {
            "pass"
        } else {
            "fail"
        }
        .into(),
        command: vec![
            "cargo".into(),
            "test".into(),
            "-p".into(),
            "statumen".into(),
            "--test".into(),
            "openslide_compare".into(),
            "--".into(),
            "--nocapture".into(),
        ],
        output_path: output_path.display().to_string(),
    })
}

fn run_bench_driver(
    bench_driver: &Path,
    slide_path: &Path,
    workload: &str,
    output_path: &Path,
) -> Result<WorkloadSummary, String> {
    let output = Command::new(bench_driver)
        .arg(slide_path)
        .arg(workload)
        .output()
        .map_err(|err| format!("failed to run bench_driver for {workload}: {err}"))?;
    fs::write(output_path, &output.stdout)
        .map_err(|err| format!("failed to write {}: {err}", output_path.display()))?;

    let summary: Value = serde_json::from_slice(&output.stdout)
        .map_err(|err| format!("invalid bench_driver json for {workload}: {err}"))?;
    let status = summary["comparison"]["status"]
        .as_str()
        .unwrap_or(if output.status.success() {
            "pass"
        } else {
            "fail"
        })
        .to_string();

    Ok(WorkloadSummary {
        name: workload.to_string(),
        status,
        output_path: output_path.display().to_string(),
        summary,
    })
}

fn render_markdown(summary: &ReleaseSummary) -> String {
    let mut out = String::new();
    out.push_str("# Release Scoreboard\n\n");
    out.push_str(&format!(
        "Status: **{}**\n\n",
        summary.overall_status.to_uppercase()
    ));
    out.push_str(&format!("Manifest: `{}`\n\n", summary.manifest_path));

    for slide in &summary.slides {
        out.push_str(&format!("## {} ({})\n\n", slide.id, slide.format));
        out.push_str(&format!("- Path: `{}`\n", slide.resolved_path));
        out.push_str(&format!(
            "- Parity: **{}** (`{}`)\n\n",
            slide.parity.status.to_uppercase(),
            slide.parity.output_path
        ));
        out.push_str("| workload | status | statumen p50 | OpenSlide p50 | Iris p50 | statumen p99 | OpenSlide p99 | Iris p99 | OpenSlide RSS ratio | Iris RSS ratio |\n");
        out.push_str("|---|---|---:|---:|---:|---:|---:|---:|---:|---:|\n");
        for workload in &slide.workloads {
            let wsi = library_summary(&workload.summary, "statumen");
            let openslide = library_summary(&workload.summary, "openslide");
            let iris = library_summary(&workload.summary, "iris");
            let openslide_rss_ratio = ratio_string(
                wsi.and_then(|lib| lib.get("median_peak_rss_bytes"))
                    .and_then(Value::as_u64),
                openslide
                    .and_then(|lib| lib.get("median_peak_rss_bytes"))
                    .and_then(Value::as_u64),
            );
            let iris_rss_ratio = ratio_string(
                wsi.and_then(|lib| lib.get("median_peak_rss_bytes"))
                    .and_then(Value::as_u64),
                iris.and_then(|lib| lib.get("median_peak_rss_bytes"))
                    .and_then(Value::as_u64),
            );
            out.push_str(&format!(
                "| {} | {} | {} | {} | {} | {} | {} | {} | {} | {} |\n",
                workload.name,
                workload.status,
                value_string(
                    wsi.and_then(|lib| lib.get("median_p50_us"))
                        .and_then(Value::as_u64)
                ),
                value_string(
                    openslide
                        .and_then(|lib| lib.get("median_p50_us"))
                        .and_then(Value::as_u64)
                ),
                value_string(
                    iris.and_then(|lib| lib.get("median_p50_us"))
                        .and_then(Value::as_u64)
                ),
                value_string(
                    wsi.and_then(|lib| lib.get("median_p99_us"))
                        .and_then(Value::as_u64)
                ),
                value_string(
                    openslide
                        .and_then(|lib| lib.get("median_p99_us"))
                        .and_then(Value::as_u64)
                ),
                value_string(
                    iris.and_then(|lib| lib.get("median_p99_us"))
                        .and_then(Value::as_u64)
                ),
                openslide_rss_ratio,
                iris_rss_ratio,
            ));
        }
        out.push('\n');
    }

    out
}

fn library_summary<'a>(summary: &'a Value, name: &str) -> Option<&'a Value> {
    summary["libraries"]
        .as_array()?
        .iter()
        .find(|entry| entry["library"].as_str() == Some(name))
}

fn value_string(value: Option<u64>) -> String {
    value
        .map(|v| format!("{v}us"))
        .unwrap_or_else(|| "-".into())
}

fn ratio_string(left: Option<u64>, right: Option<u64>) -> String {
    match (left, right) {
        (Some(left), Some(right)) if right > 0 => format!("{:.2}x", left as f64 / right as f64),
        _ => "-".into(),
    }
}