use std::io::Write;
use std::path::Path;
use std::process::Command;
use std::time::{Duration, Instant};
fn scc() -> &'static str {
env!("CARGO_BIN_EXE_scc")
}
fn workdir(tmp: &Path) -> std::path::PathBuf {
tmp.join("repo")
}
fn run_ok(dir: &Path, args: &[&str]) -> String {
let out = Command::new(scc())
.args(args)
.current_dir(dir)
.output()
.expect("scc binary runs");
assert!(
out.status.success(),
"`scc {}` failed: {}",
args.join(" "),
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).to_string()
}
fn generate(dir: &Path, files: usize, lines: usize) -> usize {
std::fs::create_dir_all(dir).unwrap();
let mut loc = 0;
for i in 0..files {
let name = format!("mod_{i:04}");
let mut body = format!("# module {name}\n");
if i > 0 {
body.push_str(&format!("from mod_{:04} import helper\n", i - 1));
}
let mut line = 3;
for s in 0..(lines / 10).max(2) {
body.push_str(&format!(
"def func_{s:03}(a, b):\n r = a + {s}\n if r > 0:\n return helper(r)\n return r\n"
));
line += 4;
}
while line < lines {
body.push_str("# pad\n");
line += 1;
}
loc += line;
let mut f = std::fs::File::create(dir.join(format!("{name}.py"))).unwrap();
f.write_all(body.as_bytes()).unwrap();
}
loc
}
fn cold_index_once() -> (Duration, usize, String) {
let repo = tempfile::TempDir::new().unwrap();
std::fs::create_dir_all(workdir(repo.path())).unwrap();
let loc = generate(&workdir(repo.path()), 200, 250);
let start = Instant::now();
run_ok(&workdir(repo.path()), &["index", "--quiet"]);
let elapsed = start.elapsed();
let status = run_ok(&workdir(repo.path()), &["status"]);
(elapsed, loc, status)
}
#[test]
fn cold_index_50k_loc_under_30s() {
let bound = Duration::from_secs(180);
let mut attempts = Vec::new();
for i in 1..=2 {
let (elapsed, loc, status) = cold_index_once();
assert!(loc >= 50_000, "generated {loc} LOC");
attempts.push(elapsed);
if elapsed < bound {
eprintln!("50k LOC cold index: {elapsed:?} (attempt {i}; bound {bound:?})");
assert!(status.contains("relationships:"), "{status}");
return;
}
eprintln!("50k LOC cold index attempt {i} over bound: {elapsed:?}");
}
panic!(
"cold index of 50k LOC exceeded {bound:?} on all attempts {attempts:?}"
);
}
#[test]
fn entrypoint_timings_audit() {
let repo = tempfile::TempDir::new().unwrap();
std::fs::create_dir_all(workdir(repo.path())).unwrap();
generate(&workdir(repo.path()), 50, 100);
let dir = workdir(repo.path());
run_ok(&dir, &["index", "--quiet"]);
let cmds: &[&[&str]] = &[
&["index", "--quiet"],
&["status"],
&["important"],
&["surface"],
&["surface", "--task", "rename the helper"],
&["context", "task", "rename the helper"],
&["context", "startup"],
&["atlas"],
&["components"],
&["flows"],
&["verify"],
&["query", "helper"],
&["impact", "mod_0000.py"],
&["export", "system-ir.json"],
&["diff", "--help"],
&["history"],
];
for cmd in cmds {
let start = Instant::now();
let out = std::process::Command::new(scc())
.args(*cmd)
.current_dir(&dir)
.output()
.unwrap_or_else(|e| panic!("{cmd:?} failed to spawn: {e}"));
let elapsed = start.elapsed();
assert!(
out.status.success(),
"{cmd:?} exited {}: {}",
out.status,
String::from_utf8_lossy(&out.stderr)
);
eprintln!(
"PERF-AUDIT cmd={:?} ms={}",
cmd.join(" "),
elapsed.as_millis()
);
}
}
#[test]
#[ignore]
fn cold_index_250k_loc() {
let repo = tempfile::TempDir::new().unwrap();
std::fs::create_dir_all(workdir(repo.path())).unwrap();
let loc = generate(&workdir(repo.path()), 1000, 250);
assert!(loc >= 250_000, "generated {loc} LOC");
let start = Instant::now();
run_ok(&workdir(repo.path()), &["index", "--quiet"]);
let elapsed = start.elapsed();
assert!(
elapsed.as_secs() < 120,
"cold index of {loc} LOC took {elapsed:?} (120s bound)"
);
eprintln!("250k LOC cold index: {elapsed:?}");
}