Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ CARGO_BUILD_JOBS=4 python3 scripts/footprint.py --rounds 8 --check --out .cache/

Pass `--baseline /path/to/previous/output` to alternate preserved baseline and
current binaries in fresh processes. The tool verifies baseline binary hashes,
the full compiler identity (including distribution) and `RUSTFLAGS`, then writes
the new baseline samples and summary under `baseline/` in the output. Each
the full compiler identity (including distribution) and `RUSTFLAGS`. After
measuring, it checks that both binaries ran the same workload: the call count,
the source bytes, and a digest of the ordered program names and sources, so an
edited or reordered fixture can't pass as a compiler change. It then writes the
new baseline samples and summary under `baseline/` in the output. Each
executable receives one unmeasured launch before the measured rounds so a
freshly copied binary does not skew round 0, and paired preparation ends on the
candidate so block 0 sees the same preceding-binary transition as later blocks.
Expand Down
60 changes: 59 additions & 1 deletion examples/footprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ impl Stage {
}
}

/// Identifies the ordered program names and sources, so a paired run can
/// confirm that both revisions measured the same workload.
fn programs_digest(programs: &[(&str, &str)]) -> String {
// FNV-1a, with each name and source preceded by its length so bytes moved
// across a boundary change the digest.
let mut hash = 0xcbf29ce484222325_u64;
for (name, source) in programs {
for part in [name.as_bytes(), source.as_bytes()] {
for byte in (part.len() as u64).to_le_bytes().iter().chain(part) {
hash = (hash ^ u64::from(*byte)).wrapping_mul(0x100000001b3);
}
}
}
format!("{hash:016x}")
}

fn registered(engine: &mut Engine) -> vibescript::Result<Vec<Capability>> {
let mut capabilities = Vec::new();
for name in ["log_event", "metric", "lookup", "publish"] {
Expand Down Expand Up @@ -168,7 +184,49 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!(
"{}",
serde_json::json!({"calls": 1000, "steps": steps, "tracked_peak_bytes": peak,
"tracked_retained_bytes_sum": retained, "source_bytes": programs::PROGRAMS.iter().map(|(_, source)| source.len()).sum::<usize>()})
"tracked_retained_bytes_sum": retained, "source_bytes": programs::PROGRAMS.iter().map(|(_, source)| source.len()).sum::<usize>(),
"programs_digest": programs_digest(&programs::PROGRAMS)})
);
Ok(())
}

#[cfg(test)]
mod tests {
use super::programs_digest;

const PROGRAMS: [(&str, &str); 2] = [
("a.vibe", "def run\n 1\nend\n"),
("b.vibe", "def run\n 2\nend\n"),
];

#[test]
fn digest_changes_with_a_same_length_source_edit() {
let edited = [PROGRAMS[0], ("b.vibe", "def run\n 3\nend\n")];
assert_ne!(programs_digest(&PROGRAMS), programs_digest(&edited));
}

#[test]
fn digest_changes_with_program_order() {
let reordered = [PROGRAMS[1], PROGRAMS[0]];
assert_ne!(programs_digest(&PROGRAMS), programs_digest(&reordered));
}

#[test]
fn digest_changes_with_a_renamed_program() {
let renamed = [PROGRAMS[0], ("c.vibe", PROGRAMS[1].1)];
assert_ne!(programs_digest(&PROGRAMS), programs_digest(&renamed));
}

#[test]
fn digest_changes_when_bytes_cross_a_boundary() {
// Each pair concatenates to the same bytes.
assert_ne!(
programs_digest(&[("ab", "c")]),
programs_digest(&[("a", "bc")])
);
assert_ne!(
programs_digest(&[("a", "xy"), ("b", "z")]),
programs_digest(&[("a", "x"), ("yb", "z")])
);
}
}
5 changes: 3 additions & 2 deletions scripts/footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ def main():
if baseline:
baseline_summary = json.loads((baseline / "summary.json").read_text())
previous = summarize(baseline_samples, baseline_summary["stripped_vibes_bytes"], paired=True)
for key in ["calls", "source_bytes"]:
assert previous["workload"][key] == summary["workload"][key], "baseline workload differs"
for key in ["calls", "source_bytes", "programs_digest"]:
baseline_value = previous["workload"].get(key)
assert baseline_value == summary["workload"][key], f"baseline workload {key} {baseline_value!r} differs from {summary['workload'][key]!r}"
baseline_environment["measurement_command"] = sys.argv
if "cpu_affinity" in environment:
baseline_environment["measurement_cpu_affinity"] = environment["cpu_affinity"]
Expand Down
42 changes: 42 additions & 0 deletions scripts/test_footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,21 @@ def setup_fake_repo(root: Path, footprint_body: str) -> dict[str, str]:
"tracked_peak_bytes": 20,
"tracked_retained_bytes_sum": 30,
"source_bytes": 40,
"programs_digest": "0123456789abcdef",
}))
""".lstrip()


def workload_body(programs_digest: str | None) -> str:
"""A fake footprint binary whose workload reports the given programs digest."""
workload = {"calls": 1000, "steps": 10, "tracked_peak_bytes": 20, "tracked_retained_bytes_sum": 30, "source_bytes": 40}
if programs_digest is not None:
workload["programs_digest"] = programs_digest
stage = {"stage": "compiled_1", "allocations": 100, "allocated_bytes": 1000, "live_bytes": 500,
"peak_live_bytes": 800, "rss_bytes": 4096, "elapsed_ns": 400_000}
return f"import json\nprint(json.dumps({stage!r}))\nprint(json.dumps({workload!r}))\n"


class FootprintDriverTests(unittest.TestCase):
def test_budget_failure_reports_both_captured_streams(self):
with tempfile.TemporaryDirectory() as directory:
Expand Down Expand Up @@ -222,6 +233,37 @@ def test_paired_run_prepares_both_binaries_and_removes_first_launch_spike(self):
self.assertEqual(json.loads((root / "paired/summary.json").read_text()), cand_summary)
self.assertEqual(json.loads((root / "paired/baseline/summary.json").read_text()), base_summary)

def test_paired_run_rejects_a_baseline_with_different_programs(self):
# An edited fixture of the same length keeps the call count and source
# bytes, so only the programs digest tells the revisions apart. A
# baseline from before the digest was recorded can't show it matches.
for baseline_digest in ["fedcba9876543210", None]:
with self.subTest(baseline_digest=baseline_digest), tempfile.TemporaryDirectory() as directory:
root = Path(directory)
env = setup_fake_repo(root, workload_body(baseline_digest))
base_run = subprocess.run(
[sys.executable, "scripts/footprint.py", "--out", "base", "--rounds", "2"],
cwd=root,
env=env,
capture_output=True,
text=True,
)
self.assertEqual(base_run.returncode, 0, base_run.stdout + base_run.stderr)
write_script(root / "target/release/examples/footprint", workload_body("0123456789abcdef"))

paired_run = subprocess.run(
[sys.executable, "scripts/footprint.py", "--out", "paired", "--baseline", "base", "--rounds", "2"],
cwd=root,
env=env,
capture_output=True,
text=True,
)
self.assertNotEqual(paired_run.returncode, 0, paired_run.stdout + paired_run.stderr)
self.assertIn(
f"baseline workload programs_digest {baseline_digest!r} differs from '0123456789abcdef'",
paired_run.stderr,
)


if __name__ == "__main__":
unittest.main()
Loading