介绍

[p4]性能监控SDK和错误上报 - 图1

核心性能监控指标

FCP(First Contentful Paint)

首次有内容的绘制。 指标参考数值: 1000-2500

FID(First Input Delay)

首次输入延迟

TTI(Time to Interactive)

可交互时间

TBT(Total Blocking Time)

总阻塞时间

监控哪些东西?

SYN 合成监控

通过一系列的工具、规则运行页面, 提取性能指标。

chrome 的 Performance

image.pngimage.png
FCP

  1. let perfomanceMetrics = {};
  2. const observer = new PerformanceObserver((list) => {
  3. for (const entry of list.getEntries()) {
  4. // `entry` is a PerformanceEntry instance.
  5. // `name` will be either 'first-paint' or 'first-contentful-paint'.
  6. const metricName = entry.name;
  7. const time = Math.round(entry.startTime + entry.duration);
  8. if (metricName === 'first-paint') {
  9. perfomanceMetrics.fp = time;
  10. }
  11. if (metricName === 'first-contentful-paint') {
  12. perfomanceMetrics.fcp = time;
  13. }
  14. }
  15. });
  16. // Start observing the entry types you care about.
  17. observer.observe({entryTypes: ['paint']});

TTI
方式1:

  1. if (PerformanceLongTaskTiming) {
  2. window.__tti = {e: []};
  3. const observer = new PerformanceObserver((list) => {
  4. for (const entry of list.getEntries()) {
  5. // observe the longtask to get the time to interactive (TTI)
  6. if (entry.entryType === 'longtask') {
  7. window.__tti.e.concat(entry);
  8. }
  9. }
  10. });
  11. observer.observe({entryTypes: ['longtask']});
  12. }

方式2:

  1. if (PerformanceLongTaskTiming) {
  2. window.__tti = {e: []};
  3. const observer = new PerformanceObserver((list) => {
  4. for (const entry of list.getEntries()) {
  5. // observe the longtask to get the time to interactive (TTI)
  6. if (entry.entryType === 'longtask') {
  7. window.__tti.e.concat(entry);
  8. }
  9. }
  10. });
  11. observer.observe({entryTypes: ['longtask']});
  12. }

Google 的 Lighthouse

  • Performance
  • PWA
  • Accessibility
  • Best Parctices SEO

    RUM 真实用户监控

    image.png

    使用标准的API

    image.png

    定义合适的指标

    采集正确的数据

    上报关联的维度

    image.png

    性能监控SDK实践

项目名称: TypeScript开发性能监控的SDK+用户错误的源代码

什么是SDK?

Software Development Kit 的缩写,翻译过来——软件开发工具包。(辅助开发某一类软件的相关文档、范例和工具的集合都可以叫做SDK)
例如:

相比API, 有区别。
API全称 Application Programming Interface,应用程序接口

错误源代码

错误记录方式?

  • 编译环境 ts/js
  • 打包工具 rollup/esbuild/swc(rust)
  • 目标 umd, 使用microbundle (封装rollup)

参考:
教程 https://v.youku.com/v_show/id_XNTEwNjU0NTk0MA==.html
蚂蚁金服如何把前端性能监控做到极致?)
如何从 0 到 1 搭建性能检测系统)
有赞移动端性能监控平台 https://mp.weixin.qq.com/s/0Uf0G8P43XUWl7IPVtAu8g

node自带api

  1. // node --expose-gc index.js
  2. global.gc(); // 垃圾回收
  3. console.log(process.memoryUsage().heapTotal);
  4. {
  5. rss: 20746240,
  6. heapTotal: 4468736,
  7. heapUsed: 1871224,
  8. external: 821322,
  9. arrayBuffers: 9898
  10. }

http://nodejs.cn/api/process/process_memoryusage.html
image.png