10.3 编辑器自身性能剖析
编辑器的卡顿往往不在渲染,而在自己身上:属性面板重建、资产扫描、布局计算。剖析器用 begin/end 成对计时,栈结构支持嵌套;百分位与慢操作清单回答偶发卡顿有多偶发、最慢的是谁。时钟可注入,测试不依赖真实时间。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| Profiler | 本卷 8.1 | 8.1 剖析运行时帧;本节剖析编辑器自身操作 |
| 后台任务 | 本卷 10.2 | 10.2 管任务调度;本节度量任务耗时 |
| 全卷脉络 | 本卷目录 | 本节收口性能章 |
从零实现:编辑器自身性能剖析
计时栈记录名字与开始时间;end 弹出栈顶并计算差值,栈空时报错。百分位先排序,再取向上取整的索引并夹到合法范围,因此 p=100 得到最大值。慢操作按阈值过滤,返回副本。时钟通过构造函数注入,测试可以给定一组固定时间点。
对标执行与测量
Unity ProfilerMarker 可为编辑器代码中的操作记录时间区间。helio 示例统计调用方传入的时间样本。嵌套区间可用来观察包含子操作的耗时,求独占耗时还需扣除子区间。 Unity ProfilerMarker
可运行产物:6 条用例
下面是 src/self-profiling.ts,层级 L1,完整可编译。运行命令 node --test test/10-3-editor-self-profiling.test.ts:
// file: src/self-profiling.ts
// tier: L1
// verify: node --test test/10-3-editor-self-profiling.test.ts
//
// 卷四(第十章):编辑器自身性能剖析。
// 编辑器的卡顿往往不在渲染,而在自己身上:属性面板重建、资产扫描、布局
// 计算。剖析器用 begin/end 成对计时,栈结构支持嵌套;百分位与慢操作清单
// 回答「偶发卡顿有多偶发、最慢的是谁」。时钟可注入,测试不依赖真实时间。
export interface Timing {
name: string;
ms: number;
}
export class SpanProfiler {
#stack: { name: string; start: number }[] = [];
#timings: Timing[] = [];
#clock: () => number;
constructor(clock: () => number = () => Date.now()) {
this.#clock = clock;
}
begin(name: string): void {
this.#stack.push({ name, start: this.#clock() });
}
end(): Timing {
const frame = this.#stack.pop();
if (!frame) throw new Error('end 没有配对的 begin');
const timing = { name: frame.name, ms: this.#clock() - frame.start };
this.#timings.push(timing);
return timing;
}
depth(): number {
return this.#stack.length;
}
/** 最近样本的百分位;空集合返回 0。 */
percentile(p: number): number {
if (this.#timings.length === 0) return 0;
const sorted = this.#timings.map((t) => t.ms).sort((a, b) => a - b);
const index = Math.min(
sorted.length - 1,
Math.max(0, Math.ceil((p / 100) * sorted.length) - 1),
);
return sorted[index];
}
slow(thresholdMs: number): Timing[] {
return this.#timings
.filter((t) => t.ms >= thresholdMs)
.map((t) => ({ ...t }));
}
samples(): Timing[] {
return this.#timings.map((t) => ({ ...t }));
}
}
注入时钟让测试完全确定:给定 0、5、45、50 四个时间点,嵌套两层的耗时就能精确断言。百分位用上取整而不是线性插值,样本少时更接近「第几个样本」,对「偶发卡顿」这种问题更直观。
下面是 test/10-3-editor-self-profiling.test.ts,层级 L1:
// file: test/10-3-editor-self-profiling.test.ts
// tier: L1
// verify: node --test test/10-3-editor-self-profiling.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { SpanProfiler } from '../src/self-profiling.ts';
function fakeClock(values: number[]) {
let i = 0;
return () => values[i++];
}
test('成对 begin/end 记录时长', () => {
const p = new SpanProfiler(fakeClock([100, 130]));
p.begin('layout');
assert.deepEqual(p.end(), { name: 'layout', ms: 30 });
assert.equal(p.depth(), 0);
});
test('嵌套 span 各自记录', () => {
const p = new SpanProfiler(fakeClock([0, 5, 45, 50]));
p.begin('outliner');
p.begin('filter');
assert.deepEqual(p.end(), { name: 'filter', ms: 40 });
assert.deepEqual(p.end(), { name: 'outliner', ms: 50 });
});
test('百分位按最近样本计算', () => {
let t = 0;
const p = new SpanProfiler(() => t);
for (const ms of [30, 10, 40, 20]) {
p.begin('op');
t += ms;
p.end();
}
assert.equal(p.percentile(50), 20);
assert.equal(p.percentile(95), 40);
assert.equal(p.percentile(100), 40);
});
test('慢操作按阈值筛选', () => {
let t = 0;
const p = new SpanProfiler(() => t);
for (const ms of [5, 120, 30]) {
p.begin('op');
t += ms;
p.end();
}
assert.deepEqual(p.slow(30), [{ name: 'op', ms: 120 }, { name: 'op', ms: 30 }]);
});
test('未配对 end 报错', () => {
const p = new SpanProfiler(() => 0);
assert.throws(() => p.end(), /没有配对的 begin/);
});
test('空样本百分位为零', () => {
const p = new SpanProfiler(() => 0);
assert.equal(p.percentile(95), 0);
assert.deepEqual(p.samples(), []);
});
本机实测(Node v26.5.0,node --test test/10-3-editor-self-profiling.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉成对计时与嵌套;第三条钉百分位;第四条钉慢操作;后两条钉未配对 end 与空样本。
小结
- 编辑器卡顿要单独度量,不能只看渲染帧。
- begin/end 用栈支持嵌套,end 必须配对。
- 百分位用上取整索引,样本少时更直观。
- 时钟注入让计时测试完全确定。
阅读导航:上一节:10.2 后台任务与缩略图队列 · 下一节:11.1 变更广播与同步 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。