дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
Profile on a Python run configuration uses yappi when the run's interpreter can import it: Automatic picks yappi, else py-spy where it can sample without root, else cProfile, and Settings › Python › Profiler gains a yappi choice (missing yappi offers `python -m pip install yappi`, or only the homepage under uv/poetry). yappi runs through python_profile.py with the wall clock and builtins, so a coroutine's time includes its awaits and every thread is profiled; the launcher writes the merged profile.pstats plus one pstats per thread and threads.json (busiest 64), converted to cProfile's call-count order and built-in names. Non-daemon threads and pools are waited for before saving, except after Stop. The Profiler view gets a thread selector (All threads merged, then each thread) when a run has two or more threads. The Rust CI job installs yappi 1.7.6 in a venv on GitHub-hosted runners for the real-interpreter tests. Co-authored-by: wildcatDownstairs <[email protected]>
54 рядки
5.2 KiB
Plaintext
54 рядки
5.2 KiB
Plaintext
---
|
|
title: "性能分析"
|
|
description: "用采样分析器对运行配置做性能分析,并以火焰图、调用树或方法列表查看结果。"
|
|
---
|
|
|
|
**性能分析** 在外部采样分析器下执行[运行配置](/zh/guide/run)。程序退出(或被你停止)后,IDE 读取采样数据,并在性能分析器工具窗口中打开。
|
|
|
|
## 对运行做性能分析
|
|
|
|
- 使用 **运行 › 性能分析** 分析当前选中的配置。
|
|
- 在边栏运行图标的菜单或运行小部件的 **更多操作** 菜单中选择性能分析那一项。
|
|
- **对上下文配置进行性能分析**(在 **查找操作…** 中,`⌘⇧A` / `Ctrl+Shift+A`)分析光标处的程序或测试。
|
|
|
|
程序照常在运行工具窗口里执行。如果缺少分析器,通知会说明缺的是什么,并提供 **安装**(有安装命令时)或 **下载…**(打开分析器的下载页面)。
|
|
|
|
## 支持的运行时
|
|
|
|
| 运行时 | 配置 | 分析器 | 平台 |
|
|
| -------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------ | ------------ |
|
|
| Node.js | `node`、`npx`、`tsx`、`ts-node` 程序,以及 npm、pnpm、yarn 脚本 | `node --cpu-prof`(通过 `NODE_OPTIONS` 设置,本次运行的所有 Node.js 进程都会被分析) | 全部 |
|
|
| Python | 脚本、模块、pytest | yappi,否则 `py-spy record`,再否则 cProfile(见下) | 全部 |
|
|
| JVM | 直接启动 `java` 的配置,例如 Application 和 JAR Application | async-profiler agent(通过 `JAVA_TOOL_OPTIONS` 设置) | macOS、Linux |
|
|
| Rust | `cargo run` | `perf record -g` | Linux |
|
|
| 原生程序 | native 配置 | `perf record -g` | Linux |
|
|
|
|
分析器的准备:
|
|
|
|
- **yappi**:运行所用的解释器能导入它时,Profile 使用它(设置 › Python › 性能分析器 › 自动)。它按墙钟时间分析所有线程,协程的时间包含其 await;快照带线程选择器:**所有线程合并**或单个线程(最多列出最忙的 64 个)。墙钟时间下,只在等待的线程也算忙,线程之间会重叠,所以合并后的时间可能超过运行时长。缺少 yappi 时选择它,会提供 `python -m pip install yappi`。
|
|
- **py-spy**:IDE 先在项目解释器所在目录查找,再到搜索路径中查找。项目没有使用包管理器时,会提供 `python -m pip install py-spy`。
|
|
- **async-profiler**:从 [async-profiler releases](https://github.com/async-profiler/async-profiler/releases) 下载并解压,把其中的 `bin/asprof` 加到 `PATH`,或者把 `ASYNC_PROFILER_HOME` 设为解压目录。IDE 无法替你安装。
|
|
- **perf**:安装发行版的 `linux-tools` / `linux-perf` 软件包。
|
|
|
|
暂不支持:Bun 和 Deno 程序、Windows 上的 JVM 程序、macOS 和 Windows 上的原生程序和 `cargo run`,以及 Maven、Gradle 配置。
|
|
|
|
## 分析 Python 内存分配
|
|
|
|
在 macOS 和 Linux 上,**对 '…' 分析内存分配** 会在 [memray](https://github.com/bloomberg/memray) 跟踪内存分配的情况下运行 Python 脚本、模块或 pytest 配置。项目解释器中需要装有 memray;缺少时通知会给出安装命令。memray 不支持 Windows。
|
|
|
|
快照按分配调用栈显示 **内存占用最高的那一刻** 仍占用的分配,大小以字节计。在那之前已经释放的内存不会列出。每一次 Python 对象分配都会被跟踪,因此程序会比平时运行得慢。只跟踪被分析的进程,不跟踪它的子进程。
|
|
|
|
## 查看结果
|
|
|
|
每次分析得到一个快照,最近五个快照以标签页形式保留在性能分析器工具窗口的标题栏上。每个快照有三个视图:
|
|
|
|
| 视图 | 内容 |
|
|
| ------------ | -------------------------------------------------------------------------------------------------------------------- |
|
|
| **火焰图** | 从根节点向下堆叠的调用栈,越宽表示耗时越多。点击某一帧放大查看,点 **重置缩放** 返回。悬停可以看到总耗时和自身耗时。 |
|
|
| **调用树** | 同样的调用栈,以可展开的树展示,耗时最多的调用排在前面。 |
|
|
| **方法列表** | 每个函数一行,显示自身和总耗时。点击 **自身**、**总计** 或 **方法** 列标题排序。 |
|
|
|
|
双击某一帧或某一行,可以打开对应的源代码位置。在标题栏的 **搜索帧** 中输入文字,会在火焰图中高亮匹配的帧,并过滤方法列表。
|
|
|
|
快照只在当前会话中保留,切换项目或关闭窗口后就会清空。
|