diff --git a/examples/numpy-revisions/.gitignore b/examples/numpy-revisions/.gitignore new file mode 100644 index 0000000..ea93687 --- /dev/null +++ b/examples/numpy-revisions/.gitignore @@ -0,0 +1,2 @@ +out/ +__pycache__/ diff --git a/examples/numpy-revisions/README.md b/examples/numpy-revisions/README.md new file mode 100644 index 0000000..3192a60 --- /dev/null +++ b/examples/numpy-revisions/README.md @@ -0,0 +1,225 @@ +# numpy-revisions: two NumPy revisions, one of NumPy's own asv benchmarks + +This is [UC-03](https://github.com/benchx-dev/benchx/blob/main/docs/use-cases/UC-03-compare.md), +"run a benchmark comparison across two revisions", for NumPy. Two revisions are built with +`spin`, each in a build directory of its own in the NumPy checkout, so both builds exist at once. +A benchmark that is already in NumPy, written in [asv](https://asv.readthedocs.io)'s format, is +run on each, alternately, round by round, and `bx compare` says whether the second revision +differs from the first. + +`compare.sh` is used like `spin bench --compare`: two revisions, and `-t` to choose benchmarks of +NumPy's own asv suite, `benchmarks/benchmarks`, which are run unchanged. The example below is +`Mean.time_mean`, in `bench_function_base.py`. benchx reads asv's format as a language (discovery, +`params`, `setup`) and times it with pyperf: neither asv nor asv_runner is installed. + +```python +class Mean(Benchmark): + param_names = ['size'] + params = [[1, 10, 100_000]] + + def setup(self, size): + self.array = np.arange(2 * size).reshape(2, size) + + def time_mean(self, size): + np.mean(self.array) +``` + +One benchmark with three sizes is three cases. The file is the same in `v2.4.0` and `v2.4.3`, the +pair used below, so both sides run the same benchmark. + +## Run it + +You need a git checkout of NumPy with no uncommitted changes (the revisions are checked out in it, +see below), network access the first time (for NumPy's submodules), and an environment with NumPy's +build requirements, `spin`, pyperf and `bx`: + +``` +$ micromamba create -f environment.yml +$ micromamba activate numpy-revisions +$ pip install benchx +$ NUMPY=~/repos/numpy ./compare.sh -t 'Mean\.time_mean\(' v2.4.0 v2.4.3 +``` + +The usage is `./compare.sh [-t TEST] [BASELINE [CONTENDER]]`, and everything is optional, as it is +for `spin bench --compare`: + +- **Revisions.** Anything that git resolves in `$NUMPY`: tags, branches, `HEAD`, `HEAD^`, commits. They + default to `main` and the checked-out code, and `./compare.sh v2.4.0` compares `v2.4.0` with it. The + first is the baseline and the second the contender. +- **`-t TEST`.** A regular expression searched in asv's name of a benchmark, `file.Class.method` + (the file is under `benchmarks/benchmarks/`, without `.py`), and in `name(params)` for each parameter + combination, as `spin bench -t` does. So `-t bench_function_base` is a file, `-t Mean` a class, and + `-t Mean.time_mean` a method, and also `time_mean_axis`, since it only searches. The `\(` above ends the + name, so that only `time_mean` is selected. Combine tests with `|` to select more. Without `-t` + every benchmark runs, which takes hours. A `-t` that selects nothing is refused, and the refusal lists + the benchmarks whose names are closest to it (a misspelled class or method finds its benchmarks), or the first few that exist. + +**One combination of parameters.** A parametrized benchmark is searched once per combination, as +`name(p0, p1, ...)`: the arguments in the order of its `param_names`, each as its Python `repr`, so a string +is quoted and a tuple keeps its parentheses. `Partition.time_argpartition` takes `dtype, array_type, k`, so +this selects one case of it, the same in `v2.4.0` and `v2.4.3`: + +``` +$ NUMPY=~/repos/numpy ./compare.sh -t "Partition\.time_argpartition\('int16', \('sorted_block', 1000\), 10\)" v2.4.0 v2.4.3 +``` + +The parentheses are escaped because the filter is a regular expression, and the double quotes keep the shell +from reading the single ones. The arguments in another order select nothing. When a `-t` names a +parametrized benchmark and none of its combinations, the refusal says so, gives the order of the parameters +and one case as it is searched: +`bench_function_base.Partition.time_argpartition(dtype, array_type, k) is searched as +bench_function_base.Partition.time_argpartition('float64', ('random',), 10)`. + +**Builds.** `spin build` builds each revision in `$NUMPY`, in a build directory of its own, so both builds +exist at once: a revision in `build-` (installed in `build--install`), the checked-out +code in `build`, where your own `spin build` puts it. It runs every time, and is incremental: a revision +takes a few minutes the first time on a many-core machine, and less after. NumPy git-ignores these +directories. `` is the short id (10 hex digits) of the commit the revision names now, so `main` is built in +the directory of the commit `main` is at (`build-c5ab79c14c` for `v2.4.0`). + +| You run | Baseline | Contender | +|---|---|---| +| `./compare.sh` | `main`, built in `build-` | the checked-out code, built in `build` | +| `./compare.sh SHA1` | `SHA1`, built in `build-` | the checked-out code, built in `build` | +| `./compare.sh SHA1 SHA2` | `SHA1`, built in `build-` | `SHA2`, built in `build-` | + +**The checkout.** `bx` reads each side's revision from the checkout `$NUMPY` (the provider names it as the +target's source) and tells the sides apart by their trees, so each call has its commit checked out first: +ten checkouts for 5 rounds. A checkout includes NumPy's +submodules (`git submodule update --init`): releases pin other commits of `vendored-meson`, `svml` and +others than `main` does, and without the update a build is made of the wrong sources and benchx tags the +tree dirty. When the script ends, also when it fails or is interrupted, the checkout is +put back, on its branch or at its commit. That is why it refuses to start when `$NUMPY` has +uncommitted changes, which would otherwise go into a build: commit them first. Untracked files are left +alone. Checking out another revision touches the files that differ, so your next `spin build` in +`$NUMPY` recompiles those. + +The measuring took about a minute on a 64-core machine. `benchx.conf.json` holds the precision +(5 processes x 3 values per case), and the loop in `compare.sh` the number of rounds (5). + +Everything that is generated here is in `out/`, which is git-ignored. Each run has a run key of its own, +`numpy-revisions--