# How to embed a BenchX comparison in Sphinx This run compares the time to sum an Int64 series in pandas and Polars. Both libraries received the same integers. BenchX timed the sum after constructing the series and alternated the two implementations over six rounds. ```{benched-comparison} ../../examples/benchx/comparison.json --- results: ../../examples/benchx/measurements.zip benchmark: bench_series_sum --- ``` Each timing is the median of the per-attempt medians used in the comparison. The ratio divides the contender timing by the baseline timing. BenchX calculates the paired effect across rounds and supplies the verdict. A negative effect means less time for this operation. BenchX flagged a difference between the requested and observed worker environments on both sides of this run. ## Embed a saved comparison Enable `benched.sphinx` in your Sphinx extensions. Give the directive a BenchX variants comparison saved with `bx compare --json` and the directory of measurement files from the same run. Both paths are relative to the page. To select a single workload, supply its exact name. ```rst .. benched-comparison:: comparison.json :results: results :benchmark: bench_series_sum ``` The directive reads version 0.1.0 comparison and measurement documents with the observation median estimator. It also accepts a ZIP of the measurement JSON files at the archive root. Sphinx builds the table offline and uses its theme to render it. An indeterminate verdict includes BenchX’s reason. An incomplete set of referenced files produces a build error. ## Publish the comparison as standalone HTML From the directory containing the downloaded example files, run: ```bash benched report --benchx-comparison comparison.json measurements.zip --output site ``` Open or publish `site/index.html`. The file contains the same table as the Sphinx directive and works offline. To publish one workload from a larger comparison, add `--benchmark` followed by its exact name. ## Show measurements from a single benchmark Enable `benched.sphinx` and point `benched-measurements` at a BenchX 0.1.0 measurement JSON file. For a directory or ZIP, place the JSON files at its root. Select a workload by exact name with `:benchmark:`. Use `:run:` to select the BenchX run key when the source contains measurements from multiple matching runs. ```rst .. benched-measurements:: measurements.zip :benchmark: bench_series_sum :run: series-sum ``` Each row shows the attempt, workload parameters, quantity, unit, and status. Benched displays each estimate supplied by the producer. For an attempt with only observations, it takes their median. Censored attempts show the recorded bound. Partial, skipped, and failed attempts retain their recorded reason.
Show the 36 recorded series-sum attempts ```{benched-measurements} ../../examples/benchx/measurements.zip --- benchmark: bench_series_sum run: series-sum --- ```
## Reproduce the example BenchX's pyperf adapter runs the same `bench_series_sum(implementation, size)` function used by the [pytest example](customize-sphinx-report.md#publish-a-standalone-pandas-and-polars-comparison). The module constructs the six Int64 series when it loads. Each timed call looks up the requested series and sums it. Run the following from the Benched checkout with pandas, Polars, and BenchX installed. The saved example used pandas 3.1.1, Polars 2.2.0, and [BenchX at 84595be](https://github.com/benchx-dev/benchx/tree/84595be887a814ac400a79814b26f0525040bbf8). ```bash mkdir -p build/series-sum export BENCHX_SETUP_FILE="$PWD/build/series-sum/packages.json" python -c 'import json; from importlib.metadata import version; print(json.dumps({"packages": {p: version(p) for p in ("pandas", "polars")}}))' > "$BENCHX_SETUP_FILE" bx compare . --profile variants --adapter pyperf \ --suite docs/examples/pytest/series_sum.py --vary implementation \ --baseline "'pandas'" --rounds 6 --processes 1 --values 3 --min-time 0.01 --warmups 1 \ --project benched --run-key series-sum --no-ingest --out build/series-sum/results \ --json > build/series-sum/comparison.json ``` The setup file records installed versions with the measurements. Benched uses the version when a side’s parameter value matches a package name. The downloadable example retains all 36 timing measurements. The hostname was anonymized and local artifact references removed before regenerating the comparison. {download}`Benchmark source <../../examples/pytest/series_sum.py>` · {download}`Measurement files <../../examples/benchx/measurements.zip>` · {download}`Comparison document <../../examples/benchx/comparison.json>`