Skip to content

test: add direct-call baseline and threaded-contention benchmarks - #129

Open
IIIIbntttt wants to merge 3 commits into
bagowix:mainfrom
IIIIbntttt:perf/bench-baseline-contention
Open

test: add direct-call baseline and threaded-contention benchmarks#129
IIIIbntttt wants to merge 3 commits into
bagowix:mainfrom
IIIIbntttt:perf/bench-baseline-contention

Conversation

@IIIIbntttt

@IIIIbntttt IIIIbntttt commented Aug 6, 2026

Copy link
Copy Markdown

Summary

Closes the two gaps left open in #84 after the CodSpeed suite landed (per the status check in the issue comments):

  • Direct-call baseline. test_baseline_direct_call measures the unwrapped callable, so any call-path result divides into an overhead ratio — the number a prospective user actually wants. Its async twin runs the bare coroutine on the same pre-built asyncio.Runner the async benchmarks use, so the loop-dispatch cost the async paths pay is present in their baseline too and the ratio stays honest.
  • Threaded contention around one breaker. benchmarks/test_contention.py drives one closed breaker from four worker threads (25 calls each, pool reused across iterations so thread startup is excluded), tracking the work under the shared lock as a trend. The module docstring and CONTRIBUTING.md carry the caveat: CodSpeed's simulation mode counts instructions under Valgrind, which runs threads one at a time — the number is lock-path work, not wall-clock contention. Correctness under real contention stays with tests/test_concurrency.py / Run the test suite on free-threaded CPython (3.14t) #102.

No runtime code is touched; the CodSpeed workflow needs no change (it collects benchmarks/ wholesale). docs/ is intentionally untouched — the correctness.md CodSpeed paragraph stays accurate as written; the reading guidance is contributor-facing and lives in CONTRIBUTING.md.

Checklist

  • Tests added or updated (suite stays at 100% coverage)
  • uv run ruff format --check and uv run ruff check pass
  • uv run mypy, uv run pyright and uv run pyrefly check pass
  • Docs updated (docs/) for user-facing changes
  • CHANGELOG.md [Unreleased] updated
  • Commits follow Conventional Commits

Related issues

Closes #84

Summary by CodeRabbit

  • Documentation

    • Added guidance for interpreting direct-call baseline results and calculating protected-call overhead.
    • Clarified how threaded contention benchmarks measure work performed under lock protection.
    • Documented the new benchmark coverage in the unreleased changelog.
  • Tests

    • Added synchronous and asynchronous baseline performance benchmarks.
    • Added a four-worker threaded contention benchmark covering repeated protected calls and breaker state validation.

Close the two gaps left open in bagowix#84 after the CodSpeed suite landed:

- test_baseline_direct_call and its async twin measure the unwrapped
  callable, so any call-path result divides into an overhead ratio —
  the number a prospective user actually wants.
- benchmarks/test_contention.py drives one closed breaker from four
  worker threads to track the work under the shared lock as a trend.
  The docstring and CONTRIBUTING.md spell out that instruction counting
  runs threads one at a time: the number is lock-path work, not
  wall-clock contention.
bagowix
bagowix previously approved these changes Aug 6, 2026

@bagowix bagowix left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for picking this up — this closes out #84 exactly as scoped in the status check, and the execution is careful: the pool is reused across iterations so thread startup stays out of the measurement, the warm-up assert both validates correctness and fills the window to steady state before measuring, and the Valgrind caveat is spelled out right where the number will be read. I ran the full gate locally on the branch (ruff, mypy, pyright, pyrefly, full suite at 100% coverage, pytest benchmarks --codspeed -n 0) — all green, and the workflow picks up the new module with no changes, as advertised.

One non-blocking nit, take it or leave it: the async baseline is slightly asymmetric with test_call_async. The breaker path runs a wrapper coroutine (guarded) that awaits breaker.call(...), while the baseline runs the bare _async_work coroutine with no wrapper — so the overhead ratio includes one extra coroutine frame that real usage doesn't pay (in real code the user's own coroutine exists in both cases: await fn(...) vs await breaker.call(fn, ...)). Wrapping the baseline the same way (async def bare(): return await _async_work(1, 2)) would make the ratio exactly apples-to-apples. For a trend metric it's noise either way, so happy to merge as is.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7e4b2e53-7267-456a-8e4b-f77bb68be0a9

📥 Commits

Reviewing files that changed from the base of the PR and between 1524047 and 84fffba.

📒 Files selected for processing (4)
  • CHANGELOG.md
  • CONTRIBUTING.md
  • benchmarks/test_call_paths.py
  • benchmarks/test_contention.py

📝 Walkthrough

Walkthrough

The benchmark suite adds synchronous and asynchronous direct-call baselines and a four-worker threaded contention benchmark. Documentation explains baseline comparisons and the limits of serialized instruction-count measurements.

Changes

Benchmark coverage

Layer / File(s) Summary
Direct-call baseline benchmarks
benchmarks/test_call_paths.py, CONTRIBUTING.md
Adds synchronous and asynchronous direct execution benchmarks. Documents baseline comparisons for protected-call overhead.
Threaded contention benchmark
benchmarks/test_contention.py, CHANGELOG.md
Adds a reusable four-worker benchmark for concurrent calls through a shared closed CircuitBreaker. Validates aggregate results and breaker state. Documents instruction-counting limitations.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: bagowix

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the added direct-call baseline and threaded-contention benchmarks.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
⚔️ Resolve merge conflicts 💡
  • Resolve merge conflict in branch perf/bench-baseline-contention
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

The breaker path drives a wrapper coroutine that awaits breaker.call,
while the baseline ran the bare coroutine — so the overhead ratio
charged the breaker for one coroutine frame real usage pays on both
sides. Wrapping the baseline the same way makes the ratio isolate the
breaker itself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a repeatable performance benchmark suite for the hot path

2 participants