Skip to content

fix(http-core): [#2479] make the HTTP announce benchmark measure the announce - #2509

Merged
josecelano merged 12 commits into
torrust:developfrom
josecelano:2479-fix-http-core-announce-benchmark
Oct 14, 2026
Merged

josecelano merged 12 commits into
torrust:developfrom
josecelano:2479-fix-http-core-announce-benchmark

Conversation

@josecelano

Copy link
Copy Markdown
Member

Summary

The HTTP announce benchmark (http_tracker_core_benchmark) passed an async fn to Criterion's synchronous b.iter, so it timed only the creation of a future that was never polled (about 78-87 ns) and never ran an announce. This PR makes it await one AnnounceService::handle_announce per iteration, and adds a guard that fails cargo test --benches (run by the pre-push hook and CI) when a benchmark's measured routine never runs.

Closes #2479

Changes

  • packages/http-core/benches/: a new AnnounceBenchmarkContext (helpers/announce.rs) builds the tracker services once, outside the measured routine, and the benchmark awaits one announce per iteration with b.to_async(&runtime).iter(...). The benchmark is renamed to http_tracker/announce_once/announce_once, and helpers/sync.rs is removed. Every iteration re-announces the same peer on purpose, so runs are comparable; the context's documentation and docs/benchmarking.md explain why.
  • RoutineGuard (benches/helpers/routine_guard.rs, one identical copy in http-core and one in udp-core): it counts benchmark-closure invocations and completed routines, and assert_routine_ran panics when Criterion invoked the benchmark but no routine completed. Benchmarks that Criterion skips (a non-matching filter, --list) do not trip it.
  • packages/udp-core/benches/udp_tracker_core_benchmark.rs: the same guard on connect_once, which fix(udp-core): inject the UDP connection-cookie cipher #2458 repaired without one.
  • docs/benchmarking.md: removes the Known Defects entry, updates the catalog row, and documents the routine guard, the same-peer rationale, and a caution that editing a benchmark can shift a nanosecond-scale result through code layout. The run-benchmarks skill points new async benchmarks at the guard.
  • docs/issues/open/2479-fix-http-core-announce-benchmark/: maintainer decisions D1-D4, red/green and recheck evidence, and an implementation retrospective.

Results

Benchmark Before After
HTTP announce (renamed to http_tracker/announce_once/announce_once) 78 ns: creates a future, runs 0 announces about 342-348 ns: one announce per iteration

The counter probe from the original reproduction, repeated like-for-like: the routine body ran 0 times in about 12M timed iterations before the fix. After the fix it ran 4,819,101 times in a run with 2.7M timed iterations (the rest are warm-up iterations), and exactly once in Criterion's test mode.

Notes for Reviewers

  • Guard overhead: wrapping the routine in an async fn added 10-15 ns to the roughly 60 ns connect_once benchmark. The guard now uses futures::FutureExt::inspect, which measures the same as a control run with no instrumentation in the timed loop.
  • connect_once reads about 65 ns instead of about 58 ns after this PR, even in that control run. The cause is code layout from editing the benchmark binary, not a ConnectService regression; docs/benchmarking.md now says so.
  • The temporary probes and mutations used for verification are recorded verbatim in the evidence file and are not part of the branch.

Validation

  • cargo test -p torrust-tracker-http-core -p torrust-tracker-udp-core --benches: every benchmark reports Success.
  • R1 was red before the fix (the guard on the unfixed benchmark panics). R1 and R2 are red again under mutate-then-restore with the original defect reintroduced, and green after restore.
  • cargo bench before and after for both guarded benchmarks.
  • The pre-commit hook (linter all, cargo machete, doc tests, and the other fast checks) passed on every commit. The pre-push hook (nightly fmt, check, and doc, plus the full stable test suite) passed on the rebased branch.

Evidence: docs/issues/open/2479-fix-http-core-announce-benchmark/manual-verification-evidence.md.

josecelano and others added 9 commits October 13, 2026 19:10
…mentation

Record the answers to the pre-implementation questions as D1-D4: the
guard only fails when Criterion invoked the benchmark closure, the
benchmark is renamed to http_tracker/announce_once, the same peer is
announced on every iteration (with the reasons), and the guard helper
is duplicated per package.

Co-authored-by: Copilot <[email protected]>
…mark guard

The routine guard, wired into the unfixed HTTP announce benchmark,
fails cargo test because the measured routine never ran. A filter that
matches nothing and --list still exit 0.

Co-authored-by: Copilot <[email protected]>
…re the announce

The benchmark passed an async fn to Criterion's synchronous b.iter, so
it timed only the creation of a future that was never polled (about
80 ns) and never ran an announce.

Build the tracker services and the announce service once, outside the
measured routine, and await one announce per iteration with
b.to_async(&runtime).iter(...). Rename the benchmark to
http_tracker/announce_once/announce_once. Every iteration announces the
same peer, so the benchmark times a repeat announce; the context
documents why.

Add a routine guard that fails the benchmark, including under
cargo test --benches, when Criterion invoked it but the measured routine
never ran. Benchmarks that Criterion skips do not trip it.

Co-authored-by: Copilot <[email protected]>
…oll-level combinator

The routine guard wrapped the measured future in an async fn, which
added 10-15 ns per iteration to the 60 ns udp-core connect_once
benchmark. Use futures::FutureExt::inspect instead; its cost is not
measurable against a control with no instrumentation in the timed loop.

Co-authored-by: Copilot <[email protected]>
…st a routine that never runs

Add the routine guard used by the http-core announce benchmark: it
fails the benchmark, including under cargo test --benches, when
Criterion invoked it but the measured connect never ran. This is the
defect issue torrust#2458 repaired without a guard. Benchmarks that Criterion
skips do not trip it.

The guard helper is a copy of the http-core one, so udp-core gains no
new dependency.

Co-authored-by: Copilot <[email protected]>
…vestigation, and reviews

Record the R1 green run and its before-and-after cargo bench output,
the R2 mutate-then-restore red run, the guard-overhead measurements
that led to the poll-level combinator, both prose-first test-design
reviews, and the maintainer review (T4). Replace the deleted sync.rs
helper in the related artifacts with the new benchmark helpers, and
correct three timestamps that were written in local time instead of
UTC.

Co-authored-by: Copilot <[email protected]>
… fixed announce benchmark

Remove the Known Defects entry for the HTTP announce benchmark, which
now awaits one announce per iteration, and update its catalog row.
Describe the routine guard that fails async benchmarks whose measured
routine never runs, explain why the announce benchmark re-announces the
same peer, and warn that editing a benchmark can shift a
nanosecond-scale result through code layout alone.

Co-authored-by: Copilot <[email protected]>
… completion review

Record V2, the repeat of the V1 counter probe on the fixed benchmark:
the routine body ran 4,819,101 times in a run of 2.7M timed
iterations, and once in test mode. Mark T5 and the acceptance criteria
done, add the implementation retrospective, and correct the T1 progress
entry to its commit time.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 13, 2026 18:11
@josecelano
josecelano requested a review from a team as a code owner October 13, 2026 18:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation meets the acceptance criteria; only a non-blocking documentation clarification remains.

1 open finding
What changed in this PR

Fixes the HTTP benchmark so it measures one awaited announce rather than an unpolled future.

Changes:

  • Adds async benchmark contexts and routine-execution guards.
  • Applies the guard to HTTP announce and UDP connect benchmarks.
  • Updates benchmark guidance and issue evidence.
File Description
packages/​http-core/​benches/​http_tracker_core_benchmark.rs Awaits one announce per iteration.
packages/​http-core/​benches/​helpers/​announce.rs Adds reusable benchmark context.
packages/​http-core/​benches/​helpers/​routine_guard.rs Detects unexecuted routines.
packages/​http-core/​benches/​helpers/​mod.rs Registers new helpers.
packages/​http-core/​benches/​helpers/​sync.rs Removes defective helper.
packages/​udp-core/​benches/​udp_tracker_core_benchmark.rs Guards connect benchmark execution.
packages/​udp-core/​benches/​helpers/​routine_guard.rs Adds equivalent UDP guard.
packages/​udp-core/​benches/​helpers/​mod.rs Registers the guard.
docs/​benchmarking.md Documents corrected benchmark and guard.
.github/​skills/​dev/​benchmarking/​run-benchmarks/​SKILL.md Recommends guards for async benchmarks.
docs/​issues/​open/​2479-fix-http-core-announce-benchmark/​ISSUE.md Records implementation completion.
docs/​issues/​open/​2479-fix-http-core-announce-benchmark/​manual-verification-evidence.md Records red/green and benchmark evidence.
docs/​issues/​open/​2479-fix-http-core-announce-benchmark/​implementation-retrospective.md Captures implementation lessons.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread docs/benchmarking.md Resolved
@josecelano josecelano self-assigned this Oct 13, 2026
…shift to the control build

The text said adding the routine guard moved connect_once from about
58 ns to 65 ns with nothing added to its timed loop, but the committed
guard does add track() to the loop. Say that the byte-identical timed
loop was a control build, and that the committed guard measures the
same. Apply the same correction to the issue retrospective.

Co-authored-by: Copilot <[email protected]>
@josecelano

Copy link
Copy Markdown
Member Author

ACK 258a603

@josecelano
josecelano merged commit 8af0071 into torrust:develop Oct 14, 2026
21 checks passed
josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Oct 14, 2026
…closed

Move the issue torrust#2479 specification, its evidence, and its
retrospective to docs/issues/closed/, mark it done, record the PR torrust#2509
merge, and point the PR torrust#2480 and torrust#2509 review audits at the new path.

Co-authored-by: Copilot <[email protected]>
@josecelano
josecelano deleted the 2479-fix-http-core-announce-benchmark branch October 14, 2026 06:32
josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Oct 14, 2026
…closed

Move the issue torrust#2479 specification, its evidence, and its
retrospective to docs/issues/closed/, mark it done, record the PR torrust#2509
merge, and point the PR torrust#2480 and torrust#2509 review audits at the new path.

Co-authored-by: Copilot <[email protected]>
josecelano added a commit that referenced this pull request Oct 14, 2026
…ssues/closed

6bb5d74 chore(issues): archive closed issue #2479 spec to docs/issues/closed (Jose Celano)

Pull request description:

  Archives the specification for issue #2479 (closed on GitHub by the merge of PR #2509) from `docs/issues/open/` to `docs/issues/closed/`.

  - Verified that issue #2479 is `CLOSED` on GitHub.
  - Moved `ISSUE.md`, `manual-verification-evidence.md`, and `implementation-retrospective.md`.
  - Updated the frontmatter: `status: done`, `spec-path`, `issue-spec`, `related-artifacts`, and `last-updated-utc`.
  - Checked the "Issue closed and spec moved" checkpoint and added a progress-log entry recording the PR #2509 merge. "Reviewer validated acceptance criteria" stays unchecked, because only Copilot reviewed the PR before it merged.
  - Pointed the frontmatter `related-artifacts` of the PR #2480 and PR #2509 review audits at the new path. The one remaining mention of the old path is a historical progress-log entry.
  - Pre-commit and pre-push hooks passed.

  Related to #2479

ACKs for top commit:
  josecelano:
    ACK 6bb5d74

Tree-SHA512: 6db3edad00b2cd5e6fed77095a0a975cd1e77774c66d284e8473224ef215f39c5de49a3a5c3c87c787663cc143ae09c5d9eeaf27299a07fffc0a7f9354d44592

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed at 258a6037041e19a2081fcaa5a9932e505ce3ac87 (post-merge). Recomputed from the bytes at the merged head, against the merge base b5f1801c6; the PR merged at 06:25:01Z as 8af007187.

Label: POST_MERGE_FINDINGS — the review request of 20:34Z was received while this reviewer's loop was paused, and the PR merged before the round ran. The findings below stand at the merged head and are for a follow-up; no ACK follows a merged PR.

The fix. AnnounceBenchmarkContext::new (benches/helpers/announce.rs:39-61) builds the services and moves them into the AnnounceService once, under runtime.block_on (http_tracker_core_benchmark.rs:15); announce_once (announce.rs:67-77) only borrows the request, IP sources and binding, so the timed routine clones nothing, and every iteration announces the same peer (D3, ISSUE.md:116). Runtime::new() (:14) is the pattern docs/benchmarking.md:111 gives and hosts the statistics listener new() spawns (helpers/util.rs:89-96). 500 ms / 1 s match udp-core and udp-server. helpers/sync.rs has no user left; the old names survive only as before-state records in issue docs, and no workflow or script names them.

The guard. assert!(benchmark_invocations == 0 || routine_runs > 0, …) (routine_guard.rs:52-56) fails only when the closure ran and no routine completed; zero invocations pass, as D1 and the docs say. Relaxed is enough: Criterion 0.5.1's async iter runs its loop through Runtime::block_on (tokio 1.53.2), which polls on the calling thread, so both counters are written and read on the main thread in program order. The two copies are byte-identical (sha256 083e1b6f…). futures is a normal dependency of both packages (http-core/Cargo.toml:23, udp-core/Cargo.toml:25), so no manifest change. cargo test --benches runs in CI (testing.yaml:114) and pre-push (pre-push.sh:51).

The udp-core wiring. Only the guard, the GROUP/BENCHMARK constants (same names) and helpers/mod.rs +1 change. R2 is red under mutate-then-restore at 52:9, the head's assert line (evidence :355-366).

Evidence and spec. The red run (17:35Z, evidence :139) and e852009fd (17:41:34Z) come before 068c976ce (17:42:07Z); author times are +01:00. Both V2 counts split exactly into Criterion's doubling warm-up plus 100 linear samples: 4,819,101 = (2^21−1) + 539·5050 and 4,531,251 = (2^21−1) + 482·5050. The evidence puts the excess down to warm-up (:495-496) and shows one run in test mode (:481). The three-run rule (benchmarking.md:71) covers changes to performance-critical code, and this PR changes none (ISSUE.md:96). D1–D4, T1–T5 DONE, AC1–AC4 with evidence; stamp 18:12 = last log entry = 89c4cf855; related-pr: 2509. F3, F4.

Documentation. Known Defects is gone. The catalog row (benchmarking.md:38) links the new section (:411), which gives all three D3 reasons (:433-441); the layout caution (:81-85) now names the control build, which resolves F1. The skill gains its pointer (SKILL.md:41-43). F2.

The audit record. Template sections and Completion Rules verbatim; the F1 row and detail carry review-finding:pr-2509-f1, and the fix is cited by subject (PR-REVIEW.md:75). validate-audit-record.py, run offline on the captured comments with --base b5f1801c6, reports ok.

Hygiene. 12 Conventional subjects tagged [#2479], each scoped to its files; bodies match the diffs; author = committer; author times ascend (the first ten were re-committed by the logged rebase); one trailer line each; no banned token. git diff --check flags only blank context lines inside the verbatim V2 probe diff (evidence :436/453/462), the same form the base's V1 probe has. Closes #2479 worked. The title is the fix subject, though the PR also adds the guard and docs (note only).

Findings

  • F2 [Minor] docs/benchmarking.md:119: the guard section calls the two guarded benchmarks "the async Criterion benchmarks", but other async ones exist.
  • F3 [Minor] manual-verification-evidence.md:233: a mutate-then-restore output carries a panic line its stated code state cannot produce.
  • F4 [Nit] ISSUE.md:166: three spec statements describe code this PR did not ship.
  • F5 [Nit] announce.rs:59: .unwrap() with no message.

Checked, no finding: Relaxed ordering; the skip semantics; no per-iteration clone; the runtime and timings; probe arithmetic; F1's fix; the 10–15 ns figure (relative to the control, :313-322); retrospective sections; Rust lines ≤ 121; no manifest change; no skill-version bump (the repository does not bump consistently).

Checks

On the loop's compute hub at the merged head, merge base b5f1801c6 (receipt server-gates-krkavec-260): pre-commit profile gate exit 0 (88 s); linter markdown, linter cspell, linter lychee exit 0; frontmatter-validator --all: 19 errors / 4 warnings at this head and 19 / 4 at b5f1801c6 re-measured, none in the files this PR touches; cargo test --benches exit 0 for torrust-tracker-http-core (19 s) and torrust-tracker-udp-core (32 s); cargo clippy --all-targets -- -D warnings exit 0 with no diagnostics and cargo fmt --check exit 0 for both packages; validate-audit-record.py --pr-number 2509 exit 0 (1 row, 3 log entries, 0 failures). GitHub checks at the merged head at posting time: "Publish (Development): completed/skipped; Publish (Release): completed/skipped; Context: completed/success; Package Coverage Regression: completed/success; Docker E2E: completed/skipped; Layer Boundary Bans: completed/success; Unit (stable): completed/success; Unit (nightly): completed/success; Documented Clippy Allows: completed/success; Package Coverage (torrust-tracker-udp-core): completed/success; Package Coverage (torrust-tracker-http-core): completed/success; Test (Docker) (release): completed/success; Generate Coverage Report: completed/success; Discover Package Coverage: completed/success; Build on macos-latest (stable): completed/success; Build on macos-latest (nightly): completed/success; Build on windows-latest (stable): completed/success; Build on ubuntu-latest (stable): completed/success; Build on ubuntu-latest (nightly): completed/success; Build on windows-latest (nightly): completed/success; Docs Lint: completed/success". At posting, develop is cbdbce9c1.

Comment thread docs/benchmarking.md
`sync::return_announce_data_once` to `b.iter` without awaiting it, and the runtime it builds is
unused, so it measures only the creation of a future. Its results are not meaningful until it is
fixed the same way.
Comparing with a known cost is a manual check. The async Criterion benchmarks, `udp-core`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Minor][F2] This section calls connect_once and announce_once "the async Criterion benchmarks", but there are other async Criterion benchmarks, and they have no guard.

  • udp-server's udp_tracker/handle_scrape_once/74_torrents (packages/udp-server/benches/udp_tracker_server_benchmark.rs:103, bench.to_async(&runtime).iter(|| async { … })).
  • The to_async benchmarks in packages/torrent-repository-benchmarking/benches/repository_benchmark.rs (from line 31).

The retrospective says the same thing ("Both async Criterion benchmarks", implementation-retrospective.md:24-25; "Revisit if a third async benchmark needs it", :84). The spec's Out of Scope (ISSUE.md:98-99) also calls the repository benchmarks synchronous.

Leaving the scrape benchmark unguarded was the spec's decision and is fine. The problem is that the text overstates coverage, so a reader can assume an unguarded benchmark is protected.

Follow-up: say that two async Criterion benchmarks carry the guard and name the unguarded async ones, or add the guard to them. Make the same correction in the retrospective and the archived spec under docs/issues/closed/2479-fix-http-core-announce-benchmark/, where this text still stands on develop.

$ cargo test -p torrust-tracker-http-core --bench http_tracker_core_benchmark # mutated
Testing http_tracker/announce_once/announce_once
Success
thread 'main' (2108138) panicked at packages/http-core/benches/helpers/routine_guard.rs:43:9:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Minor][F3] This mutate-then-restore output is labelled with the fix commit's code state, but it reports a panic location that code cannot produce.

  • The section's code state (line 212) is the commit fix(http-core): [#2479] make the HTTP announce benchmark measure the announce. In that commit, the guard's assert! is on line 46 of routine_guard.rs.
  • After the refactor(http-core) commit it is on line 52, which the later runs at lines 359 and 375 match.
  • This run panics at routine_guard.rs:43:9, so it came from an uncommitted intermediate helper.
  • The note at lines 143-146 says the committed helper has "two more documentation lines" than the 41:9 draft. That also predicts 43, not 46.

