perf: lazy imports and session reuse for 93% faster CLI startup#67
perf: lazy imports and session reuse for 93% faster CLI startup#67
Conversation
- Defer auth module imports in main.py (~160ms saved) - Defer interactive mode imports in main.py (~64ms saved) - Add lazy_command() utility in fab_lazy_load.py for deferred command loading - Apply lazy command loading to all parser modules (fs, acls, api, auth, config, jobs, labels, tables, describe) - Defer psutil import in fab_context.py (~9ms saved) - Optimize init_defaults() to skip config write when unchanged - Add session reuse in fab_api_client.py for connection pooling - Move describe_parser command functions to separate module for lazy loading - Update test mock paths for lazy imports Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
…ype hint Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
|
@copilot how can i test your changes and see the perf improvment? |
You can measure the improvement with Python's 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 -20This shows cumulative import times per module — you'll see 3. End-to-end CLI timing: time fab --version
time fab --help4. Run the automated perf tests included in this PR: python -m pytest tests/test_utils/test_lazy_load.py -vThis runs 8 tests including On the CI environment I measured ~737ms → ~54ms for the module import (93% reduction). |
|
@copilot the fab:build type check code failed. fixed it |
Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
- Add Accept-Encoding gzip/deflate header for compressed responses - Increase connection pool from 10 to 20 for better connection reuse - Default GET timeout to 30s (was 240s); writes stay at 240s Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…hub.com/ayeshurun/fabric-cli into copilot/improve-cli-latency-performance
Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
📥 Pull Request
✨ Description of new changes
Deferred heavy imports (
msal,jwt,cryptography,requests,prompt_toolkit,psutil) that were loaded on every CLI invocation — even forfab --version.Changes
lazy_command()utility infab_lazy_load.pydefers command module imports in all 9 parser files until the command is actually invokedmain.py— auth stack (~160ms) and prompt_toolkit (~64ms) only load when neededpsutilinfab_context.py— moved into_get_context_session_id()withTYPE_CHECKINGguard for mypyfab_state_config.py—init_defaults()skips file write when nothing changedfab_api_client.py— sharedrequests.Sessionwith retry adapter for connection poolingfab_describe_commands.py— separated heavy describe logic from parser registrationResults
8 new tests covering lazy loading, startup threshold, session reuse, and config optimization.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.