Skip to content

Benchmark safe_label_str hello and handshake-reject paths#1687

Merged
bdraco merged 2 commits into
mainfrom
benchmark-noise-hello-sanitize
May 20, 2026
Merged

Benchmark safe_label_str hello and handshake-reject paths#1687
bdraco merged 2 commits into
mainfrom
benchmark-noise-hello-sanitize

Conversation

@bdraco
Copy link
Copy Markdown
Member

@bdraco bdraco commented May 20, 2026

What does this implement/fix?

Adds CodSpeed benchmarks for the three sites where safe_label_str runs on the noise wire-format hot path; this lands before #1669 so the cimport rewiring there has a measurable before/after baseline instead of going in blind.

test_noise_hello_parse drives a hello frame through a fresh MockAPINoiseFrameHelper, covering _handle_hello and its two sanitize calls (server name, MAC). test_noise_handshake_reject_parse drives hello + reject so _error_on_incorrect_preamble runs the explanation sanitize. test_safe_label_str_throughput is a parametrized direct benchmark over realistic name, MAC and explanation inputs, both clean and with embedded control bytes, so any regression in the printable filter or length cap shows up directly.

No production code is touched, only tests/benchmarks/test_noise.py.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

  • n/a

Pull request in esphome (if applicable):

  • n/a, no protocol changes.

Checklist:

  • The code change is tested and works locally.
  • If api.proto was modified, a linked pull request has been made to esphome with the same changes.
  • Tests have been added to verify that the new code works (under tests/ folder).

Adds CodSpeed coverage for the three places `safe_label_str` runs on
the wire-format hot path so any future regression to the printable
filter or length-cap shows up as a measurable delta.

- `test_noise_hello_parse` drives a hello frame through a fresh
  MockAPINoiseFrameHelper, exercising `_handle_hello` and its two
  safe_label_str calls (server name and MAC).
- `test_noise_handshake_reject_parse` drives hello + reject frames so
  `_error_on_incorrect_preamble` runs the explanation sanitize.
- `test_safe_label_str_throughput` is a parametrized direct benchmark
  of `safe_label_str` over realistic name / MAC / explanation inputs
  (clean and control-char-laden), catching algorithm-level
  regressions in the printable-filter / length-cap path.
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 20, 2026

Merging this PR will not alter performance

✅ 40 untouched benchmarks
🆕 7 new benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
🆕 test_safe_label_str_throughput[name_noisy] N/A 3.8 ms N/A
🆕 test_safe_label_str_throughput[explanation] N/A 4.6 ms N/A
🆕 test_safe_label_str_throughput[explanation_noisy] N/A 5.3 ms N/A
🆕 test_safe_label_str_throughput[mac] N/A 3.2 ms N/A
🆕 test_safe_label_str_throughput[name] N/A 3 ms N/A
🆕 test_noise_handshake_reject_parse N/A 44.7 ms N/A
🆕 test_noise_hello_parse N/A 42.7 ms N/A

Comparing benchmark-noise-hello-sanitize (32465d0) with main (f12108e)

Open in CodSpeed

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (f12108e) to head (32465d0).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1687   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines         4159      4159           
=========================================
  Hits          4159      4159           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco marked this pull request as ready for review May 20, 2026 13:23
Copilot AI review requested due to automatic review settings May 20, 2026 13:23
@bdraco bdraco merged commit 533d608 into main May 20, 2026
18 checks passed
@bdraco bdraco deleted the benchmark-noise-hello-sanitize branch May 20, 2026 13:23
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 20, 2026

Warning

Rate limit exceeded

@bdraco has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 2 minutes and 36 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 196694a1-74c5-4ed1-babd-0b63543b62c8

📥 Commits

Reviewing files that changed from the base of the PR and between f12108e and 32465d0.

📒 Files selected for processing (1)
  • tests/benchmarks/test_noise.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch benchmark-noise-hello-sanitize

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds CodSpeed benchmarks to quantify the overhead of safe_label_str on the Noise “hello” and handshake-reject parsing paths, plus a direct throughput benchmark for safe_label_str with realistic inputs.

Changes:

  • Add benchmarks that drive a hello frame through MockAPINoiseFrameHelper to cover the server-name and MAC sanitization calls.
  • Add benchmarks that drive hello + a crafted handshake-reject frame to cover the explanation sanitization path.
  • Add a parametrized throughput benchmark for safe_label_str over clean/noisy representative strings and per-field length caps.
Comments suppressed due to low confidence (1)

tests/benchmarks/test_noise.py:284

  • Same concern as the hello benchmark: each inner iteration constructs a new helper (including PSK decode + NoiseConnection setup) and then triggers an error path that sets ready_future to an exception. Because the future’s exception is never awaited/consumed, repeated iterations can generate “Future exception was never retrieved” noise and affect benchmark stability. Prefer benchmark.pedantic with per-iteration setup/teardown that consumes/cancels ready_future, and keep helper/proto setup outside the timed portion if the intent is to measure the handshake-reject sanitize path specifically.
    @benchmark
    def parse_reject_packets() -> None:
        for _ in range(20):
            connection, _ = _make_mock_connection()
            helper = MockAPINoiseFrameHelper(
                connection=connection,
                noise_psk=noise_psk,
                expected_name=None,
                expected_mac=None,
                client_info="my client",
                log_name="test",
                writer=_drop,
            )
            mock_data_received(helper, hello_pkt)
            mock_data_received(helper, reject_frame)
            helper.close()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +234 to +248
@benchmark
def parse_hello_packets() -> None:
for _ in range(20):
connection, _ = _make_mock_connection()
helper = MockAPINoiseFrameHelper(
connection=connection,
noise_psk=noise_psk,
expected_name=None,
expected_mac=None,
client_info="my client",
log_name="test",
writer=_drop,
)
mock_data_received(helper, hello_pkt)
helper.close()
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.

2 participants