Skip to content

Commit 2ff1047

Browse files
author
dengxiongfeng
committed
fix: 🐛 增强性能监控功能,添加xhr和observerFp
1 parent 8f8ffeb commit 2ff1047

4 files changed

Lines changed: 25 additions & 1 deletion

File tree

packages/monitor-sdk/src/plugins/performance/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import fetch from './utils/fetch';
2+
import xhr from './utils/xhr';
23
import observerEntries from './utils/observerEntries';
34
import observerFCP from './utils/observerFcp';
45
import observerLCP from './utils/observerLcp';
56
import observerLoad from './utils/observerLoad';
7+
import observerFp from './utils/observerFp';
68
/**
79
* 性能监控
810
*/
911
export default function performanceUtil() {
1012
fetch();
13+
xhr();
1114
observerEntries();
15+
observerFp();
1216
observerFCP();
1317
observerLCP();
1418
observerLoad();

packages/monitor-sdk/src/plugins/performance/utils/observerFcp.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,39 @@ import { PaintType } from '../../../types';
77
*
88
*/
99
export default function observerFCP() {
10+
// 定义回调函数,当性能观察器触发时执行
1011
const entryHandler = (list: PerformanceObserverEntryList) => {
12+
// 遍历性能观察器条目列表
1113
for (const entry of list.getEntries()) {
14+
// 如果条目名称为 'first-contentful-paint'
1215
if (entry.name === 'first-contentful-paint') {
16+
// 断开观察器
1317
observer.disconnect();
18+
// 将条目转换为JSON对象
1419
const json = entry.toJSON();
20+
// 定义报告数据对象
1521
const reportData: PaintType = {
1622
...json,
23+
// 数据类型
1724
type: TraceTypeEnum.performance,
25+
// 一级类型
1826
subType: TraceSubTypeEnum.fcp,
27+
// 页面URL
1928
pageUrl: window.location.href,
29+
// 时间戳
2030
timestamp: new Date().getTime(),
2131
};
2232
// 发送数据 todo;
33+
// 延迟批量报告数据
2334
lazyReportBatch(reportData);
2435
}
2536
}
2637
};
38+
2739
// 统计和计算fcp的时间
40+
// 创建性能观察器,并传入回调函数
2841
const observer = new PerformanceObserver(entryHandler);
2942
// buffered: true 确保观察到所有paint事件
43+
// 开始观察性能条目,类型为 'paint',并启用缓冲
3044
observer.observe({ type: 'paint', buffered: true });
3145
}

packages/monitor-sdk/src/plugins/performance/utils/observerLcp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function observerLCP() {
2020
pageUrl: window.location.href,
2121
timestamp: new Date().getTime(),
2222
};
23-
// 发送数据 todo;
23+
// 发送数据 ;
2424
lazyReportBatch(reportData);
2525
}
2626
};

packages/monitor-sdk/src/plugins/performance/utils/xhr.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ declare global {
1818
}
1919
}
2020

21+
/**
22+
* 重写 XMLHttpRequest 的 open 和 send 方法,以便在发送请求时记录相关性能数据。
23+
*/
2124
function overwriteOpenAndSend() {
2225
originalProto.open = function newOpen(
2326
method: string,
@@ -39,6 +42,9 @@ function overwriteOpenAndSend() {
3942
this.startTime = Date.now();
4043
});
4144

45+
/**
46+
* 页面加载完成后触发的回调函数
47+
*/
4248
const onLoaded = () => {
4349
this.endTime = Date.now();
4450
this.duration = (this.endTime ?? 0) - (this.startTime ?? 0);

0 commit comments

Comments
 (0)