django-test-profiling
Profile and measure slow Django test suites with Django's runner, pytest-django, shell timing, py-spy, cProfile, pytest durations, and profiler visu…
它会碰到什么
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
Django Test Profiling
Use this skill before changing a Django test suite for speed. Measurement decides which optimization is worth doing; otherwise it is easy to spend time on harmless settings while the real cost sits in startup, database setup, fixture construction, or a small set of slow tests.
Measurement Workflow
- Capture a full wall-clock baseline.
- Prefer shell timing because it includes Python startup, Django import time, test discovery, database setup, and teardown.
- Record the exact command, environment, and repeat count.
- Separate framework phases when possible.
- For Django's runner, use
--timingto see database setup and teardown. - For pytest, compare shell timing with
pytest --durations.
- Find the slowest tests.
- For pytest-django, start with
pytest --durations 20. - For Django's runner, use an existing slow-test plugin or a small custom runner only when the project does not already have one.
- Profile only after you know where the time goes.
- Use
py-spyfirst for a low-overhead whole-run view. - Use
cProfilewhen you need exhaustive Python call data.
- Convert the result into a ranked action list.
- Identify whether the expensive part is startup, database creation, repeated setup, test data, query count, I/O, cache/file/storage access, or a specific test body.
- Hand off to
django-test-performance,django-test-data, ordjango-test-parallelizationwhen the next step fits those skills.
Commands
time python manage.py test
python manage.py test --timing
pytest --durations 20
For profiling command variants and interpretation guidance, read [profiler-commands.md](references/profiler-commands.md).
Decision Rules
- Treat shell timing as the authoritative user-visible duration.
- Use Django
--timingwhen database setup or teardown may dominate. - Optimize the slowest measured tests first, not the tests that look suspicious.
- Put noisy duration reports in CI logs when they distract from normal local runs.
- Use
py-spy --subprocesseswhen the test runner uses child processes. - Sort cProfile by cumulative time first; inspect self-time only after you understand the caller chain.
Common Mistakes
- Trusting a framework's final "ran in X seconds" line as the complete runtime.
- Comparing Django runner timing and pytest timing as if they measure identical phases.
- Profiling everything before finding slow tests.
- Reading unsorted cProfile output and chasing import internals.
- Expecting sampling profiles to catch every short hot path.
- Optimizing a helper without checking how often it is reached by tests.
Verification
Before reporting findings, include:
- The exact baseline command and measured wall-clock time.
- The slowest tests or slowest phases.
- The profiler used, if any, and why it was appropriate.
- The top 3 practical next actions, ordered by expected impact.
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
plugins/LVTD-LLC/skills/skills/django-test-profiling/SKILL.md