You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ci: add import profiler check across monorepo (#17657)
## Overview
This PR integrates the import profiler script (introduced in #17467)
into our automated CI pipeline.
Why this matters: Import times significantly impact CLI responsiveness
and cold starts for Serverless products like Cloud Run and Cloud
Functions. Ideally, library imports should stay under 500ms, and
anything taking over 1 second is a target for optimization. This CI
check helps us proactively track metrics like import latency, memory
footprint, and code volume to prevent performance regressions on
critical libraries.
The primary goal of this check is to track and enforce performance
standards for package import times across the repository, especially
following our recent work on lazy loading and cold-start optimizations.
By running this benchmark as a CI check with a defined dynamic
differential failure threshold, we can programmatically prevent latency
regressions in module initialization times before they are merged,
ensuring downstream consumers aren't impacted by unexpectedly slow
startup times.
## Changes Included
* **GitHub Actions Workflow**: Created a new workflow
(`.github/workflows/import-profiler.yml`) that triggers on PRs and merge
groups. The workflow is pinned specifically to Python 3.15.
* **Native CI Integration (No template bloat)**: Rather than polluting
the central gapic-generator template (`noxfile.py.j2`) and forcing
updates across 150+ packages, the CI script (`ci/run_single_test.sh`)
natively handles the profiler. It automatically spins up a lightweight
virtual environment, installs the target package, and runs the profiler.
* **Dynamic Differential Checks**: Enhanced `ci/run_single_test.sh` to
checkout `HEAD^1` (the main branch), generate a baseline CSV profile,
and then diff it against the PR branch. If the Median (P50) import time
of the PR degrades by >100ms compared to the baseline, the CI check will
fail. *(Note: Using Median instead of P99 ensures stability against
intermittent CPU spikes on GitHub Action runners).*
* **Safety Backstop**: The script still enforces an absolute
hard-failure backstop of 5000ms for extreme regressions.
* **Conditional Execution & Graceful Skips**: The pipeline verifies if a
valid `setup.py` exists before attempting to profile, gracefully
skipping directories that are not valid Python packages.
## Developer Interactions
If a developer fails this CI check due to an import latency regression,
they can reproduce and debug it locally by running the profiler script
with the `--cprofile` flag (`python scripts/import_profiler/profiler.py
--package <their-package> --cprofile`). This will generate a cProfile
stack trace breakdown of the import time, allowing them to pinpoint
exactly which new dependency or module initialization is causing the
latency spike.
Related PRs
* Builds upon the import profiler tool added in #17467
* Regression test proving the CI catches regressions: #17690
---------
Co-authored-by: Chalmer Lowe <chalmerlowe@google.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
f"FAILURE: Import time regression of {diff:.2f} ms exceeds both the absolute threshold ({diff_threshold} ms) "
270
+
f"and the relative threshold ({relative_diff_threshold:.2f} ms, 15% of baseline Median)."
271
+
)
272
+
exit_code=1
273
+
else:
274
+
ifdiff>diff_threshold:
275
+
final_messages.append(f"SUCCESS: Import time regression of {diff:.2f} ms exceeds absolute threshold ({diff_threshold} ms) but is within relative threshold ({relative_diff_threshold:.2f} ms, 15%).")
276
+
else:
277
+
final_messages.append("SUCCESS: Import time diff is within acceptable thresholds.")
278
+
else:
279
+
final_messages.append(f"WARNING: Baseline CSV {diff_baseline} not found. Skipping diff check.")
final_messages.append(f"WARNING: Median import time ({p50_time:.2f} ms) exceeds the absolute failure threshold ({fail_threshold} ms), but the baseline ({baseline_p50:.2f} ms) also exceeded it. Bypassing absolute backstop failure.")
285
+
else:
286
+
final_messages.append(f"FAILURE: Median import time ({p50_time:.2f} ms) exceeds the failure threshold ({fail_threshold} ms).")
287
+
exit_code=1
288
+
else:
289
+
final_messages.append(f"SUCCESS: Median import time ({p50_time:.2f} ms) is within the failure threshold ({fail_threshold} ms).")
290
+
291
+
iffinal_messages:
292
+
print("\n"+"\n".join(final_messages))
293
+
294
+
ifexit_code==0:
295
+
print("\nSession import_profiler was successful.")
296
+
sys.exit(0)
297
+
else:
298
+
print("\nSession import_profiler failed.")
299
+
sys.exit(1)
300
+
301
+
232
302
defrun_trace(target_module):
233
303
"""Generates importtime trace log and writes it to a file."""
0 commit comments