Skip to content

ENH: warn when both sides of bx compare import the same build - #189

Merged
ev-br merged 5 commits into
benchx-dev:mainfrom
MarcoGorelli:same-build-warning
Dec 4, 2026
Merged

ev-br merged 5 commits into
benchx-dev:mainfrom
MarcoGorelli:same-build-warning

Conversation

@MarcoGorelli

@MarcoGorelli MarcoGorelli commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

closes #185

compare the builds and warn if they're the same

the if round_ == 0 is there so that the same warning isn't repeated for each round (and, to show it as early as possible)

@MarcoGorelli
MarcoGorelli marked this pull request as ready for review December 3, 2026 14:17

@ev-br ev-br left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked Claude to cook up a reproducer for gh-185, and the result is under the fold

Details
$ cat repro.sh 
#!/usr/bin/env bash
# Reproducer for benchx-dev/benchx#185: `bx compare` times one installed copy of a project on both
# sides, and does not say so.
#
#   usage: repro.sh [BENCHX_CHECKOUT [asv|pyperf]]    (default: the checkout this script is in; both adapters)
#   needs: uv, git, network (for PyPI)
#
# What it does: makes two versions of a tiny package `mylib` (rev-a does 100 times the work of rev-b),
# installs only rev-a into the Python that bx will use, and runs `bx compare rev-a rev-b`. Both sides
# then import the installed rev-a, so the comparison should find no difference although the versions
# differ a lot. The script prints whether bx told you about it: on screen, or only in its log file.
# The trees use a src layout, so `mylib` is not importable from a tree's root and each side gets the
# installed copy.
#
# Run it once per benchx checkout, e.g. upstream/main and the branch with the fix.
set -eu

BENCHX=$(cd "${1:-$(dirname "$0")/..}" && pwd)  # default: the checkout this script is in
WHICH=${2:-both}
W=$(mktemp -d "${TMPDIR:-/tmp}/bx185.XXXXXX")
cd "$W"

uv venv -q venv
uv pip install -q --python venv/bin/python "$BENCHX"
export PATH="$W/venv/bin:$PATH"

mktree() {  # NAME VERSION WORK
    mkdir -p "$1/src/mylib" "$1/asv_bench/benchmarks"
    printf 'def work():\n    sum(range(%s))\n' "$3" > "$1/src/mylib/__init__.py"
    printf '[project]\nname = "mylib"\nversion = "%s"\n[build-system]\nrequires = ["setuptools"]\nbuild-backend = "setuptools.build_meta"\n' \
        "$2" > "$1/pyproject.toml"
    printf '{"version": 1, "project": "mylib"}\n' > "$1/asv_bench/asv.conf.json"
    printf 'import mylib\n\n\ndef time_work():\n    mylib.work()\n' > "$1/asv_bench/benchmarks/bench_work.py"
    printf 'def bench_work():\n    import mylib\n\n    mylib.work()\n' > "$1/bench_mylib.py"
    git -C "$1" init -q
    git -C "$1" add -A
    git -C "$1" -c user.name=repro -c user.email=repro@example.com commit -qm "$1"
}
mktree rev-a 1.0 20000
mktree rev-b 2.0 200
uv pip install -q --python venv/bin/python ./rev-a   # only rev-a is installed

cat <<EOF
bx under test: $BENCHX ($(git -C "$BENCHX" log -1 --format='%h %s'))

WHAT THIS SHOWS
  Two versions of a tiny project: rev-a (slow) and rev-b (fast, 100 times less work per call).
  Only rev-a is installed in the Python that bx uses, so bx can only ever time rev-a.
  We ask bx to compare rev-a with rev-b, and check whether it tells us that both sides
  used the same installed copy.

THE REAL SPEEDS, timed directly (the installed copy is $(python -c 'import mylib; print(mylib.__file__)'))
EOF
for r in a b; do
    printf '  rev-%s: ' "$r"
    PYTHONPATH="rev-$r/src" python -m timeit -s 'import mylib' 'mylib.work()'
done
echo "  So rev-b is much faster. A correct comparison reports that, or says it cannot tell."

FLAGS="--profile revisions --rounds 5 --processes 2 --values 3 --warmups 1 --no-ingest --source-uri https://example.com/mylib.git"

