mirror of https://github.com/msgbyte/tailchat
feat: 增加性能埋点统计
parent
11c5c42e15
commit
312d96fb7a
@ -0,0 +1,38 @@
|
||||
import { measure } from '@/utils/measure-helper';
|
||||
import React, { useMemo } from 'react';
|
||||
|
||||
export const SettingsPerformance: React.FC = React.memo(() => {
|
||||
const { record, timeUsage } = useMemo(
|
||||
() => ({
|
||||
record: measure.getRecord(),
|
||||
timeUsage: measure.getTimeUsage(),
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="mb-4">
|
||||
<div>Record:</div>
|
||||
<div className="rounded bg-black bg-opacity-10 p-2">
|
||||
{Object.entries(record).map(([n, t]) => (
|
||||
<div key={n}>
|
||||
{n}: {t}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div>TimeUsage:</div>
|
||||
<div className="rounded bg-black bg-opacity-10 p-2">
|
||||
{Object.entries(timeUsage).map(([n, t]) => (
|
||||
<div key={n}>
|
||||
{n}: {t}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
SettingsPerformance.displayName = 'SettingsPerformance';
|
@ -1,8 +1,10 @@
|
||||
import { isDevelopment, request, version } from 'tailchat-shared';
|
||||
import { measure } from './utils/measure-helper';
|
||||
|
||||
if (isDevelopment === true) {
|
||||
(window as any).DEBUG = {
|
||||
request,
|
||||
version,
|
||||
measure,
|
||||
};
|
||||
}
|
||||
|
@ -0,0 +1,60 @@
|
||||
import { useLayoutEffect } from 'react';
|
||||
|
||||
const records: Record<string, number> = {};
|
||||
|
||||
/**
|
||||
* 记录测量点
|
||||
* @param name 测量点唯一名
|
||||
*/
|
||||
export function recordMeasure(name: string) {
|
||||
if (!records[name]) {
|
||||
records[name] = performance.now();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录测量点(hook)
|
||||
* @param name 测量点唯一名
|
||||
*/
|
||||
export function useRecordMeasure(name: string) {
|
||||
useLayoutEffect(() => {
|
||||
recordMeasure(name);
|
||||
}, []);
|
||||
}
|
||||
|
||||
export const measure = {
|
||||
getRecord: () => ({ ...records }),
|
||||
getTimeUsage() {
|
||||
let t = performance.timing;
|
||||
|
||||
const usage = {
|
||||
// DNS查询耗时
|
||||
dnsUsage: t.domainLookupEnd - t.domainLookupStart,
|
||||
|
||||
// TCP链接耗时
|
||||
tcpUsage: t.connectEnd - t.connectStart,
|
||||
|
||||
// request请求耗时
|
||||
requestUsage: t.responseEnd - t.responseStart,
|
||||
|
||||
// 解析dom树耗时
|
||||
parseDOMUsage: t.domComplete - t.domInteractive,
|
||||
|
||||
// 白屏时间
|
||||
firstPaintTime: t.responseStart - t.navigationStart,
|
||||
|
||||
// domready时间
|
||||
domReadyTime: t.domContentLoadedEventEnd - t.navigationStart,
|
||||
|
||||
// onload 时间
|
||||
onloadTime: t.loadEventEnd - t.navigationStart,
|
||||
};
|
||||
|
||||
if ((t = performance.memory)) {
|
||||
// js内存使用占比
|
||||
usage['jsHeapRatio'] = t.usedJSHeapSize / t.totalJSHeapSize;
|
||||
}
|
||||
|
||||
return usage;
|
||||
},
|
||||
};
|
Loading…
Reference in New Issue