Skip to content

refactor(bench): give the three benchmark tools one shared definition (#101) - #108

Merged
Andste82 merged 1 commit into
mainfrom
refactor/101-bench-single-source
Aug 9, 2026
Merged

Andste82 merged 1 commit into
mainfrom
refactor/101-bench-single-source

Conversation

@Andste82

@Andste82 Andste82 commented Aug 9, 2026

Copy link
Copy Markdown
Member

Fixes #101.

CORELIB_PLAN §10 wants bench, perf and run_callgrind.sh to follow one specification (BENCH_SPEC) so their numbers stay comparable across languages. The pieces that specification constrains were copied rather than shared.

What moved

  • bench/bench_common.hpp (new) — the stream adapters OStreamRaw/IStreamRaw (the harnesses' only view of the corelib's public surface) and the timing harness: cpu_now(), the cycle counter, kBlockSeconds with its justifying comment, calibrateBlock() and the ~1 s measured loop. bench.cpp and perf.cpp both include it; perf's PerfResult is now the shared MeasureResult, so the two tools cannot time differently.
  • The workload list lives once — a Workload table in bench.cpp (name, row label, setup, measured op, byte count). The timed table, the single-shot Callgrind mode and the new bench --list all walk it, so the strcmp chain can no longer disagree with what is listed. run_callgrind.sh populates WORKLOADS and its row labels from bench --list (name<TAB>label), and its second label() copy is gone. bench/CMakeLists.txt's run_bench_callgrind delegates to the script instead of repeating the names in a foreach.
  • A failed measurement is loud — run_cg() now checks the Callgrind exit status and prints the captured stderr, and a row with no summary: line or no BYTES= aborts the run. Previously a broken row died silently (stderr went to a discarded log) or would have printed a -, which looks exactly like a fresh table.

Numbers

Unchanged, measured against main on the same host:

Workload main this branch
encode: u64 array (1000) 38046 38046
encode: typical message 266 266
decode: u64 array (1000) 44839 44839
decode: typical message 1329 1329

Regression test

bench/test_bench_tools.sh, wired into CTest as test_bench_tools (skips when the build produced no bench binary). It fails on main — the first check, bench --list, does not exist there. It asserts:

  1. every published workload runs single-shot and reports BYTES=, and an unknown name is rejected with a diagnostic;
  2. neither run_callgrind.sh nor bench/CMakeLists.txt contains any published workload name or row label (no second copy of the list);
  3. bench.cpp and perf.cpp include bench_common.hpp and declare no OStreamRaw/IStreamRaw/cpu_now/kBlockSeconds of their own;
  4. run_callgrind.sh end to end against a stub valgrind — one row per published workload on success, and a non-zero exit carrying the failing run's diagnostic when the measurement fails. No Valgrind needed, runs in ~0.3 s.

Note on the issue text

The issue expected a rejected workload to print a row of dashes and exit 0. It does not: set -euo pipefail already turns the failing run_cg into an abort — but a completely silent one, mid-table, with the diagnostic in a discarded temp file. That half is fixed as described above rather than as reported.

README's Benchmarks and Build & test sections updated (new suite, bench --list, the single-source rule, and the CMake target's changed behaviour).

CORELIB_PLAN §10 requires `bench`, `perf` and `run_callgrind.sh` to follow one
specification so their numbers stay comparable across languages. The pieces that
specification constrains were copied instead of shared, so the three tools could
drift apart without anything noticing:

* `OStreamRaw` / `IStreamRaw` — the harnesses' only view of the corelib's public
  surface — were declared verbatim in both bench.cpp and perf.cpp.
* `cpu_now()`, `kBlockSeconds` and the doubling block-calibration loop, i.e.
  BENCH_SPEC's timing rules, were duplicated as well.
* The workload names lived in three places: the `strcmp` chain in bench.cpp, the
  `WORKLOADS` array plus a second copy in `label()` in run_callgrind.sh, and the
  `foreach` in bench/CMakeLists.txt.

The adapters and the whole measured loop now live once in bench/bench_common.hpp,
included by both harnesses. The workloads live once in a table in bench.cpp that
the timed run, the Callgrind single-shot mode and the new `bench --list` all walk;
run_callgrind.sh takes both the names and the row labels from that output, and the
run_bench_callgrind CMake target delegates to the script rather than repeating the
list. Measured Ir/op is unchanged on all four workloads.

run_callgrind.sh also stops swallowing a failed measurement: a Callgrind run that
exits non-zero, or a row with no instruction count or message size, now aborts
with the captured diagnostic instead of printing a table cell that reads `-`.

Regression test: bench/test_bench_tools.sh, wired into CTest as
`test_bench_tools`. It checks that every published workload runs and reports its
size, that an unknown name is rejected, that neither run_callgrind.sh nor
bench/CMakeLists.txt keeps a copy of the workload list, and that bench.cpp and
perf.cpp take the adapters and the timing harness from the shared header. It also
drives run_callgrind.sh end to end against a stub `valgrind`, so it needs no
Valgrind and still proves a failing run aborts the table.

Closes #101
@Andste82
Andste82 merged commit 35ecce2 into main Aug 9, 2026
7 checks passed
@Andste82
Andste82 deleted the refactor/101-bench-single-source branch August 9, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10: bench and perf duplicate the stream adapters and timing harness, and the workload list lives in three unchecked copies

1 participant