Repository navigation
ENH: warn when both sides of bx compare import the same build - #189
Conversation
545afa2 to
05a7af8
Compare
ev-br
left a comment
There was a problem hiding this comment.
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:
-
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. -
This PR only works for
asvsuites:pyperfadapter does not detect AA comparisons. Ideally, both adapters would share the mechanism.
| 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( |
There was a problem hiding this comment.
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.
|
Below is a Claudish recommendation for adding the same warning to Plan to make the same-build warning work for the pyperf adapter. Nothing is changed yet.
I'd start with A.
Limits. No |
# Conflicts: # src/benchx/session.py
|
thanks @ev-br !
sure, happy to take a look as a follow-up |
|
Macos runners are having a day, it seems. I strongly doubt there's anything platform-specific here. Merging, thanks @MarcoGorelli ! |
closes #185
compare the builds and warn if they're the same
the
if round_ == 0is there so that the same warning isn't repeated for each round (and, to show it as early as possible)