@kylincloud/flamegraph 0.35.6 → 0.35.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +179 -60
- package/dist/index.cjs.js +18 -5087
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +18 -5079
- package/dist/index.esm.js.map +1 -0
- package/dist/index.node.cjs.js +13 -0
- package/dist/index.node.cjs.js.map +1 -0
- package/dist/index.node.d.ts +4 -3
- package/dist/index.node.d.ts.map +1 -1
- package/dist/index.node.esm.js +13 -0
- package/dist/index.node.esm.js.map +1 -0
- package/package.json +26 -24
- package/src/index.node.ts +22 -13
- package/src/index.tsx +32 -6
- package/dist/index.cjs.css +0 -792
- package/dist/index.esm.css +0 -792
- /package/dist/{logo-v3-small-T5VXIMRR.svg → logo-v3-small.svg} +0 -0
package/README.md
CHANGED
|
@@ -1,63 +1,113 @@
|
|
|
1
1
|
# @kylincloud/flamegraph
|
|
2
2
|
|
|
3
|
-
一个可复用的 **火焰图 / 差分火焰图 React 组件库**,基于 Pyroscope 的 flamegraph 思路进行了工程化封装,方便在任意前端应用中渲染 CPU / 内存等采样型 Profiling
|
|
3
|
+
一个可复用的 **火焰图 / 差分火焰图 React 组件库**,基于 Pyroscope 的 flamegraph 思路进行了工程化封装,方便在任意前端应用中渲染 CPU / 内存等采样型 Profiling 数据。:contentReference[oaicite:1]{index=1}
|
|
4
4
|
|
|
5
5
|
> ⚠️ 说明
|
|
6
|
-
> 本仓库主要服务于麒麟内部项目(如性能观测 / 智算平台等),API 在 0.x
|
|
6
|
+
> 本仓库主要服务于麒麟内部项目(如性能观测 / 智算平台等),API 在 0.x 阶段可能会有不兼容调整,请以当前版本导出的实际 API 为准。
|
|
7
7
|
|
|
8
8
|
---
|
|
9
9
|
|
|
10
10
|
## 1. 功能特性
|
|
11
11
|
|
|
12
12
|
- 🔥 支持 **单视图** 与 **Diff 差分视图**
|
|
13
|
-
- 📊 支持 **表格 / 火焰图 / 表格+火焰图 / Sandwich**
|
|
13
|
+
- 📊 支持 **表格 / 火焰图 / 表格+火焰图 / Sandwich** 等多种布局(具体取决于业务侧如何组合封装)
|
|
14
14
|
- 🧭 丰富交互:
|
|
15
15
|
- 鼠标悬停 Tooltip(展示占比、采样数、绝对值等)
|
|
16
16
|
- 点击放大 / 回退、滚轮缩放、拖拽平移
|
|
17
|
-
-
|
|
17
|
+
- 右键菜单(折叠、重置视图等)
|
|
18
18
|
- 🔍 函数名搜索 & 高亮
|
|
19
19
|
- 🎨 多种配色 / Diff 渐变色,适配普通与色弱模式
|
|
20
20
|
- 🧱 基于 **Flamebearer** 数据结构,兼容 Pyroscope / Grafana Pyroscope 的 profile 数据
|
|
21
|
-
- 🧩
|
|
21
|
+
- 🧩 以 React 组件的方式独立封装,可作为「黑盒区域」嵌入任意业务页面
|
|
22
22
|
|
|
23
23
|
---
|
|
24
24
|
|
|
25
25
|
## 2. 安装
|
|
26
26
|
|
|
27
|
-
>
|
|
27
|
+
> 发布渠道可按实际情况调整(npm / GitLab Registry / 本地 `link` 等)。
|
|
28
28
|
|
|
29
29
|
```bash
|
|
30
30
|
# npm
|
|
31
31
|
npm install @kylincloud/flamegraph
|
|
32
32
|
|
|
33
|
-
#
|
|
33
|
+
# pnpm
|
|
34
34
|
pnpm add @kylincloud/flamegraph
|
|
35
35
|
|
|
36
|
-
#
|
|
36
|
+
# yarn
|
|
37
37
|
yarn add @kylincloud/flamegraph
|
|
38
38
|
````
|
|
39
39
|
|
|
40
|
+
本库对 React 有 peer 依赖:
|
|
41
|
+
|
|
42
|
+
* `"react": ">=16.14.0"`
|
|
43
|
+
* `"react-dom": ">=16.14.0"`
|
|
44
|
+
|
|
45
|
+
确保你的工程中 React 版本满足要求。
|
|
46
|
+
|
|
40
47
|
---
|
|
41
48
|
|
|
42
49
|
## 3. 快速上手(React 示例)
|
|
43
50
|
|
|
51
|
+
当前包的入口文件导出了一些核心组件 / 工具函数 / i18n 能力,形态大致如下:
|
|
52
|
+
|
|
53
|
+
* 组件与工具:
|
|
54
|
+
|
|
55
|
+
* `Flamegraph`
|
|
56
|
+
* `FlamegraphRenderer`
|
|
57
|
+
* `Box`
|
|
58
|
+
* `DefaultPalette`
|
|
59
|
+
* `convertJaegerTraceToProfile`
|
|
60
|
+
* `diffTwoProfiles`
|
|
61
|
+
* i18n 导出:
|
|
62
|
+
|
|
63
|
+
* `flamegraphDefaultMessages`
|
|
64
|
+
* `flamegraphZhCNMessages`
|
|
65
|
+
* `FlamegraphI18nProvider`
|
|
66
|
+
* 类型 `FlamegraphMessages`
|
|
67
|
+
|
|
44
68
|
### 3.1 引入样式与组件
|
|
45
69
|
|
|
70
|
+
构建后会输出 ESM / CJS 两套 JS 以及对应的 CSS 文件:
|
|
71
|
+
|
|
72
|
+
* `dist/index.esm.js`(ESM,`"module"`)
|
|
73
|
+
* `dist/index.cjs.js`(CJS,`"main"`)
|
|
74
|
+
* `dist/index.esm.css` / `dist/index.cjs.css`(样式)
|
|
75
|
+
|
|
76
|
+
在业务工程中建议按 ESM 方式引入:
|
|
77
|
+
|
|
46
78
|
```tsx
|
|
47
79
|
import React from 'react'
|
|
48
80
|
|
|
49
|
-
// 必须:引入打包好的 CSS
|
|
50
|
-
import '@kylincloud/flamegraph/dist/index.css'
|
|
51
|
-
|
|
52
|
-
//
|
|
53
|
-
import {
|
|
54
|
-
|
|
81
|
+
// 必须:引入打包好的 CSS(推荐使用 ESM 版本)
|
|
82
|
+
import '@kylincloud/flamegraph/dist/index.esm.css'
|
|
83
|
+
|
|
84
|
+
// 典型用法:只用渲染器
|
|
85
|
+
import {
|
|
86
|
+
FlamegraphRenderer,
|
|
87
|
+
Flamegraph,
|
|
88
|
+
Box,
|
|
89
|
+
DefaultPalette,
|
|
90
|
+
convertJaegerTraceToProfile,
|
|
91
|
+
diffTwoProfiles,
|
|
92
|
+
FlamegraphI18nProvider,
|
|
93
|
+
flamegraphDefaultMessages,
|
|
94
|
+
flamegraphZhCNMessages,
|
|
95
|
+
} from '@kylincloud/flamegraph'
|
|
55
96
|
```
|
|
56
97
|
|
|
57
|
-
|
|
98
|
+
> ✅ 提示:
|
|
99
|
+
>
|
|
100
|
+
> * CSS 路径请使用 `dist/index.esm.css` 或 `dist/index.cjs.css`,与当前打包结果一致。
|
|
101
|
+
> * 若你的打包器不支持从 `exports` 读 CSS 子路径,可使用显式路径
|
|
102
|
+
> `@kylincloud/flamegraph/dist/index.esm.css`。
|
|
103
|
+
|
|
104
|
+
### 3.2 最小可用示例:渲染单个 Flamebearer profile
|
|
58
105
|
|
|
59
106
|
```tsx
|
|
60
|
-
|
|
107
|
+
import React from 'react'
|
|
108
|
+
import '@kylincloud/flamegraph/dist/index.esm.css'
|
|
109
|
+
import { FlamegraphRenderer } from '@kylincloud/flamegraph'
|
|
110
|
+
|
|
61
111
|
const simpleProfile = {
|
|
62
112
|
version: 1,
|
|
63
113
|
flamebearer: {
|
|
@@ -67,12 +117,15 @@ const simpleProfile = {
|
|
|
67
117
|
[0, 100, 0, 100], // root
|
|
68
118
|
// ...
|
|
69
119
|
],
|
|
70
|
-
|
|
120
|
+
numTicks: 100,
|
|
121
|
+
maxSelf: 100,
|
|
122
|
+
sampleRate: 100,
|
|
123
|
+
units: 'samples',
|
|
71
124
|
},
|
|
72
125
|
metadata: {
|
|
73
126
|
appName: 'demo',
|
|
74
127
|
spyName: 'cpu',
|
|
75
|
-
format: 'single',
|
|
128
|
+
format: 'single',
|
|
76
129
|
},
|
|
77
130
|
}
|
|
78
131
|
|
|
@@ -81,7 +134,7 @@ export default function App() {
|
|
|
81
134
|
<div style={{ height: 600 }}>
|
|
82
135
|
<FlamegraphRenderer
|
|
83
136
|
profile={simpleProfile}
|
|
84
|
-
//
|
|
137
|
+
// 典型配置示例(具体以实现为准)
|
|
85
138
|
// viewType="both" // 'flamegraph' | 'table' | 'both' | 'sandwich'
|
|
86
139
|
// type="single" // 'single' | 'diff'
|
|
87
140
|
// showToolbar={true}
|
|
@@ -92,10 +145,40 @@ export default function App() {
|
|
|
92
145
|
}
|
|
93
146
|
```
|
|
94
147
|
|
|
148
|
+
### 3.3 使用 i18n 包装
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
import React from 'react'
|
|
152
|
+
import '@kylincloud/flamegraph/dist/index.esm.css'
|
|
153
|
+
import {
|
|
154
|
+
FlamegraphRenderer,
|
|
155
|
+
FlamegraphI18nProvider,
|
|
156
|
+
flamegraphDefaultMessages,
|
|
157
|
+
flamegraphZhCNMessages,
|
|
158
|
+
} from '@kylincloud/flamegraph'
|
|
159
|
+
|
|
160
|
+
export default function App() {
|
|
161
|
+
return (
|
|
162
|
+
<FlamegraphI18nProvider
|
|
163
|
+
messages={{
|
|
164
|
+
// 可按需合并多语言
|
|
165
|
+
en: flamegraphDefaultMessages,
|
|
166
|
+
zhCN: flamegraphZhCNMessages,
|
|
167
|
+
}}
|
|
168
|
+
locale="zhCN"
|
|
169
|
+
>
|
|
170
|
+
<div style={{ height: 600 }}>
|
|
171
|
+
<FlamegraphRenderer profile={/* your profile */} />
|
|
172
|
+
</div>
|
|
173
|
+
</FlamegraphI18nProvider>
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
95
178
|
> ✅ 建议:
|
|
96
179
|
>
|
|
97
180
|
> * 将容器高度固定(如 400–800px),避免页面滚动条与内部滚动冲突
|
|
98
|
-
> *
|
|
181
|
+
> * App 入口处只需引入一次 CSS;组件可在各处复用
|
|
99
182
|
|
|
100
183
|
---
|
|
101
184
|
|
|
@@ -118,8 +201,9 @@ type FlamebearerProfile = {
|
|
|
118
201
|
metadata?: {
|
|
119
202
|
appName?: string
|
|
120
203
|
spyName?: string
|
|
121
|
-
format?: 'single' | 'double' | '
|
|
122
|
-
|
|
204
|
+
format?: 'single' | 'double' | 'diff' | string
|
|
205
|
+
from?: number
|
|
206
|
+
until?: number
|
|
123
207
|
[key: string]: any
|
|
124
208
|
}
|
|
125
209
|
}
|
|
@@ -129,89 +213,122 @@ type FlamebearerProfile = {
|
|
|
129
213
|
* `levels`:二维数组,每一行对应火焰图中的一层;
|
|
130
214
|
|
|
131
215
|
* **单视图**:每 4 个数字为一组
|
|
132
|
-
* **Diff
|
|
216
|
+
* **Diff 视图**:每组数字扩展为左/右 profile 的宽度及差分值(通常 7 个)
|
|
133
217
|
* `units` / `sampleRate`:用于 Tooltip 和表格中做单位换算、展示总时间等
|
|
134
218
|
|
|
135
|
-
|
|
219
|
+
在麒麟内部项目中,也可以在后端将自定义的树型数据转换为上述格式,再传递给组件。
|
|
136
220
|
|
|
137
221
|
---
|
|
138
222
|
|
|
139
223
|
## 5. Diff 差分火焰图
|
|
140
224
|
|
|
141
|
-
如果 profile 为 Diff
|
|
225
|
+
如果 profile 为 Diff 格式(用于比较「基线」与「当前」两段 profile),通常会:
|
|
226
|
+
|
|
227
|
+
* 在 `metadata.format` 中标注 diff 格式(例如 `'diff'`)
|
|
228
|
+
* 在 `flamebearer.levels` 中扩展出:
|
|
229
|
+
|
|
230
|
+
* 左 profile(基线)的宽度 / 值
|
|
231
|
+
* 右 profile(比较)的宽度 / 值
|
|
232
|
+
* 差分值(正负表示变慢 / 变快)
|
|
142
233
|
|
|
143
|
-
|
|
144
|
-
* 在 `flamebearer.levels` 中扩展出左/右两个 profile 的宽度以及差分数值
|
|
145
|
-
* 在前端通过:
|
|
234
|
+
前端典型表现:
|
|
146
235
|
|
|
147
|
-
|
|
148
|
-
* 表格中展示 `Baseline / Comparison / Diff` 三列
|
|
149
|
-
* Tooltip 中展示百分比、绝对值、样本数等
|
|
236
|
+
* 火焰图区域:
|
|
150
237
|
|
|
151
|
-
|
|
238
|
+
* 使用渐变或红/绿配色展示「变慢 / 变快」
|
|
239
|
+
* 支持针对 diff 值的高亮与过滤
|
|
240
|
+
* 表格区域:
|
|
241
|
+
|
|
242
|
+
* 展示 `Symbol / Baseline / Comparison / Diff` 列
|
|
243
|
+
* 百分比 + 绝对值组合展示(例如 `12.3% (12 ms)`)
|
|
244
|
+
* Tooltip:
|
|
245
|
+
|
|
246
|
+
* 展示 `% of total / value / samples` 等信息
|
|
247
|
+
|
|
248
|
+
具体字段与含义请以实际 `DiffProfile` / `DiffNode` 类型定义及实现为准。
|
|
152
249
|
|
|
153
250
|
---
|
|
154
251
|
|
|
155
252
|
## 6. 样式与主题
|
|
156
253
|
|
|
157
|
-
*
|
|
158
|
-
* 支持通过 Sass 变量、CSS 变量覆盖部分样式:
|
|
254
|
+
* 核心样式文件为:`src/sass/flamegraph.scss`,打包后输出为:
|
|
159
255
|
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
|
|
163
|
-
* 如需与业务系统整体主题统一,可以:
|
|
256
|
+
* `dist/index.esm.css`
|
|
257
|
+
* `dist/index.cjs.css`
|
|
258
|
+
* 打包配置中已将 `*.css` / `*.scss` 标记为 `sideEffects`,确保在使用方构建时不会被错误 Tree-Shaking 掉。
|
|
164
259
|
|
|
165
|
-
|
|
166
|
-
|
|
260
|
+
如需与业务系统整体主题统一,可以:
|
|
261
|
+
|
|
262
|
+
* 在外层容器控制背景色、边框、圆角等
|
|
263
|
+
* 通过 CSS 变量或自定义类名覆盖部分颜色 / 字体
|
|
264
|
+
* 如有必要,可在上层再封装一层组件,对内部 props 进行约束与二次包装
|
|
167
265
|
|
|
168
266
|
---
|
|
169
267
|
|
|
170
268
|
## 7. 开发与构建
|
|
171
269
|
|
|
172
|
-
### 7.1
|
|
270
|
+
### 7.1 脚本说明
|
|
271
|
+
|
|
272
|
+
当前 `package.json` 中提供的脚本:
|
|
173
273
|
|
|
174
274
|
```jsonc
|
|
175
275
|
{
|
|
176
276
|
"scripts": {
|
|
177
|
-
"
|
|
178
|
-
"
|
|
179
|
-
"build
|
|
277
|
+
"clean": "rm -rf dist",
|
|
278
|
+
"build": "pnpm run clean && pnpm run build:types && pnpm run build:js",
|
|
279
|
+
"build:types": "tsc -p tsconfig.build.json --emitDeclarationOnly",
|
|
280
|
+
"build:js": "node scripts/build-esbuild.mjs",
|
|
180
281
|
"type-check": "tsc -p tsconfig.build.json --noEmit",
|
|
181
282
|
"lint": "eslint ./ --cache --fix"
|
|
182
283
|
}
|
|
183
284
|
}
|
|
184
285
|
```
|
|
185
286
|
|
|
186
|
-
*
|
|
287
|
+
* **构建类型声明**
|
|
187
288
|
|
|
188
289
|
```bash
|
|
189
|
-
pnpm build
|
|
290
|
+
pnpm run build:types
|
|
190
291
|
```
|
|
191
292
|
|
|
192
|
-
|
|
293
|
+
输出:`dist/**/*.d.ts`(由 `tsconfig.build.json` 控制)。
|
|
193
294
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
295
|
+
* **构建 JS & CSS**
|
|
296
|
+
|
|
297
|
+
```bash
|
|
298
|
+
pnpm run build:js
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
基于 esbuild + sass 插件,将 TSX + Sass 打包成:
|
|
302
|
+
|
|
303
|
+
* `dist/index.esm.js`(ESM 入口,对应 `module`)
|
|
304
|
+
* `dist/index.cjs.js`(CJS 入口,对应 `main`)
|
|
305
|
+
* `dist/index.esm.css`
|
|
306
|
+
* `dist/index.cjs.css`
|
|
307
|
+
|
|
308
|
+
* **完整构建**
|
|
309
|
+
|
|
310
|
+
```bash
|
|
311
|
+
pnpm run build
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
等价于:清理 `dist` → 生成类型声明 → 生成 JS/CSS。
|
|
198
315
|
|
|
199
316
|
* **类型检查**
|
|
200
317
|
|
|
201
318
|
```bash
|
|
202
|
-
pnpm type-check
|
|
319
|
+
pnpm run type-check
|
|
203
320
|
```
|
|
204
321
|
|
|
205
322
|
* **代码规范检查**
|
|
206
323
|
|
|
207
324
|
```bash
|
|
208
|
-
pnpm lint
|
|
325
|
+
pnpm run lint
|
|
209
326
|
```
|
|
210
327
|
|
|
211
328
|
> ✅ 建议:
|
|
212
329
|
>
|
|
213
|
-
> *
|
|
214
|
-
> *
|
|
330
|
+
> * 在 CI 中至少执行 `pnpm run build` 与 `pnpm run type-check`,保证包的构建与类型安全。
|
|
331
|
+
> * 本项目使用 TypeScript 5.x 与 esbuild 进行打包,建议 Node 版本与麒麟内部前端工程保持一致。
|
|
215
332
|
|
|
216
333
|
---
|
|
217
334
|
|
|
@@ -221,10 +338,10 @@ type FlamebearerProfile = {
|
|
|
221
338
|
* 主要差异点集中在:
|
|
222
339
|
|
|
223
340
|
* 包名与发布渠道:使用 `@kylincloud/flamegraph`
|
|
224
|
-
*
|
|
341
|
+
* 构建链路与样式组织适配麒麟内部前端工程(ESM/CJS 双入口、CSS 独立导出)
|
|
225
342
|
* 后续可能增加针对麒麟项目的定制功能(如与内部监控体系联动)
|
|
226
343
|
|
|
227
|
-
|
|
344
|
+
如需回溯或对比实现细节,可以参考源码中的注释与 commit 记录。
|
|
228
345
|
|
|
229
346
|
---
|
|
230
347
|
|
|
@@ -239,13 +356,15 @@ type FlamebearerProfile = {
|
|
|
239
356
|
|
|
240
357
|
建议遵循:
|
|
241
358
|
|
|
242
|
-
1. 所有 PR 保持
|
|
243
|
-
2.
|
|
244
|
-
3. 对关键交互(如 Diff
|
|
359
|
+
1. 所有 PR 保持 `pnpm run type-check` 与 `pnpm run lint` 通过
|
|
360
|
+
2. 尽量避免破坏现有公开 API;如有必要,务必在 `CHANGELOG.md` 与本 README 中标注
|
|
361
|
+
3. 对关键交互(如 Diff 视图、搜索、高亮)补充最小可复现示例或 demo 代码
|
|
245
362
|
|
|
246
363
|
---
|
|
247
364
|
|
|
248
365
|
## 10. 许可证
|
|
249
366
|
|
|
250
367
|
本项目使用 **Apache-2.0** 许可证发布。
|
|
368
|
+
|
|
251
369
|
如在外部开源,请确保保留上游版权与许可证声明,并补充 @kylincloud 的版权信息。
|
|
370
|
+
|