perf: tighten fresh_jvm readiness polling for XD CodSpeed sampling#16
Merged
Conversation
XD (full cold start) was producing ~2.7 s per round on CodSpeed CI, which exhausts the per-benchmark wall-time budget after one sample. With only one sample, CodSpeed reports 0% noise and "no measurable impact" for every cold-start PR — including any genuine 10-100 ms improvement that would otherwise be detectable. This made #15 (GatewayConnection background-warm) invisible on the dashboard even though the optimization itself is correct. Replace the legacy readiness model: 0.25 s sleep + 1 retry x 2.0 s = 2.25 s ceiling with tight polling: 0 s sleep + 300 retries x 0.05 s = 15 s ceiling fast hosts now succeed in ~50-200 ms; slow CI in ~1-2 s; ceiling still generous enough for any reasonable runner. With XD per-round down to ~250-500 ms, CodSpeed fits 4-8 samples per benchmark budget and can compute a usable CV. Also drops the unconditional startup_sleep: the rationale ("let the OS reuse the listen port") no longer applies — the JVM-side GatewayServer.startSocket() uses SO_REUSEADDR (already on master), so bind() succeeds immediately even over TIME_WAIT. Backwards compatibility: callers that need the old sleep pattern can pass ``startup_sleep=0.25`` explicitly. Local measurement (M-series + JDK 21): XD before: median ~355 ms, max ~2.4 s (variance dominated by retries) XD after: median ~260 ms, max ~420 ms (proper tight polling) Co-authored-by: Isaac
3 tasks
Tagar
added a commit
that referenced
this pull request
May 28, 2026
py4j#557) (py4j#595) Adds four CodSpeed scenarios mapping 1:1 to py4j cold-start improvement work areas, plus tightens the perf-framework JVM readiness check so slow scenarios get enough samples per benchmark budget. Pure measurement infrastructure — zero behavior change in py4j proper. Touches only the perf testing subtree. New scenarios: * XA-3 — attribute_walk_jvm_java_lang_System 1 000 walks of ``gateway.jvm.java.lang.System``. Each walk re-issues 3 reflection round-trips today because JVMView / JavaPackage / JavaClass __getattr__ don't memoize. Cache canary for the upcoming Python attribute-cache PR. * XB — gateway_reconnect_against_running_jvm 50 fresh ``JavaGateway()`` against the fixture JVM per round. Measures per-connection setup cost — Java accept() loop allocates 14 command class instances per connection (GatewayConnection.java:196-208), Python runs _create_connection + socket setup. * XC — callback_infra_overhead_unused X1-1 workload with ``CallbackServer`` started, but no callbacks actually invoked. Delta vs X1-1 = the steady-state cost of running callback infrastructure alongside the main gateway. * XD — full_cold_start_subprocess_first_call subprocess.Popen -> first call -> shutdown, one cycle per round. Slow per-iteration; CodSpeed adapts iteration count. Target of the JVM-flag work (AppCDS, -XX:TieredStopAtLevel=1, CRaC). XA/XB/XC share the long-running fixture JVM (``test_macro_scenario``). XD owns its full JVM lifecycle inside ``measure()`` and is dispatched via a separate ``test_cold_start_scenario`` test function — it can't share the fixture because both default to port 25333. Readiness model overhaul in ``perf.jvm.fresh_jvm``: before: 0.25 s sleep + 1 retry x 2.0 s = 2.25 s ceiling after: 0 s sleep + 300 retries x 0.05 s = 15 s ceiling Tight polling matters for XD on CodSpeed: under the old model each XD iteration cost ~2.7 s and CodSpeed could fit only one sample per benchmark budget, making variance and regression detection impossible. Under the new model XD per-iteration finishes in ~500-1100 ms — enough for 2-5 samples per budget. The unconditional 0.25 s startup_sleep's original rationale ("let the OS reuse the listen port") no longer applies: GatewayServer.startSocket() already uses SO_REUSEADDR, so bind() succeeds immediately even over TIME_WAIT. Backwards compatibility preserved — callers can pass ``startup_sleep=0.25`` to restore the old wait pattern. Validated on #12, #13, #16 (where these changes originated as 3 iterative PRs; bundled here for a clean single upstream review surface). Full 56-cell CI matrix green on each byteoak iteration. CodSpeed scenarios surface real cold-start work on subsequent PRs — issue py4j#557. Co-authored-by: Isaac
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.
Summary
Follow-up to #13. XD (full cold start) was producing ~2.7 s per round on CodSpeed CI, which exhausts the per-benchmark wall-time budget after one sample. With only one sample, CodSpeed reports 0% noise and "no measurable impact" for every cold-start PR — even when the optimization itself is correct. This made #15 (GatewayConnection background-warm) invisible on the dashboard.
Change
Replace the legacy readiness model in
perf.jvm.fresh_jvm:startup_sleep0.25s0.0sreadiness_retries1300readiness_poll_s2.0)0.05Fast hosts now succeed in ~50-200 ms; slow CI in ~1-2 s; the 15 s ceiling stays generous enough for any reasonable runner.
The unconditional 0.25 s
startup_sleepwas originally there to "let the OS reuse the listen port" — that rationale no longer applies.GatewayServer.startSocket()usesSO_REUSEADDR, sobind()succeeds immediately even over TIME_WAIT.Local measurement (M-series + JDK 21)
On CodSpeed CI we expect XD per-round to drop from 2.7 s → ~500-700 ms, enough for 4-6 samples per benchmark budget.
Backwards compatibility
Callers that need the old behavior can pass
startup_sleep=0.25explicitly. No public API removal.Diff
py4j-python/src/py4j/tests/perf/jvm.py: +28 / -8 (new defaults + docstring rewrite)py4j-python/src/py4j/tests/perf/scenarios/macro.py: +6 / -8 (XD inherits new defaults — drops the explicitreadiness_retries=5)Test plan