File tree Expand file tree Collapse file tree
packages/monitor-sdk/src/plugins/performance Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import fetch from './utils/fetch' ;
2+ import xhr from './utils/xhr' ;
23import observerEntries from './utils/observerEntries' ;
34import observerFCP from './utils/observerFcp' ;
45import observerLCP from './utils/observerLcp' ;
56import observerLoad from './utils/observerLoad' ;
7+ import observerFp from './utils/observerFp' ;
68/**
79 * 性能监控
810 */
911export default function performanceUtil ( ) {
1012 fetch ( ) ;
13+ xhr ( ) ;
1114 observerEntries ( ) ;
15+ observerFp ( ) ;
1216 observerFCP ( ) ;
1317 observerLCP ( ) ;
1418 observerLoad ( ) ;
Original file line number Diff line number Diff line change @@ -7,25 +7,39 @@ import { PaintType } from '../../../types';
77 *
88 */
99export 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}
Original file line number Diff line number Diff 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 } ;
Original file line number Diff line number Diff line change @@ -18,6 +18,9 @@ declare global {
1818 }
1919}
2020
21+ /**
22+ * 重写 XMLHttpRequest 的 open 和 send 方法,以便在发送请求时记录相关性能数据。
23+ */
2124function 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 ) ;
You can’t perform that action at this time.
0 commit comments