Skip to content

Commit 0d770ae

Browse files
hardfistchenjiahan
andauthored
docs: add profile guide using samply (#9157)
* docs: add profile guide using samply * Update website/docs/zh/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * Update website/docs/zh/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * Update website/docs/en/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * Update website/docs/zh/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * Update website/docs/zh/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * Update website/docs/zh/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * Update website/docs/zh/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * Update website/docs/en/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * Update website/docs/en/contribute/development/profiling.md Co-authored-by: neverland <[email protected]> * chore: add link guide * Apply suggestions from code review * Update website/docs/en/contribute/development/profiling.md * Update website/docs/zh/contribute/development/profiling.md --------- Co-authored-by: neverland <[email protected]>
1 parent 3c2ab03 commit 0d770ae

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

website/docs/en/contribute/development/profiling.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@ Since different profilers have different strengths. It is good to use more than
66

77
<!-- toc -->
88

9+
## Build release version with debug info
10+
11+
Performance analysis should be conducted on a release version that includes debug information. This approach ensures accurate performance results while providing sufficient debug information for analysis. Use the following command to profiling using local build rspack.
12+
13+
1. Build a release version with debug information:
14+
15+
```sh
16+
just build release-debug
17+
```
18+
19+
2. Change `@rspack/core` and `@rspack/cli` to use `link` protocol to link to local build Rspack:
20+
21+
```diff title="package.json"
22+
dependencies: {
23+
- "@rspack/core": "x.y.z",
24+
- "@rspack/cli": "x.y.z",
25+
# link protocol only works in pnpm
26+
+ "@rspack/core": "link:{your_rspack_repo}/packages/rspack",
27+
+ "@rspack/cli": "link:{your_rspack_repo}/packages/rspack-cli"
28+
}
29+
```
30+
31+
3. Reinstall:
32+
33+
```sh
34+
pnpm install
35+
```
36+
937
## Tracing
1038

1139
[`tracing`](https://crates.io/crates/tracing) is used to instrumenting Rspack.
@@ -46,6 +74,36 @@ RSPACK_PROFILE=TRACE=layer=logger rspack build
4674

4775
will print the options passed to Rspack as well as each individual tracing event.
4876

77+
## CPU profiling
78+
79+
### Samply
80+
81+
[Samply](https://github.com/mstange/samply) supports performance analysis for both Rust and JavaScript simultaneously. Follow these steps to perform a complete performance analysis:
82+
83+
- Run the following command to start performance analysis:
84+
85+
```sh
86+
samply record -- node --perf-prof --perf-basic-prof {your_rspack_folder}/rspack-cli/bin/rspack.js -c {your project}/rspack.config.js
87+
```
88+
89+
- After the command execution, the analysis results will automatically open in the [Firefox Profiler](https://profiler.firefox.com/). The screenshot below is from a [Samply profiler](https://profiler.firefox.com/public/5fkasm1wcddddas3amgys3eg6sbp70n82q6gn1g/calltree/?globalTrackOrder=0&symbolServer=http%3A%2F%2F127.0.0.1%3A3000%2F2fjyrylqc9ifil3s7ppsmbwm6lfd3p9gddnqgx1&thread=2&v=10).
90+
91+
:::warning
92+
Node.js currently only supports `--perf-prof` on Linux platforms. JavaScript profiling in Samply depends on `--perf-prof` support. If you need to use Samply for JavaScript profiling on other platforms, consider using Docker for profiling, or you can compile Node.js yourself for macOS using [node-perf-maps](https://github.com/tmm1/node/tree/v8-perf-maps) for profiling purposes.
93+
:::
94+
95+
#### JavaScript profiling
96+
97+
Rspack’s JavaScript typically runs in the Node.js thread. Select the Node.js thread to view the time distribution on the Node.js side.
98+
99+
![Javascript Profiling](https://assets.rspack.dev/rspack/assets/profiling-javascript.png)
100+
101+
#### Rust profiling
102+
103+
Rspack’s Rust code usually runs in the tokio thread. Select the tokio thread to view the time distribution on the Rust side.
104+
105+
![Rust Profiling](https://assets.rspack.dev/rspack/assets/profiling-rust.png)
106+
49107
### Node.js profiling
50108

51109
If we find that the performance bottleneck is on the JS side (e.g. js loader), then we need to further analyse the js side, and we can use Nodejs Profiling to analyse. for example

website/docs/zh/contribute/development/profiling.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@
88

99
<!-- toc -->
1010

11+
## Build release version with debug info
12+
13+
性能分析应基于包含调试信息的发布版本进行。这种方法既能确保性能结果的准确性,又能提供充足的调试信息用于分析。使用以下命令使用本地的 rspack 进行 profiling
14+
15+
1. 构建带有调试信息的发布版本:
16+
17+
```sh
18+
just build release-debug
19+
```
20+
21+
2. 更改 `@rspack/core``@rspack/cli`,使用 `link` 协议链接到本地​​构建的 Rspack:
22+
23+
```diff title="package.json"
24+
dependencies: {
25+
- "@rspack/core": "x.y.z",
26+
- "@rspack/cli": "x.y.z",
27+
# link protocol only works in pnpm
28+
+ "@rspack/core": "link:{your_rspack_repo}/packages/rspack",
29+
+ "@rspack/cli": "link:{your_rspack_repo}/packages/rspack-cli"
30+
}
31+
```
32+
33+
3. 重新安装依赖:
34+
35+
```sh
36+
pnpm install
37+
```
38+
1139
## Tracing
1240

1341
[`tracing`](https://crates.io/crates/tracing) 被用于度量(instrumenting) Rspack。
@@ -48,6 +76,36 @@ RSPACK_PROFILE=TRACE=layer=logger rspack build
4876

4977
将打印传递给 Rspack 的选项以及每个单独的 tracing 事件.
5078

79+
## CPU profiling
80+
81+
### Samply
82+
83+
[Samply](https://github.com/mstange/samply) 支持同时对 Rust 和 JavaScript 进行性能分析,可通过如下步骤进行完整的性能分析:
84+
85+
- 运行以下命令启动性能分析:
86+
87+
```sh
88+
samply record -- node --perf-prof --perf-basic-prof {your_rspack_folder}/rspack-cli/bin/rspack.js -c {your project}/rspack.config.js
89+
```
90+
91+
- 命令执行完毕后会自动在 [Firefox profiler](https://profiler.firefox.com/) 打开分析结果,如下截图来自 [Samply profiler](https://profiler.firefox.com/public/5fkasm1wcddddas3amgys3eg6sbp70n82q6gn1g/calltree/?globalTrackOrder=0&symbolServer=http%3A%2F%2F127.0.0.1%3A3000%2F2fjyrylqc9ifil3s7ppsmbwm6lfd3p9gddnqgx1&thread=2&v=10)
92+
93+
:::warning
94+
Node.js 目前仅在 Linux 平台支持 `--perf-prof`,而 Samply 里的 JavaScript Profiling 依赖 `--perf-prof`的支持,如果你需要在其他平台使用 Samply 进行 JavaScript Profiling,可以选择使用 docker 里进行 profiling,或者可以基于 [node-perf-maps](https://github.com/tmm1/node/tree/v8-perf-maps) 自行在 macOs 平台编译 Node.js 用于 profiling。
95+
:::
96+
97+
#### JavaScript profiler
98+
99+
Rspack 的 JavaScript 代码通常执行在 Node.js 线程里,选择 Node.js 线程查看 Node.js 侧的耗时分布。
100+
101+
![Javascript Profiling](https://assets.rspack.dev/rspack/assets/profiling-javascript.png)
102+
103+
#### Rust profiler
104+
105+
Rspack 的 Rust 代码通常执行在 tokio 线程里,选择 tokio 线程就可以查看 Rust 侧的耗时分布。
106+
107+
![Rust Profiling](https://assets.rspack.dev/rspack/assets/profiling-rust.png)
108+
51109
### Nodejs profiling
52110

53111
如果我们发现性能瓶颈在 JS 端(比如 js loader),那么我们需要进一步分析 js 端,可以使用 Nodejs Profiling 来分析。例如

website/project-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ ruleoneof
157157
ruleparserdataurlcondition
158158
Rust
159159
rustup
160+
samply
160161
sanyuan
161162
Shenzhen
162163
shulaoda

0 commit comments

Comments
 (0)