The inspect recheck at line 375 still shows R1 red against committed code, so the guard itself is not in doubt; what is wrong is the record of which code produced this output.

Follow-up: in the archived evidence file, label this as an intermediate-draft run (or replace it with a run on the fix commit) and correct the line arithmetic at 143-146.

after `bench_function`, the benchmark panics with a message naming the benchmark if the count is
zero. It runs under `cargo test --benches` (pre-push and CI) and under `cargo bench`.
after `bench_function`, the benchmark panics with a message naming the benchmark if Criterion
invoked the benchmark closure and the count is zero (D1). It runs under `cargo test --benches` (pre-push and CI) and under `cargo bench`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Nit][F4] Three statements in the spec describe code this PR did not ship:

  • Line 164 says "the benchmark context counts completed announces", but the counters live in RoutineGuard, not in AnnounceBenchmarkContext.
  • Line 324 says the counter probe is "a temporary edit of packages/http-core/benches/helpers/sync.rs", but this PR deletes that file, and the V2 probe edits helpers/announce.rs (evidence line 427).
  • Line 358 cites docs/benchmarking.md "(Known Defects; …)" under References, but this PR removes that section.

Line 164 is outside the diff, so this comment sits on the nearest changed line of the same R1 paragraph.

Follow-up: correct all three in the archived spec, where they stand at lines 164, 330 and 364 on develop.

announce_service,
announce_request,
client_ip_sources,
server_service_binding: ServiceBinding::new(Protocol::HTTP, server_socket_addr).unwrap(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Nit][F5] This .unwrap() is one the review checklist asks to avoid. The same PR gave the runtime (http_tracker_core_benchmark.rs:14) and the announce (announce.rs:76) expect messages; this line was carried over unchanged from the deleted helpers/sync.rs, so a panic here prints no reason.

Follow-up: use .expect("the fixed benchmark address should be a valid HTTP service binding"), which matches the # Panics section at lines 36-38.

This branch was previously deployed

1 inactive deployment
coverage — 258a6037 Deployed Oct 13, 2026 by josecelano via Generate Coverage Report #2501
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.

fix(http-core): make the HTTP announce benchmark measure the announce

3 participants