Example: benchmarking Python functions with bx bench¶
A runnable example of a benchmark suite. bench_suite.py holds five small
functions; bx bench runs each one under pyperf,
writes benchx result files, delivers them to a local store, and prints a table.
First, install benchx¶
We install benchx with pyperf
$ python3 -m venv .venv && . .venv/bin/activate # Python >= 3.11, or any environment you like
$ pip install pyperf benchx[parquet]
Benchmarks¶
In the simplest case, benchmarks are plain python functions. Here is an example suite of several small microbenchmarks (here we only show one function out of five in the suite):
$ head -20 bench_suite.py
"""Example benchmark suite: plain Python functions, one case each.
`bx bench bench_suite.py` runs every function named bench_* or benchmark_*.
Name functions on the command line to run only those:
bx bench bench_suite.py bench_sorted bench_join
Each function is the timed body; pyperf calls it many times. Do your setup
at module level (it runs once per worker process, outside the timing).
"""
import os
DATA = list(range(2000, 0, -1))
WORDS = [f"word{i}" for i in range(500)]
def bench_sorted():
sorted(DATA)
Additionally, this directory contains an optional configuration file, benchx.conf.json,
which collects settings for the benchmarking process. See Sec. 8 below for details.
The benchmark functions are timed bodies: pyperf calls each one many times, and does
the calibration (how many calls make up one value) itself. Put setup at module level.
Run the benchmarks¶
The main CLI entry point for the benchx package is the bx executable, which
supports several subcommands.
1. Run all benchmarks with default settings¶
The first subcommand is bx bench which accepts the
name of the suite or a selection of benchmarks:
$ bx bench . --run-key example-all --out out/all
measuring bench_suite.py on .
precision: 5 processes x 3 values (config: examples/pyperf-suite/benchx.conf.json)
case median mean ± sd min .. max n status
----------------- -------- ------------------- -------------------- -- -------
bench_dict_build 8.724 us 8.743 us ± 285.5 ns 8.282 us .. 9.278 us 15 success
bench_env_var 401.7 ns 409.9 ns ± 24.22 ns 388 ns .. 489.8 ns 15 success
bench_join 2.382 us 2.382 us ± 173.6 ns 1.949 us .. 2.614 us 15 success
bench_sorted 7.114 us 7.136 us ± 177.3 ns 6.947 us .. 7.497 us 15 success
bench_sum_squares 24.65 us 24.5 us ± 758.6 ns 23.13 us .. 25.65 us 15 success
10 result(s) -> out/all (order sha256:544c17387f38)
run-key: example-all
examples/pyperf-suite/out/store.parquet: ingested 10, duplicate 0
Here the first argument to bx bench is the current directory, and it makes it search
for functions names bench_ in all python files in the current directory.
Other arguments are optional and are only provided for convenience:
--out controls where the output files go (default is $PWD/out), and --run-key
sets the key for the results of this run in the local store. If not provided, it will
be synthesized.
Specific parameters for running benchmarks (the number of repetitions
and so on) will be taken from the configuration file, benchx.conf.json.
The output lists benchmarks and their results:
2. Run a single benchmark, with an environment variable and process controls¶
To run a single benchmark from the suite, use the second optional argument of bx bench:
$ bx bench . bench_env_var -e EXAMPLE_N=100000 --processes 3 \
--run-key example-env --out out/env
measuring bench_suite.py on .
precision: 3 processes x 3 values (config: examples/pyperf-suite/benchx.conf.json)
case median mean ± sd min .. max n status
-------------------------------- -------- ------------------- -------------------- - -------
bench_env_var [EXAMPLE_N=100000] 228.4 ns 228.6 ns ± 10.58 ns 215.5 ns .. 241.4 ns 9 success
2 result(s) -> out/env (order sha256:915e0fd7dbfe)
run-key: example-env
examples/pyperf-suite/out/store.parquet: ingested 2, duplicate 0
--processes) can supplied
as command line arguments---if provided, they superseded the values from the configuration file.
We also set an environment variable, which the benchmark reads, via the -e argument.
3. Repeat a run¶
Here we rerun a benchmark again under a new run key, so its series gets a second point:
$ bx bench . bench_join --run-key example-again --out out/again
measuring bench_suite.py on .
precision: 5 processes x 3 values (config: examples/pyperf-suite/benchx.conf.json)
case median mean ± sd min .. max n status
---------- ------- ------------------- -------------------- -- -------
bench_join 2.62 us 2.642 us ± 196.3 ns 2.489 us .. 3.302 us 15 success
2 result(s) -> out/again (order sha256:c4f35e1baffc)
run-key: example-again
examples/pyperf-suite/out/store.parquet: ingested 2, duplicate 0
Note that using a different --run-key is essential, while setting a different --out directory
only is not.
4. Idempotency conflicts¶
Attempting to reuse the same run key for a different workload is a user error. The system will refuse to add the new result to the local store (see Section 5 below): results of the rerun will not be byte-identical, and the store maintains a one-to-one correspondence between the run key and the run results. The error will be reported as idempotency conflict:
# same run-key
$ bx bench . bench_join --run-key example-again --out out/again2
measuring bench_suite.py on .
precision: 5 processes x 3 values (config: examples/pyperf-suite/benchx.conf.json)
case median mean ± sd min .. max n status
---------- -------- ------------------- ------------------- -- -------
bench_join 2.502 us 2.449 us ± 101.7 ns 2.279 us .. 2.55 us 15 success
2 result(s) -> out/again2 (order sha256:a5c6a5f59f67)
run-key: example-again
rejected example-again_bench_join_wall-time.json: idempotency-conflict — same (producer, ingest_key), different payload
rejected example-again_bench_join_load-average.json: idempotency-conflict — same (producer, ingest_key), different payload
examples/pyperf-suite/out/store.parquet: ingested 0, duplicate 0, rejected 2 (idempotency-conflict)
5. The local store¶
Benchmark results are saved to the local store. bx head lists its contents:
$ bx head -n 10
examples/pyperf-suite/out/store.parquet: 14 result(s); latest 10, newest first
row started run-key workload quantity status median revision project
13 2026-11-30 23:30:32 example-again bench_join load-average success 0.77 5308f3ec84 local/runnervm3lb7s
12 2026-11-30 23:30:32 example-again bench_join wall-time success 2.61989e-06 s 5308f3ec84 local/runnervm3lb7s
11 2026-11-30 23:30:30 example-env bench_env_var load-average success 0.75 5308f3ec84 local/runnervm3lb7s
10 2026-11-30 23:30:30 example-env bench_env_var wall-time success 2.28373e-07 s 5308f3ec84 local/runnervm3lb7s
9 2026-11-30 23:30:26 example-all bench_sum_squares load-average success 0.75 5308f3ec84 local/runnervm3lb7s
8 2026-11-30 23:30:26 example-all bench_sum_squares wall-time success 2.46478e-05 s 5308f3ec84 local/runnervm3lb7s
7 2026-11-30 23:30:24 example-all bench_sorted load-average success 0.73 5308f3ec84 local/runnervm3lb7s
6 2026-11-30 23:30:24 example-all bench_sorted wall-time success 7.1141e-06 s 5308f3ec84 local/runnervm3lb7s
5 2026-11-30 23:30:21 example-all bench_join load-average success 0.73 5308f3ec84 local/runnervm3lb7s
4 2026-11-30 23:30:21 example-all bench_join wall-time success 2.38242e-06 s 5308f3ec84 local/runnervm3lb7s
Note that each run has two entries, one per measured quantity (by default: wall time and load average).
6. List the series in the store¶
bx series lists the series from the local store. We can also filter by measured quantities.
$ bx series --quantity wall-time
7be11b2db2b8 local/runnervm3lb7s bench_dict_build wall-time [s] est=benchx/median/v1
0caa092d0ff6 local/runnervm3lb7s bench_env_var wall-time [s] est=benchx/median/v1
7fae02964ab1 local/runnervm3lb7s bench_join wall-time [s] est=benchx/median/v1
ffb0cd88527e local/runnervm3lb7s bench_sorted wall-time [s] est=benchx/median/v1
cd0a65406758 local/runnervm3lb7s bench_sum_squares wall-time [s] est=benchx/median/v1
9a09bc14fe11 local/runnervm3lb7s bench_env_var wall-time [s] est=benchx/median/v1
Note that bench_env_var workload has two entries: we ran it with two values of the envirornment variable, setting -e changes the environment, which benchx tracks:
To filter by the workload use the --workload argument: bx series --quantity wall-time --workload bench_env_var.
7. The history of one series¶
bx history lists the points of one series, one for each run. We take the series of
bench_join and see the two runs of it that we made, example-all and example-again.
The last line fails the example unless there are exactly two points:
$ SERIES=$(bx series --workload bench_join --quantity wall-time | awk 'NR == 1 {print $1}')
$ bx history "$SERIES" | tee out/history.txt
$ [ "$(grep -c example- out/history.txt)" -eq 2 ]
revision order unavailable without a revision graph; listed by measurement start
5308f3ec8413 2026-11-30T23:30:21.766848Z 2.38242e-06 (server) example-all:bench_join:wall-time
5308f3ec8413 2026-11-30T23:30:32.374768Z 2.61989e-06 (server) example-again:bench_join:wall-time
8. The config file¶
An optional benchx.conf.json file collects various configuration settings. Calling
bx config init writes a file with default settings, which you can then customize.
A bad config file is reported before anything runs.
Settings provided as command line arguments take precedence over the config file.
Here is an example:
$ cat benchx.conf.json
{
"schema_version": "benchx/config/0.1.0",
"adapter": {
"name": "pyperf",
"suite": "bench_suite.py",
"precision": {
"repetitions": { "mode": "fixed", "levels": [{ "unit": "process", "n": 5 }, { "unit": "value", "n": 3 }] },
"calibration": { "mode": "adaptive", "minimum_sample_seconds": 0.05 },
"warmup": { "mode": "count", "n_warmup": 1 }
}
},
"paths": { "store": "out/store.parquet", "results": "out" }
}
Here the suite is being with pyperf via its harness adapter; Additional settings are
supplied to pyperf through the work-order:
repetitions.levels:processis the number of worker processes,valuethe values each one records. Leave a level out and it takes pyperf's default (20 and 3).calibration:adaptivewithminimum_sample_secondslets pyperf pick how many calls make up one value;{"mode": "fixed", "n_iterations": N}fixes it.warmup:{"mode": "count", "n_warmup": N}discards N values per process, or{"mode": "none"}.- Every key is optional; pyperf's defaults (20 x 3, adaptive 0.1 s, 1 warmup) fill the rest.
Precision is resolved as: flags, then the config file, then pyperf's defaults (20 processes x 3 values, 0.1 s calibration, 1 warmup). A level you leave out of the file takes pyperf's default.
9. What is written¶
Finally, take a peek into the output directory (typically, you never need to look there unless debugging):
$ ls -l out/all/
total 100
drwxr-xr-x 3 runner runner 4096 Nov 30 23:30 artifacts
-rw-r--r-- 1 runner runner 26037 Nov 30 23:30 bx.log
-rw-r--r-- 1 runner runner 2898 Nov 30 23:30 example-all_bench_dict_build_load-average.json
-rw-r--r-- 1 runner runner 6621 Nov 30 23:30 example-all_bench_dict_build_wall-time.json
-rw-r--r-- 1 runner runner 2892 Nov 30 23:30 example-all_bench_env_var_load-average.json
-rw-r--r-- 1 runner runner 6631 Nov 30 23:30 example-all_bench_env_var_wall-time.json
-rw-r--r-- 1 runner runner 2886 Nov 30 23:30 example-all_bench_join_load-average.json
-rw-r--r-- 1 runner runner 6623 Nov 30 23:30 example-all_bench_join_wall-time.json
-rw-r--r-- 1 runner runner 2890 Nov 30 23:30 example-all_bench_sorted_load-average.json
-rw-r--r-- 1 runner runner 6618 Nov 30 23:30 example-all_bench_sorted_wall-time.json
-rw-r--r-- 1 runner runner 2900 Nov 30 23:30 example-all_bench_sum_squares_load-average.json
-rw-r--r-- 1 runner runner 6643 Nov 30 23:30 example-all_bench_sum_squares_wall-time.json
drwxr-xr-x 2 runner runner 4096 Nov 30 23:30 orders
-rw-r--r-- 1 runner runner 1030 Nov 30 23:30 workorder-544c17387f38dd6cf29afde1f184ad592ed9382a1c7cbf7d7401fb7b21ac1f33.json
benchx stores a host of information:
bx.logfile is the log file of internalbenchxoperations (the default logging level is DEBUG);example-...jsonfiles contain detailed results from individual runs, which are ingested into the parquet store;- "work orders" are internal
benchxway of tracking what to run and how: you can exactly replay a run withbx run workorder-file.json; artifacts/directory contains full details low-level inputs and outputs of harness adapters.