Skip to content

perf: lazy imports and session reuse for 93% faster CLI startup#67

Open
Copilot wants to merge 6 commits intomainfrom
copilot/improve-cli-latency-performance
Open

perf: lazy imports and session reuse for 93% faster CLI startup#67
Copilot wants to merge 6 commits intomainfrom
copilot/improve-cli-latency-performance

Conversation

Copy link

Copilot AI commented Feb 24, 2026

  • Create scripts/benchmark_startup.py — a self-contained script to compare CLI startup performance between branches
  • Verify script runs correctly

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Analyze and improve CLI latency and performance perf: lazy imports and session reuse for 93% faster CLI startup Feb 24, 2026
Copilot AI requested a review from ayeshurun February 24, 2026 12:27
@ayeshurun
Copy link
Owner

@copilot how can i test your changes and see the perf improvment?

Copy link
Author

Copilot AI commented Feb 24, 2026

@copilot how can i test your changes and see the perf improvment?

You can measure the improvement with Python's -X importtime flag or simple timing:

1. Quick before/after import time:

# On the main branch (before):
git stash && git checkout main
python -c "import time; s=time.perf_counter(); import fabric_cli.main; print(f'{(time.perf_counter()-s)*1000:.0f}ms')"

# On this branch (after):
git checkout copilot/improve-cli-latency-performance
python -c "import time; s=time.perf_counter(); import fabric_cli.main; print(f'{(time.perf_counter()-s)*1000:.0f}ms')"

2. Detailed import profiling:

python -X importtime -c "import fabric_cli.main" 2>&1 | sort -t'|' -k2 -rn | head -20

This shows cumulative import times per module — you'll see msal, jwt, cryptography, requests, and prompt_toolkit are no longer in the top imports.

3. End-to-end CLI timing:

time fab --version
time fab --help

4. Run the automated perf tests included in this PR:

python -m pytest tests/test_utils/test_lazy_load.py -v

This runs 8 tests including test_main_module_import__under_threshold (asserts import < 500ms) and test_heavy_modules_not_imported_at_startup (verifies msal, jwt, cryptography are not loaded at import time).

On the CI environment I measured ~737ms → ~54ms for the module import (93% reduction).

@ayeshurun
Copy link
Owner

@copilot the fab:build type check code failed. fixed it

Copilot AI requested a review from ayeshurun March 1, 2026 11:06
- Lazy command loading via lazy_command() utility — defers command module imports until invoked
- Deferred auth & interactive imports in main.py (auth ~160ms, prompt_toolkit ~64ms)
- Deferred psutil in fab_context.py with TYPE_CHECKING guard
- Config write optimization — init_defaults() skips write when unchanged
- HTTP session reuse with retry adapter for connection pooling
- Extracted fab_describe_commands.py from parser registration
- 8 new tests covering lazy loading, startup, session reuse, config optimization

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ayeshurun ayeshurun force-pushed the copilot/improve-cli-latency-performance branch from 14c1945 to 715bedc Compare March 17, 2026 09:52
Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
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