Repository navigation
perf: await the framework health check directly when there are no probes - #349
Merged
Merged
Conversation
With no health_checks_probes, the default, asyncio.gather wrapped the one framework check in a Task and cost two event-loop iterations per health request. Fixes #337.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #337.
HealthChecksInstrument.define_health_statusalways ranasyncio.gatherover the framework check and every probe inhealth_checks_probes. With no probes, the default, that wrapped a single coroutine in a Task and cost two event-loop iterations per health request.With no probes, it now returns
await self._define_framework_health_status()directly. With probes configured it still usesgather, so they keep running concurrently;test_healthchecks_probes_run_concurrentlycovers that path. FastStream's_define_framework_health_statusstill pings its brokers through its owngather; I left it alone, since a broker list is the casegatherexists for.Test, written first and failing on main:
test_healthchecks_status_without_probes_skips_gather: withasyncio.gatherwrapped in aMagicMockthat still calls through,define_health_status()returns True and the mock is never called. On main it was called once.Measured with
benchmarks/frameworkson macOS (Apple M2, CPython 3.14), in-process, per health request. Both columns use the same venv with only this file swapped, over 3 interleaved runs of 3 repeats:health, over a plain handler with the same JSON body (health_ping)health, overbare#337 measured +7.1 to +0.4 µs (Litestar) and +22.6 to +9.3 µs (FastAPI) on Linux. The saving is larger on a Mac because each event-loop iteration costs more there (
KqueueSelector.select(0)againstEpollSelector), as the issue noted. The Linux numbers are the ones to expect in production.Checked locally: lint and
tyare clean, all 400 tests pass on the branch rebased onto #348, and coverage is 100%.Not breaking.