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