diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 1a65d3bf..1f5ca88c 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -46,11 +46,15 @@ jobs: strategy: fail-fast: false # `requires`: what the example needs beyond benchx and pyperf, for `uv pip install`. + # arrow-demo and archery-local build a C++ suite with CMake: the runner has cmake and a C++ + # compiler, and CMake fetches Google Benchmark. matrix: include: - example: pyperf-suite - example: matmul-params requires: numpy torch --index-url https://download.pytorch.org/whl/cpu + - example: arrow-demo + - example: archery-local env: EXAMPLE: ${{ matrix.example }} steps: diff --git a/docs/examples.md b/docs/examples.md index 6fff1712..9e413705 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -39,6 +39,10 @@ in this repository. `bx bench`, the local store, and `bx series` / `bx history`. - [`matmul-params`](examples/matmul-params.md) — one benchmark function with several parameter combinations (sizes, numpy and torch), and a numpy-vs-torch comparison with `bx compare`. +- [`arrow-demo`](examples/arrow-demo.md) — CMake + Google Benchmark walkthrough covering the + `revisions` and `environments` comparison profiles on a small C++ suite, with the local store. +- [`archery-local`](examples/archery-local.md) — target provider walkthrough + (`benchx.conf.json` and `@CONFIG` targets): one commit built plain and hardened, and compared. ## Additional, cookbook-style examples @@ -51,9 +55,5 @@ Standalone, runnable examples of local usage live in a separate repository, tracked over a series of commits, and one varied by an environment variable. - `examples/pandas-asv` -- cross-environment comparison run of a specific benchmark from the pandas ASV suite. Also shows how to run existing ASV benchmarks. -- `examples/arrow/demo/run.sh` — CMake + Google Benchmark walkthrough covering - comparison profiles on a small C++ suite. -- `examples/arrow/archery-local` — target provider walkthrough - (`benchx.conf.json` and `@CONFIG` targets). See [benchx](benchx.md) for installation and the full `bx` CLI reference. diff --git a/examples/README.md b/examples/README.md index 6ee4376f..01fa0797 100644 --- a/examples/README.md +++ b/examples/README.md @@ -41,6 +41,8 @@ Each directory here is an example: a `README.md` and the files it uses. CI execu link to a Markdown page that does not exist fails `zensical build --strict`. - Benchmarks run in the checkout, so the example measures the benchx repository as the tree. Put the output in an ignored directory (`out/`, as `pyperf-suite` does). +- A directory here with no `README.md` is not an example, and the documentation tooling skips it. It holds + files that examples share: `arrow-suite` is the C++ suite that `arrow-demo` and `archery-local` copy. ## Adding one diff --git a/examples/archery-local/.gitignore b/examples/archery-local/.gitignore new file mode 100644 index 00000000..e96b7d6e --- /dev/null +++ b/examples/archery-local/.gitignore @@ -0,0 +1 @@ +_work/ diff --git a/examples/archery-local/README.md b/examples/archery-local/README.md new file mode 100644 index 00000000..3d725e47 --- /dev/null +++ b/examples/archery-local/README.md @@ -0,0 +1,461 @@ +# archery-local: a target provider, and what a build option costs + +One commit, built two ways (plain and hardened), compared with noise-aware verdicts, where the +comparison says what was built and how. It follows the Arrow local story, +["Measure what a build option costs"](https://github.com/benchx-dev/benchx/blob/main/docs/user_stories/apache-arrow-local-benchmarking.md), +on a small stand-in suite: a quiet and a noisy Google Benchmark case, where hardening adds about +3% of work. + +It exercises the target-provider contract of +[system-decomposition.md](https://github.com/benchx-dev/benchx/blob/main/docs/design/system-decomposition.md) §3.4: + +- **You are the user, and the provider is your build script.** `provider.sh` configures and builds + with CMake, as `archery` or a contributor would. benchx builds nothing. +- **The provider tells benchx what it built** in a *target description*: the build directory and its + source, plus two pieces of text you own, `how_built` (the exact commands) and `activation` (what to + run to enter the environment, with the `shell` it is written for). Either may be empty. +- **You activate the environment yourself** (`source activate.sh`). benchx never runs `activation`. + It records the text, as given, in the work order and in every result's `provenance.info`, beside the + allowlisted environment variables the runner actually inherited. If the two disagree, both stay and + nothing judges. + + +## What you need + +```sh noexec +python -m venv .venv && . .venv/bin/activate +pip install benchx[parquet] +``` + +You also need `cmake`, a C++ compiler and `git`, and network access the first time (CMake fetches +Google Benchmark v1.9.1 unless it is installed). The whole example takes a few minutes. + +Everything it creates is in `_work/`, including the local store and the logs of the long commands +(a command whose output goes to a log prints the log if it fails). Remove what an earlier run left, +so that the example can run again: + +```sh +$ rm -rf _work +$ mkdir _work +``` + + +## The project file, the provider and the environment + +The project file, `benchx.conf.json`, names the provider command, the harness and its suite, the +project, the comparison profile, and where the store and the results go: + +```sh +$ cat benchx.conf.json +``` + +```json +{ + "schema_version": "benchx/config/0.1.0", + "project": "archery-local", + "provider": { "command": ["./provider.sh"] }, + "adapter": { + "name": "google-benchmark", + "suite": "demo-bench", + "quantities": ["wall-time"], + "precision": { "repetitions": 5 } + }, + "paths": { "store": "_work/store.parquet", "results": "_work/results" }, + "compare": { "profile": "environments" } +} +``` + +The provider is your build script. It reads one JSON request on stdin (`describe` or `prepare`, and +the configuration `plain` or `hardened`) and answers with one target description on stdout. Everything +else, above all the build's own output, goes to stderr. `how_built` is assembled from the very commands +the script runs, so the text cannot drift from what happened: + +```sh +$ cat provider.sh +``` + +```text +#!/usr/bin/env bash +# The target provider of this example (system-decomposition.md §3.4). +# +# It is the user's own build script, speaking the provider contract: one JSON +# request on stdin (schemas/target-provider-request/0.1.0), one target +# description on stdout (schemas/target-description/0.1.0). Everything else, +# above all the build's own output, goes to stderr. +# +# describe reports a target that exists and builds nothing; exit 3 if none +# prepare configures and builds it, with CMake, as Arrow's contributors do +# (an incremental no-op when it is already built), then reports it +# +# Configurations: `plain`, and `hardened` (-DDEMO_HARDENED=ON), the same +# commit built two ways, like the -DARROW_HARDENING=ON story. +# +# `how_built` is assembled from the very commands this script runs, so the +# text cannot drift from what happened. benchx never runs any of it. +set -euo pipefail +HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +WORK=${BENCHX_EXAMPLE_WORK:-$HERE/_work} +export GIT_AUTHOR_NAME=demo GIT_AUTHOR_EMAIL=demo@example.org +export GIT_COMMITTER_NAME=demo GIT_COMMITTER_EMAIL=demo@example.org + +request=$(cat) +field() { python3 -c 'import json,sys; print(json.load(sys.stdin).get(sys.argv[1]) or "")' "$1" <<<"$request"; } +operation=$(field operation) +configuration=$(field configuration) +case "$configuration" in + plain) extra=() ;; + hardened) extra=(-DDEMO_HARDENED=ON) ;; + *) echo "provider: unknown configuration '$configuration' (plain or hardened)" >&2; exit 2 ;; +esac +SRC=$WORK/src +BUILD=$WORK/build-$configuration + +describe() { + [[ -f $BUILD/.benchx-target.json ]] || { echo "provider: $configuration is not built; use prepare" >&2; exit 3; } + cat "$BUILD/.benchx-target.json" +} + +if [[ $operation == describe ]]; then describe; exit 0; fi + +HOW="" +step() { # step : record it in how_built, show it, run it (output to stderr) + local line="$*" + HOW+="$line"$'\n' + printf '\033[2m provider$ %s\033[0m\n' "$line" >&2 + "$@" >&2 +} + +if [[ ! -d $SRC/.git ]]; then + mkdir -p "$WORK" + cp -R "$HERE/../arrow-suite" "$SRC" + git -C "$SRC" init -q + git -C "$SRC" remote add origin https://example.org/archery-local/demo-suite.git + git -C "$SRC" add -A + git -C "$SRC" commit -qm "demo-suite" +fi +mkdir -p "$WORK/deps/lib" # stand-in for the dependencies activate.sh points at + +step cmake -S "$SRC" -B "$BUILD" -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR="$WORK/_deps" ${extra[@]+"${extra[@]}"} +step cmake --build "$BUILD" -j + +BUILD=$BUILD SRC=$SRC HOW=$HOW CONFIGURATION=$configuration HERE=$HERE python3 - >"$BUILD/.benchx-target.json" <<'PY' +import json, os +print(json.dumps({ + "schema_version": "benchx/target-description/0.1.0", + "target": {"kind": "build", "path": os.environ["BUILD"], + "source": {"uri": "https://example.org/archery-local/demo-suite.git", "type": "git"}, + "source_dir": os.environ["SRC"]}, + "how_built": os.environ["HOW"].rstrip("\n"), + "activation": "source " + os.path.join(os.environ["HERE"], "activate.sh"), + "shell": "bash", + "provider": {"name": "provider.sh", "version": "1"}, +}, indent=1)) +PY +describe # the sidecar just written, which stays with the build directory +``` + +`activate.sh` is the environment the benchmarks are meant to run in. In the Arrow story this is where +you would load a toolchain or point at the dependencies you built. Here it only puts a stand-in +dependencies directory on the library search path: + +```sh +$ cat activate.sh +``` + +```text +# The environment this example's benchmarks are meant to run in. In the Arrow +# story this is where you would load a toolchain or point at the dependencies +# you built; here it only puts a stand-in "deps" directory on the library +# search path. You source it yourself, before invoking bx: benchx never runs +# it, it records the text that says you did (the target description's +# `activation`) next to the environment it actually inherited. +HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +export LD_LIBRARY_PATH="$HERE/_work/deps/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" +``` + + +## 1. Activate the environment + +This is you, not benchx: + +```sh +$ source activate.sh +$ echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH" +``` + +```text +LD_LIBRARY_PATH=examples/archery-local/_work/deps/lib +``` + +Each fence of this page runs in a shell of its own, so the fences that run `bx compare` begin with +`source activate.sh` again. In a terminal you do it once. + + +## 2. Ask about a target that is not built + +`describe` builds nothing, by contract. The provider has nothing to describe yet and says so with +exit status 3, and `bx` refuses: + +```sh exit=2 +$ bx target describe @plain +``` + +```text +provider: plain is not built; use prepare +cannot describe @plain: the provider failed to describe @plain (exit 3) +``` + + +## 3. Prepare both configurations + +`prepare` makes the provider build. Its CMake output is the build's own, so it goes to stderr and +here to a log; the target description it prints on stdout is the answer. `how_built` is the very +commands the provider ran: + +```sh +$ bx target prepare @plain > /dev/null 2> _work/prepare-plain.log \ + || { cat _work/prepare-plain.log; exit 1; } +$ bx target prepare @hardened 2> _work/prepare-hardened.log \ + || { cat _work/prepare-hardened.log; exit 1; } +``` + +```json +{ + "schema_version": "benchx/target-description/0.1.0", + "target": { + "kind": "build", + "path": "examples/archery-local/_work/build-hardened", + "source": { + "uri": "https://example.org/archery-local/demo-suite.git", + "type": "git" + }, + "source_dir": "examples/archery-local/_work/src" + }, + "how_built": "cmake -S examples/archery-local/_work/src -B examples/archery-local/_work/build-hardened -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR=examples/archery-local/_work/_deps -DDEMO_HARDENED=ON\ncmake --build examples/archery-local/_work/build-hardened -j", + "activation": "source examples/archery-local/activate.sh", + "shell": "bash", + "provider": { + "name": "provider.sh", + "version": "1" + } +} +``` + +The commands, as the provider printed them while it ran: + +```sh +$ grep 'provider\$' _work/prepare-hardened.log +``` + +```text + provider$ cmake -S examples/archery-local/_work/src -B examples/archery-local/_work/build-hardened -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR=examples/archery-local/_work/_deps -DDEMO_HARDENED=ON + provider$ cmake --build examples/archery-local/_work/build-hardened -j +``` + + +## 4. Describe works now + +The same description is stored in the build directory as `.benchx-target.json`, so a long-lived +build directory carries its own description (point `bx compare` at a bare directory and it reads that +file; no provider needed): + +```sh +$ bx target describe @hardened > _work/describe.json +$ head -3 _work/describe.json +$ head -3 _work/build-hardened/.benchx-target.json +``` + +```text +{ + "schema_version": "benchx/target-description/0.1.0", + "target": { +{ + "schema_version": "benchx/target-description/0.1.0", + "target": { +``` + + +## 5. Compare + +`bx compare` is the workbench's session loop. It asks the provider to prepare each side (a no-op +build now), then alternates the sides over the rounds, one work order each, delivers the results to +the local store, and compares. The progress and the provider's output go to `_work/compare.log`: + +```sh +$ source activate.sh +$ bx compare @plain @hardened --label build=plain,hardened --rounds 5 \ + --run-key hardening --out _work/results 2> _work/compare.log \ + || { cat _work/compare.log; exit 1; } +``` + +```text +run-key hardening profile environments baseline plain contender hardened + plain: how built: cmake -S examples/archery-local/_work/src -B examples/archery-local/_work/build-plain -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR=examples/archery-local/_work/_deps (+1 lines) + plain: activation: source examples/archery-local/activate.sh + plain: observed env: LD_LIBRARY_PATH=examples/archery-local/_work/deps/lib + hardened: how built: cmake -S examples/archery-local/_work/src -B examples/archery-local/_work/build-hardened -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR=examples/archery-local/_work/_deps -DDEMO_HARDENED=ON (+1 lines) + hardened: activation: source examples/archery-local/activate.sh + hardened: observed env: LD_LIBRARY_PATH=examples/archery-local/_work/deps/lib + differs: coordinates.subject.configuration.options.DEMO_HARDENED + +NO CHANGE DETECTED (2) + BM_Noisy/1024 wall-time effect +4.62% noise 2.87% rounds=5 + BM_Quiet/1024 wall-time effect +3.22% noise 0.80% rounds=5 +``` + +The comparison describes itself: what was built, how, and in what environment. And the verdict uses +each benchmark's own noise instead of a flat percentage: a few percent can be a clear change for the +quiet case and well inside the noise of the noisy one. The numbers vary from run to run, and with few +rounds the noisy case may be flagged too. + + +## 6. What was recorded + +The work order of the second slot (round 1, the hardened side) carries the declared text, as the +runner received it: + +```python +import json + +order = json.load(open("_work/results/orders/order-0001.json")) +print(json.dumps(order["target"], indent=1)) +``` + +```json +{ + "kind": "build", + "path": "examples/archery-local/_work/build-hardened", + "source_dir": "examples/archery-local/_work/src", + "source": { + "uri": "https://example.org/archery-local/demo-suite.git", + "type": "git" + }, + "how_built": "cmake -S examples/archery-local/_work/src -B examples/archery-local/_work/build-hardened -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR=examples/archery-local/_work/_deps -DDEMO_HARDENED=ON\ncmake --build examples/archery-local/_work/build-hardened -j", + "activation": "source examples/archery-local/activate.sh", + "shell": "bash", + "provider": { + "name": "provider.sh", + "version": "1" + } +} +``` + +and so does every result, next to what the runner saw: the declared text is in `provenance.info`, the +observed environment variables in `environment.system.process.environment`, and the build options, +which the runner read from `CMakeCache.txt`, in `subject.configuration`. Declared and observed are +both kept, and nothing judges: + +```python +import subprocess + +latest = subprocess.run(["bx", "head", "--json", "-n", "1"], capture_output=True, text=True, check=True) +result = json.loads(latest.stdout.splitlines()[0]) +info = result["provenance"]["info"] +recorded = { + "provenance.info.activation": info["activation"], + "provenance.info.shell": info["shell"], + "provenance.info.target_provider": info["target_provider"], + "environment.system.process.environment": result["coordinates"]["environment"]["system"]["process"]["environment"], + "subject.configuration": result["coordinates"]["subject"]["configuration"], +} +print(json.dumps(recorded, indent=1)) +``` + +```json +{ + "provenance.info.activation": "source examples/archery-local/activate.sh", + "provenance.info.shell": "bash", + "provenance.info.target_provider": { + "name": "provider.sh", + "version": "1" + }, + "environment.system.process.environment": { + "LD_LIBRARY_PATH": "examples/archery-local/_work/deps/lib" + }, + "subject.configuration": { + "build_type": "Release", + "cxx_compiler": "GNU 15.3.0", + "options": { + "DEMO_HARDENED": "ON" + } + } +} +``` + + +## 7. Forget to activate + +The same comparison with `LD_LIBRARY_PATH` removed from the environment: the declaration still says +`source .../activate.sh`, the observed environment no longer lists `LD_LIBRARY_PATH`, and nothing +refuses. Declarations are recorded as given +([benchmark-environments.md](https://github.com/benchx-dev/benchx/blob/main/docs/design/benchmark-environments.md) §3.2), +never checked. Three rounds are too few for a verdict, which is enough here: + +```sh +$ env -u LD_LIBRARY_PATH bx compare @plain @hardened --no-build --label build=plain,hardened \ + --rounds 3 --run-key hardening-unactivated --out _work/results-unactivated 2> _work/unactivated.log \ + || { cat _work/unactivated.log; exit 1; } +``` + +```text +run-key hardening-unactivated profile environments baseline plain contender hardened + plain: how built: cmake -S examples/archery-local/_work/src -B examples/archery-local/_work/build-plain -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR=examples/archery-local/_work/_deps (+1 lines) + plain: activation: source examples/archery-local/activate.sh + hardened: how built: cmake -S examples/archery-local/_work/src -B examples/archery-local/_work/build-hardened -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR=examples/archery-local/_work/_deps -DDEMO_HARDENED=ON (+1 lines) + hardened: activation: source examples/archery-local/activate.sh + differs: coordinates.subject.configuration.options.DEMO_HARDENED + +INDETERMINATE (2) + BM_Noisy/1024 wall-time effect ? noise ? rounds=3 (insufficient-rounds) + BM_Quiet/1024 wall-time effect ? noise ? rounds=3 (insufficient-rounds) +``` + + +## Try it by hand + +After the steps above, the build directories and the store are in `_work/`. In a shell with the +environment activated, from the directory of the example: + +```sh noexec +source activate.sh + +bx target describe @hardened # what exists, nothing built +bx target prepare @hardened # incremental build, then the description +bx target describe _work/build-hardened # a bare directory: its own sidecar + +# the story's own knobs: scope by filter, precision by repetitions +bx compare @plain @hardened --label build=plain,hardened --filter BM_Quiet --rounds 10 \ + --repetitions 5 --run-key mine # adapter, suite, project and profile: benchx.conf.json + +bx compare --run-key mine --label build --baseline plain # read mode +bx head -n 4 # the latest stored results +bx head --json -n 1 | python3 -m json.tool | less # provenance.info, environment.system +``` + +Edit the `hardened)` case of `provider.sh` or `activate.sh` and run again to see how each change shows +up in the next comparison. Empty text is fine: set `"activation": ""` (or `null`, or leave it out) in a +description and the runner omits it from the results. + + +## What is in this directory + +| File | Role | +|---|---| +| `README.md` | this page, executed | +| `provider.sh` | the target provider: your build script, speaking the `describe`/`prepare` contract | +| `benchx.conf.json` | the project file: the provider command, the harness and its suite, the project name, the comparison profile, and where the store and results go | +| `activate.sh` | the environment you activate before calling `bx` | +| `../arrow-suite/` | the stand-in suite, shared with `arrow-demo`: `BM_Quiet` and `BM_Noisy`, built with CMake; `-DDEMO_HARDENED=ON` adds about 3% of work | +| `_work/` | everything generated: source copy, build directories, results, store (git-ignored) | + +The documents involved are in +[`src/benchx/schemas/`](https://github.com/benchx-dev/benchx/tree/main/src/benchx/schemas): +`target-provider-request`, `target-description`, and `work-order` (its `target` takes the declared text). + + +## What this does not show + +The provider here builds one source tree. Comparing against a tag or a past revision (`@plain=v1.0`) +is the provider's business and is not implemented in this example; the `arrow-demo` example shows +revisions with two prebuilt worktrees. A declared `build` object in a description is refused by the +runner, which reads `CMakeCache.txt` instead. diff --git a/examples/archery-local/activate.sh b/examples/archery-local/activate.sh new file mode 100644 index 00000000..cd10b8b1 --- /dev/null +++ b/examples/archery-local/activate.sh @@ -0,0 +1,8 @@ +# The environment this example's benchmarks are meant to run in. In the Arrow +# story this is where you would load a toolchain or point at the dependencies +# you built; here it only puts a stand-in "deps" directory on the library +# search path. You source it yourself, before invoking bx: benchx never runs +# it, it records the text that says you did (the target description's +# `activation`) next to the environment it actually inherited. +HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +export LD_LIBRARY_PATH="$HERE/_work/deps/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" diff --git a/examples/archery-local/benchx.conf.json b/examples/archery-local/benchx.conf.json new file mode 100644 index 00000000..90293069 --- /dev/null +++ b/examples/archery-local/benchx.conf.json @@ -0,0 +1,13 @@ +{ + "schema_version": "benchx/config/0.1.0", + "project": "archery-local", + "provider": { "command": ["./provider.sh"] }, + "adapter": { + "name": "google-benchmark", + "suite": "demo-bench", + "quantities": ["wall-time"], + "precision": { "repetitions": 5 } + }, + "paths": { "store": "_work/store.parquet", "results": "_work/results" }, + "compare": { "profile": "environments" } +} diff --git a/examples/archery-local/provider.sh b/examples/archery-local/provider.sh new file mode 100755 index 00000000..df06b4fa --- /dev/null +++ b/examples/archery-local/provider.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# The target provider of this example (system-decomposition.md §3.4). +# +# It is the user's own build script, speaking the provider contract: one JSON +# request on stdin (schemas/target-provider-request/0.1.0), one target +# description on stdout (schemas/target-description/0.1.0). Everything else, +# above all the build's own output, goes to stderr. +# +# describe reports a target that exists and builds nothing; exit 3 if none +# prepare configures and builds it, with CMake, as Arrow's contributors do +# (an incremental no-op when it is already built), then reports it +# +# Configurations: `plain`, and `hardened` (-DDEMO_HARDENED=ON), the same +# commit built two ways, like the -DARROW_HARDENING=ON story. +# +# `how_built` is assembled from the very commands this script runs, so the +# text cannot drift from what happened. benchx never runs any of it. +set -euo pipefail +HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +WORK=${BENCHX_EXAMPLE_WORK:-$HERE/_work} +export GIT_AUTHOR_NAME=demo GIT_AUTHOR_EMAIL=demo@example.org +export GIT_COMMITTER_NAME=demo GIT_COMMITTER_EMAIL=demo@example.org + +request=$(cat) +field() { python3 -c 'import json,sys; print(json.load(sys.stdin).get(sys.argv[1]) or "")' "$1" <<<"$request"; } +operation=$(field operation) +configuration=$(field configuration) +case "$configuration" in + plain) extra=() ;; + hardened) extra=(-DDEMO_HARDENED=ON) ;; + *) echo "provider: unknown configuration '$configuration' (plain or hardened)" >&2; exit 2 ;; +esac +SRC=$WORK/src +BUILD=$WORK/build-$configuration + +describe() { + [[ -f $BUILD/.benchx-target.json ]] || { echo "provider: $configuration is not built; use prepare" >&2; exit 3; } + cat "$BUILD/.benchx-target.json" +} + +if [[ $operation == describe ]]; then describe; exit 0; fi + +HOW="" +step() { # step : record it in how_built, show it, run it (output to stderr) + local line="$*" + HOW+="$line"$'\n' + printf '\033[2m provider$ %s\033[0m\n' "$line" >&2 + "$@" >&2 +} + +if [[ ! -d $SRC/.git ]]; then + mkdir -p "$WORK" + cp -R "$HERE/../arrow-suite" "$SRC" + git -C "$SRC" init -q + git -C "$SRC" remote add origin https://example.org/archery-local/demo-suite.git + git -C "$SRC" add -A + git -C "$SRC" commit -qm "demo-suite" +fi +mkdir -p "$WORK/deps/lib" # stand-in for the dependencies activate.sh points at + +step cmake -S "$SRC" -B "$BUILD" -DCMAKE_BUILD_TYPE=Release -DFETCHCONTENT_BASE_DIR="$WORK/_deps" ${extra[@]+"${extra[@]}"} +step cmake --build "$BUILD" -j + +BUILD=$BUILD SRC=$SRC HOW=$HOW CONFIGURATION=$configuration HERE=$HERE python3 - >"$BUILD/.benchx-target.json" <<'PY' +import json, os +print(json.dumps({ + "schema_version": "benchx/target-description/0.1.0", + "target": {"kind": "build", "path": os.environ["BUILD"], + "source": {"uri": "https://example.org/archery-local/demo-suite.git", "type": "git"}, + "source_dir": os.environ["SRC"]}, + "how_built": os.environ["HOW"].rstrip("\n"), + "activation": "source " + os.path.join(os.environ["HERE"], "activate.sh"), + "shell": "bash", + "provider": {"name": "provider.sh", "version": "1"}, +}, indent=1)) +PY +describe # the sidecar just written, which stays with the build directory diff --git a/examples/arrow-demo/.gitignore b/examples/arrow-demo/.gitignore new file mode 100644 index 00000000..e96b7d6e --- /dev/null +++ b/examples/arrow-demo/.gitignore @@ -0,0 +1 @@ +_work/ diff --git a/examples/arrow-demo/README.md b/examples/arrow-demo/README.md new file mode 100644 index 00000000..2ee53d71 --- /dev/null +++ b/examples/arrow-demo/README.md @@ -0,0 +1,367 @@ +# arrow-demo: both comparison profiles, on a small Google Benchmark suite + +A walkthrough in the shape of the Arrow local-benchmarking story, on one small stand-in suite: a +two-benchmark Google Benchmark suite, `BM_Quiet` and `BM_Noisy`, built with CMake. The two +benchmarks are chosen so that the verdicts are easy to read: `BM_Quiet` repeats almost exactly, and +`BM_Noisy` draws its own load level in every repetition, so its repetitions disagree by around ten +percent. The two comparisons below each add a few percent of work: + +- **revisions** (the second commit does 2% more work per call): the quiet benchmark can flag it, the + noisy one has too much noise to; +- **environments** (the same commit built plain and with `-DDEMO_HARDENED=ON`, which adds about 3%). + +You play the user, and in that role the target provider: you prepare the sources and the builds +with your own tools, a git repository with two commits and three CMake build directories. benchx +builds nothing. Then `bx compare` in measure mode does the workbench's part: it alternates the sides +over the rounds, assigns each work order its round and slot, delivers every result to the local +store, and compares. + + +## What you need + +```sh noexec +python -m venv .venv && . .venv/bin/activate +pip install benchx[parquet] +``` + +You also need `cmake`, a C++ compiler and `git`, and network access the first time (CMake fetches +Google Benchmark v1.9.1 unless it is installed). The whole example takes a few minutes. + +Everything it creates is in `_work/`, including the local store, the build directories and the logs +of the long commands (a command whose output goes to a log prints the log if it fails). Remove what +an earlier run left, so that the example can run again: + +```sh +$ rm -rf _work +$ mkdir _work +``` + +The project file tells `bx` the harness and its suite, the precision, the comparison profile, and +where the store and the results go. It names no project, so a run is ad hoc unless you give +`--project`. Every `bx` command below runs from this directory, where the project file is: + +```sh +$ cat benchx.conf.json +``` + +```json +{ + "schema_version": "benchx/config/0.1.0", + "adapter": { + "name": "google-benchmark", + "suite": "demo-bench", + "precision": { "repetitions": 5 } + }, + "paths": { "store": "_work/store.parquet", "results": "_work/results" }, + "compare": { "profile": "revisions" } +} +``` + + +## 1. A source with two commits, and a worktree at each + +The suite (`../arrow-suite`, which `archery-local` uses too) goes into a git repository of its own, +in two commits: `head` raises the work per call by 2%. + +```sh +export GIT_AUTHOR_NAME=demo GIT_AUTHOR_EMAIL=demo@example.org +export GIT_COMMITTER_NAME=demo GIT_COMMITTER_EMAIL=demo@example.org +cp -R ../arrow-suite _work/src +git -C _work/src init -q +git -C _work/src add -A +git -C _work/src commit -qm base +sed -i.bak 's/kWork = 100/kWork = 102/' _work/src/bench.cpp +rm _work/src/bench.cpp.bak +git -C _work/src commit -qam "head: 2% more work" +git -C _work/src worktree add -q --detach ../wt-base HEAD~1 +git -C _work/src worktree add -q --detach ../wt-head HEAD +git -C _work/src log --oneline +ls _work +``` + +```text +b59afe9 head: 2% more work +16df608 base +src +wt-base +wt-head +``` + + +## 2. Three build directories: base, head, and head hardened + +Plain CMake, as a contributor would build. Each build's own output goes to a log: + +```sh +build() { # build NAME WORKTREE [CMAKE OPTIONS] + name=$1; tree=$2; shift 2 + { cmake -S "_work/$tree" -B "_work/build-$name" -DCMAKE_BUILD_TYPE=Release \ + -DFETCHCONTENT_BASE_DIR="$PWD/_work/_deps" "$@" \ + && cmake --build "_work/build-$name" -j; } > "_work/build-$name.log" 2>&1 \ + || { cat "_work/build-$name.log"; return 1; } +} +build base wt-base +build head wt-head +build hardened wt-head -DDEMO_HARDENED=ON +ls _work/build-*/demo-bench +grep -H DEMO_HARDENED _work/build-*/CMakeCache.txt +``` + +```text +_work/build-base/demo-bench +_work/build-hardened/demo-bench +_work/build-head/demo-bench +_work/build-base/CMakeCache.txt:DEMO_HARDENED:BOOL=OFF +_work/build-hardened/CMakeCache.txt:DEMO_HARDENED:BOOL=ON +_work/build-head/CMakeCache.txt:DEMO_HARDENED:BOOL=OFF +``` + + +## 3. Revisions, ad hoc: measured and compared in one command + +`bx compare` takes each side as `BUILD_DIR:SOURCE_DIR`. The adapter, the suite and the profile come +from the project file. The sides alternate over 5 rounds, with one work order each. The progress +goes to a log: + +```sh +$ bx compare _work/build-base:_work/wt-base _work/build-head:_work/wt-head \ + --rounds 5 --run-key rev-adhoc --out _work/results-rev 2> _work/rev.log \ + || { cat _work/rev.log; exit 1; } +``` + +```text +run-key rev-adhoc profile revisions baseline 5b6ac1aee10d contender 3eeebf3710da + differs: benchmark.revision.key + +NO CHANGE DETECTED (2) + BM_Noisy/1024 wall-time effect +4.10% noise 3.38% rounds=5 + BM_Quiet/1024 wall-time effect +2.05% noise 0.59% rounds=5 +``` + +The first work order it wrote: + +```sh +$ cat _work/results-rev/orders/order-0000.json +``` + +```json +{ + "schema_version": "benchx/work-order/0.1.0", + "work_order_id": "urn:uuid:f77bb271-a463-440e-9343-c336d845427b", + "state": "resolved", + "suites": [ + { + "adapter": "google-benchmark", + "suite": "demo-bench" + } + ], + "target": { + "kind": "build", + "path": "examples/arrow-demo/_work/build-base", + "source_dir": "examples/arrow-demo/_work/wt-base", + "source": { + "uri": "file://examples/arrow-demo/_work/src", + "type": "git" + } + }, + "quantities": [ + "wall-time" + ], + "precision": { + "repetitions": 5, + "calibration": { + "mode": "adaptive", + "minimum_sample_seconds": 0.5 + }, + "warmup": { + "mode": "none" + } + }, + "timeouts": { + "case_s": 60, + "order_s": 600 + }, + "run_key": "rev-adhoc", + "requester": { + "kind": "workbench", + "name": "bx" + }, + "plan": [ + { + "id": "p0", + "case": "*", + "quantity": "wall-time" + } + ], + "round": 0, + "slot": 0 +} +``` + +Read mode compares results that were already measured: the same run key, a baseline named by its +revision, and the result files themselves, through a throwaway store: + +```sh +$ BASE=$(git -C _work/src rev-parse --short=10 HEAD~1) +$ bx compare --run-key rev-adhoc --baseline "$BASE" --results _work/results-rev +``` + +```text +run-key rev-adhoc profile revisions baseline 5b6ac1aee10d contender 3eeebf3710da + differs: benchmark.revision.key + +NO CHANGE DETECTED (2) + BM_Noisy/1024 wall-time effect +4.10% noise 3.38% rounds=5 + BM_Quiet/1024 wall-time effect +2.05% noise 0.59% rounds=5 +``` + +`bx compare` also delivered these results to the local store, as thin results of the project +`local/`, since this run named no project. + + +## 4. Environments, tracked: project `demo`, into the local store + +The same commit, built two ways. The sides are told apart by a label, `build=plain,hardened`, and the +results are filed under a project, so they join its series: + +```sh +$ bx compare _work/build-head:_work/wt-head _work/build-hardened:_work/wt-head --profile environments \ + --label build=plain,hardened --rounds 5 --project demo \ + --run-key env-tracked --out _work/results-env 2> _work/env.log \ + || { cat _work/env.log; exit 1; } +``` + +```text +run-key env-tracked profile environments baseline plain contender hardened + differs: coordinates.subject.configuration.options.DEMO_HARDENED + +REGRESSED (1) + BM_Quiet/1024 wall-time effect +3.06% noise 0.13% rounds=5 + +NO CHANGE DETECTED (1) + BM_Noisy/1024 wall-time effect +2.16% noise 4.17% rounds=5 +``` + +Delivering the same files again is a visible no-op: + +```sh +$ bx ingest _work/results-env +``` + +```text +ingested 0, duplicate 40 +``` + + +## 5. A conflicting unit is rejected + +A copy of one result, with its unit changed from `s` to `ms`, is a different measurement of a +quantity the store already knows in seconds. The store refuses it, with a code from its taxonomy: + +```python +import json +import pathlib + +first = sorted(pathlib.Path("_work/results-env").glob("*_wall-time_*.json"))[0] +doc = json.loads(first.read_text()) +doc["ingest_key"] += ":in-ms" +doc["measurement"]["quantity"]["unit"] = "ms" +pathlib.Path("_work/bad.json").write_text(json.dumps(doc)) +``` + +```sh exit=1 +$ bx ingest _work/bad.json +``` + +```text + rejected bad.json: unit-conflict — quantity 'wall-time' is 's' in demo, got 'ms' +ingested 0, duplicate 0, rejected 1 (unit-conflict) +``` + + +## 6. Compare from the local store + +```sh +$ bx compare --run-key env-tracked --profile environments --label build --baseline plain +``` + +```text +run-key env-tracked profile environments baseline plain contender hardened + differs: coordinates.subject.configuration.options.DEMO_HARDENED + +REGRESSED (1) + BM_Quiet/1024 wall-time effect +3.06% noise 0.13% rounds=5 + +NO CHANGE DETECTED (1) + BM_Noisy/1024 wall-time effect +2.16% noise 4.17% rounds=5 +``` + +The same comparison through a throwaway store, from the result files, gives the same document: + +```sh +$ bx compare --run-key env-tracked --profile environments --label build --baseline plain --json > _work/a.json +$ bx compare --run-key env-tracked --profile environments --label build --baseline plain --json \ + --results _work/results-env > _work/b.json +$ cmp _work/a.json _work/b.json && echo identical +``` + +```text +identical +``` + + +## 7. Series and history + +`bx series` lists the series the store holds, one per benchmark, quantity and estimator, and +`bx history` lists the points of one of them. History is a query: the order of the points is by +measurement start, since the implementation has no revision graph: + +```sh +$ bx series --workload BM_Quiet --quantity wall-time > _work/series.txt +$ head -6 _work/series.txt +$ SERIES=$(awk '/demo .*est=benchx\/median/ {print $1; exit}' _work/series.txt) +$ bx history "$SERIES" > _work/history.txt +$ head -1 _work/history.txt +$ tail -n 4 _work/history.txt +``` + +```text +d48b3a015017 local/53552e5551da BM_Quiet/1024 wall-time [s] est=benchx/median/v1 +8b2a6788cd98 local/53552e5551da BM_Quiet/1024 wall-time [s] est=google-benchmark/mean +63c1214f1510 local/53552e5551da BM_Quiet/1024 wall-time [s] est=google-benchmark/median +973a5eea174a demo BM_Quiet/1024 wall-time [s] est=benchx/median/v1 +391cb2a01152 demo BM_Quiet/1024 wall-time [s] est=google-benchmark/mean +ce2aaf3daf7d demo BM_Quiet/1024 wall-time [s] est=google-benchmark/median +revision order unavailable without a revision graph; listed by measurement start +b59afe9b1c3c 2026-12-04T11:42:11.699091Z 0.00735635 (server) env-tracked:BM_Quiet%2F1024:wall-time:slot-3 +b59afe9b1c3c 2026-12-04T11:42:19.269299Z 0.0073488 (server) env-tracked:BM_Quiet%2F1024:wall-time:slot-4 +b59afe9b1c3c 2026-12-04T11:42:41.184410Z 0.00731151 (server) env-tracked:BM_Quiet%2F1024:wall-time:slot-7 +b59afe9b1c3c 2026-12-04T11:42:48.130166Z 0.00738083 (server) env-tracked:BM_Quiet%2F1024:wall-time:slot-8 +``` + + +## 8. The store + +The store is one Parquet file. Every measure-mode `bx compare` delivers to it, so it grows from run +to run, and each series' history with it. `bx head` shows the latest rows at any time: + +```sh +$ du -h _work/store.parquet +$ bx head -n 8 +``` + +```text +88K _work/store.parquet +examples/arrow-demo/_work/store.parquet: 80 result(s); latest 8, newest first + row started run-key workload quantity status median revision project + 79 2026-12-04 11:42:59 env-tracked BM_Noisy/1024 load-average success 2.03 b59afe9b1c demo {"build":"hardened"} + 78 2026-12-04 11:42:59 env-tracked BM_Noisy/1024 wall-time success 0.00887985 s b59afe9b1c demo {"build":"hardened"} + 77 2026-12-04 11:42:55 env-tracked BM_Quiet/1024 load-average success 2.03 b59afe9b1c demo {"build":"hardened"} + 76 2026-12-04 11:42:55 env-tracked BM_Quiet/1024 wall-time success 0.00759647 s b59afe9b1c demo {"build":"hardened"} + 75 2026-12-04 11:42:51 env-tracked BM_Noisy/1024 load-average success 2.03 b59afe9b1c demo {"build":"plain"} + 74 2026-12-04 11:42:51 env-tracked BM_Noisy/1024 wall-time success 0.0085137 s b59afe9b1c demo {"build":"plain"} + 73 2026-12-04 11:42:48 env-tracked BM_Quiet/1024 load-average success 2.04 b59afe9b1c demo {"build":"plain"} + 72 2026-12-04 11:42:48 env-tracked BM_Quiet/1024 wall-time success 0.00738083 s b59afe9b1c demo {"build":"plain"} +``` + +Starting over is `rm -rf _work`. diff --git a/examples/arrow-demo/benchx.conf.json b/examples/arrow-demo/benchx.conf.json new file mode 100644 index 00000000..307efb8e --- /dev/null +++ b/examples/arrow-demo/benchx.conf.json @@ -0,0 +1,10 @@ +{ + "schema_version": "benchx/config/0.1.0", + "adapter": { + "name": "google-benchmark", + "suite": "demo-bench", + "precision": { "repetitions": 5 } + }, + "paths": { "store": "_work/store.parquet", "results": "_work/results" }, + "compare": { "profile": "revisions" } +} diff --git a/examples/arrow-suite/CMakeLists.txt b/examples/arrow-suite/CMakeLists.txt new file mode 100644 index 00000000..503ff46b --- /dev/null +++ b/examples/arrow-suite/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.20) +project(demo CXX) + +option(DEMO_HARDENED "Simulated hardening checks, the one option the environments demo changes" OFF) +set(CMAKE_CXX_STANDARD 17) + +find_package(benchmark QUIET) +if(NOT benchmark_FOUND) + include(FetchContent) + set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) + set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "" FORCE) + FetchContent_Declare(benchmark + GIT_REPOSITORY https://github.com/google/benchmark.git + GIT_TAG v1.9.1 + GIT_SHALLOW TRUE) + FetchContent_MakeAvailable(benchmark) +endif() + +add_executable(demo-bench bench.cpp) +target_link_libraries(demo-bench benchmark::benchmark_main) +if(DEMO_HARDENED) + target_compile_definitions(demo-bench PRIVATE DEMO_HARDENED) +endif() diff --git a/examples/arrow-suite/bench.cpp b/examples/arrow-suite/bench.cpp new file mode 100644 index 00000000..01d59389 --- /dev/null +++ b/examples/arrow-suite/bench.cpp @@ -0,0 +1,44 @@ +// Two benchmarks for the prototype demo: a quiet one and a noisy one. +// +// kWork is what the revisions demo changes between commits; DEMO_HARDENED is +// what the environments demo changes between two builds of one commit. Both +// add a few percent of work, so the expected verdicts are known: the quiet +// benchmark flags the change, the noisy one does not. +#include + +#include +#include + +constexpr int kWork = 100; +#ifdef DEMO_HARDENED +constexpr int kExtra = 3; // simulated cost of hardening checks, in percent +#else +constexpr int kExtra = 0; +#endif + +static double Sum(const std::vector& v, long rounds) { + double s = 0; + for (long r = 0; r < rounds; ++r) { + for (double x : v) s += x; + benchmark::DoNotOptimize(s); + } + return s; +} + +static void BM_Quiet(benchmark::State& state) { + std::vector v(state.range(0), 1.0); + const long rounds = kWork * (100 + kExtra); + for (auto _ : state) benchmark::DoNotOptimize(Sum(v, rounds)); +} +BENCHMARK(BM_Quiet)->Arg(1024); + +// Each repetition draws its own load level, so repetitions disagree by +// around ten percent, as they do for a benchmark at the mercy of its machine. +static void BM_Noisy(benchmark::State& state) { + std::vector v(state.range(0), 1.0); + std::random_device device; + std::uniform_real_distribution level(1.0, 1.25); + const long rounds = static_cast(kWork * (100 + kExtra) * level(device)); + for (auto _ : state) benchmark::DoNotOptimize(Sum(v, rounds)); +} +BENCHMARK(BM_Noisy)->Arg(1024);