compare() {  # ADAPTER SUITE
    echo; echo "=== bx compare rev-a rev-b, $1 benchmarks"
    rm -rf results
    screen=$(bx compare rev-a rev-b --adapter "$1" --suite "$2" $FLAGS 2>&1 || true)
    echo "bx shows on screen:"
    printf '%s\n' "$screen" | grep -v '^$' | grep -vE '^round [0-9]/' | sed 's/^/    /'
    log=$(ls "$PWD"/results/*/bx.log)
    on_screen=$(printf '%s\n' "$screen" | grep -c 'both sides imported' || true)
    in_log=$(grep -c 'both sides imported' "$log" || true)
    echo "Did bx say that both sides used the same installed mylib?"
    echo "    on screen:       $([ "$on_screen" -gt 0 ] && echo yes || echo no)"
    echo "    in its log file: $([ "$in_log" -gt 0 ] && echo yes || echo no)   ($log)"
    if [ "$on_screen" -gt 0 ]; then
        echo "RESULT: OK, bx warned on screen."
    elif [ "$in_log" -gt 0 ]; then
        echo "RESULT: PARTLY. bx noticed, but wrote it only to the log file; you would not see it unless you open the log or use -v."
    else
        echo "RESULT: BUG. bx did not notice, and said nothing, although both sides timed the same code."
    fi
}
[ "$WHICH" = asv ] || [ "$WHICH" = both ] && compare asv asv_bench/benchmarks
[ "$WHICH" = pyperf ] || [ "$WHICH" = both ] && compare pyperf bench_mylib.py
true

The repro script shows two potential issues:

  1. The warning is logged but not shown on the screen. There's a one-line fix below.
    Maybe we should also consider making a pass over the whole logging/output across the library and make it consistently emit warnings by default? Here or in a separate PR.

  2. This PR only works for asv suites: pyperf adapter does not detect AA comparisons. Ideally, both adapters would share the mechanism.

Comment thread src/benchx/session.py Outdated
aa = sides[0]["identity"].get("tree") == sides[1]["identity"].get("tree")
if aa or None in installed or installed[0] != installed[1]:
return
logger.warning(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude's recommendation is to change logger.warning with log.console(__name__).warning, so that the message displays in the terminal with the default logging level.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 9a9ac72

@ev-br

ev-br commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Below is a Claudish recommendation for adding the same warning to pyperf suites, verbatim.
IMO to really close gh-185 it's preferable to fix both adapters.


Plan to make the same-build warning work for the pyperf adapter. Nothing is changed yet.

  1. Which package is the project (the open decision). asv gets the name from asv.conf.json. A pyperf
    suite has no such file, so the options are:
  • A. Read [project] name from the tree's pyproject.toml, with - turned into _. This needs no new
    setting, and it stays silent when the name differs from the import name.
  • B. A new setting in benchx.conf.json that names the package to import.
  • C. A as the default, with B as an override.

I'd start with A.

  1. What is written. The generated pyperf benchmark script (in adapters/pyperf.py) writes this into
    each result under provenance → info → pyperf → installed:
    {"name": "mylib", "version": "1.0", "path": "…/site-packages/mylib/__init__.py"}
  • It is written inside the benchmark process, after the suite file is loaded and before timing, as asv
    does.
  • path comes from asking Python which file it would import for mylib. The asv way, looking at the
    modules already imported, would miss suites that import inside the benchmark function, as my
    reproducer's and tests/test_build_import_path.py's do.
  • The code that builds this note is shared with asv, not copied.
  1. What is read. After round 1, bx opens one result file from each side and compares the two installed
    objects, as it does today. The only change is that it reads info.<adapter name>.installed, so
    info.pyperf.installed for pyperf, instead of only info.asv.installed. If the objects are equal and the
    two trees differ, it warns.

  2. Tests.

  • A real pyperf run whose result contains the installed note.
  • A session test with the pyperf adapter, copied from the asv ones.
  • A flat-layout project, where each side imports its own tree: no warning.
  • The reproducer's pyperf line should change from BUG to a warning.

Limits. No pyproject.toml, or a name that isn't the import name, means no warning. That is the same
limit asv has.

@MarcoGorelli
MarcoGorelli marked this pull request as draft December 4, 2026 11:43
@MarcoGorelli

MarcoGorelli commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

thanks @ev-br !

  1. sure, have gone with option a
  2. sure. and if it's not already been imported, use importlib.util.find_spec. done using a shared helper
  3. done (comparing "path")
  4. added tests

Maybe we should also consider making a pass over the whole logging/output across the library and make it consistently emit warnings by default? Here or in a separate PR.

sure, happy to take a look as a follow-up

@MarcoGorelli
MarcoGorelli marked this pull request as ready for review December 4, 2026 15:01
@ev-br

ev-br commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Macos runners are having a day, it seems. I strongly doubt there's anything platform-specific here. Merging, thanks @MarcoGorelli !

@ev-br
ev-br merged commit a3a8d28 into benchx-dev:main Dec 4, 2026
19 of 23 checks passed
@ev-br ev-br added bug Something isn't working component:comparator labels Dec 4, 2026
@ev-br ev-br added this to the 0.3.0 milestone Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make bx compare warn or error when both targets import the same build

2 participants