性能监控

作者: tiancai啊呆 | 来源:发表于2018-12-11 15:32 被阅读2次

性能监控是性能优化的第一步,至关重要,因为我们只有进行了性能监控才能知道性能的瓶颈所在,最后对症下药进行性能的优化。

关于Performance.timing

  • 支持情况:目前,所有主要浏览器都已经支持performance对象,包括Chrome 20+、Firefox 15+、IE 10+、Opera 15+。

  • API
    window.performance.timing

    window.performance.timing

从图中我们可以看到该API给了我们很多数据,我们可以使用这些数据对性能进行监控。

// 上报函数
function send(data) {
  // 发起ajax请求
}
function handleData(performance) {
  let performanceData = {}
  if (performance) {
    // 重定向时间
    performanceData.redirectTime = performance.redirectEnd - performance.redirectStart
    // 缓存时间
    performanceData.cacheTime = performance.domainLookupStart - performance.fetchStart
    // dns查询时间
    performanceData.dnsTime = performance.domainLookupEnd - performance.domainLookupStart
    // tcp握手时间
    performanceData.TcpTime = performance.connectEnd - performance.connectStart
    // ajax请求时间
    performanceData.ajaxTime = performance.responseEnd - performance.requestStart
    // 开始解析dom时间,此时document.readyState 变为 loading
    performanceData.domLoadingTime = performance.domLoading - performance.navigationStart
    // dom解析完成时间,此时document.readyState 变为 interactive
    performanceData.domInteractiveTime = performance.domInteractive - performance.navigationStart
    // dom解析完成,资源加载完成,脚本完成
    performanceData.domContentLoadedEventEndTime = performance.domContentLoadedEventEnd - performance.navigationStart
    // 页面从开始到结束的全部时间时间
    performanceData.loadPageTime = performance.loadEventStart - performance.navigationStart
  }
  return performanceData
}

// 初始化函数
function init() {
   window.onload = function () {
     if (window.performance) {
        let timing = window.performance.timing;
        let performanceData = handleData(timing)
        performanceData.timestamp = Date.now()
        performanceData.url = location.href
        send(performanceData)
      }
    }
}
let performanceMonitor = {
    init
}
export default performanceMonitor

关于Performance.getEntries() 获取所有资源请求的时间数据

这个函数返回的将是一个数组,包含了页面中所有的 HTTP 请求

function getAllSourceTime() {
  let allSourceTime = []
  if (window.performance && window.performance.getEntries) {
    window.performance.getEntries().forEach(function (item) {
      let temp = {}
      temp.name = item.name
      temp.entryType = item.entryType
      if (item.entryType === 'paint') {
        temp.startTime = item.startTime
      } else {
        temp.transferSize = item.transferSize
        temp.duration = item.duration
        temp.initiatorType = item.initiatorType
      }
      allSourceTime.push(temp)
    })
  }
  return allSourceTime
}

返回的数组中有两个东西需要我们注意,一个是first-paint(首次绘制),另一个是first-contentful-paint(首次内容绘制)。first-paint表示浏览器绘制了页面的第一个像素的那一刻,first-contentful-paint表示第一 bit 的内容被绘制的那一刻。

paint

但实际上我们有的时候并不需要所有资源的请求时间数据,我们只关心first-paint和first-contentful-paint的时间,因此我们可以把上述两种方法结合一下,让我们的监控数据变得有重点。

// 获取first-paint和first-contentful-paint的时间
function getPaintTime() {
  let obj = {}
  if (window.performance && window.performance.getEntriesByType) {
    let paintArr = window.performance.getEntriesByType('paint')
    if (paintArr && paintArr.length) {
      paintArr.forEach(function (item) {
        obj[item.name] = item.startTime
      })
    }
  }
  return obj
}
// 初始化函数
function init() {
   window.onload = function () {
     if (window.performance) {
        let timing = window.performance.timing;
        let performanceData = handleData(timing)
        performanceData.timestamp = Date.now()
        performanceData.url = location.href
        performanceData = Object.assign({}, performanceData, getPaintTime())
        send(performanceData)
      }
    }
}

关于PerformanceObserver

function performanceObserver() {
  let obj = {}
  var observer = new PerformanceObserver(list => {
    list.getEntries().forEach(entry => {
      if (entry.entryType === 'paint') {
        obj[entry.name] = entry.startTime
      } else {
        let temp = handleData(entry)
        obj = Object.assign({}, obj, temp)
      }
    })
  });
  observer.observe({ entryTypes: ['paint', 'navigation'] });
  return obj
}
performanceObserver()

对比与融合

经过试验我发现,PerformanceObserver是使用一种观察者模式去实时的取到那些数据值,而Performance.timing必须放在window.onload函数里面进行取值。这些差异导致会有一些小问题产生。
问题一:在window.onload函数里面我们进行loadEventEnd的取值会取不到,而在PerformanceObserver则不存在这样的问题;
问题二:使用PerformanceObserver我们发现没有navigationStart,domLoading的值。
问题三:PerformanceObserver更精确。
所以综合它们,最终我们的代码是这样的:

// 上报函数
function send(data) {
  // 发起ajax请求

}

// 处理数据
function handleData(performance) {
  let navigationStart = performance.navigationStart || performance.fetchStart
  let performanceData = {}
  if (performance) {
    // 重定向时间
    performanceData.redirectTime = performance.redirectEnd - performance.redirectStart
    // 缓存时间
    performanceData.cacheTime = performance.domainLookupStart - performance.fetchStart
    // dns查询时间
    performanceData.dnsTime = performance.domainLookupEnd - performance.domainLookupStart
    // tcp握手时间
    performanceData.TcpTime = performance.connectEnd - performance.connectStart
    // ajax请求时间
    performanceData.ajaxTime = performance.responseEnd - performance.requestStart
    // 开始解析dom时间,此时document.readyState 变为 loading
    performanceData.domLoadingTime = performance.domLoading ? performance.domLoading - navigationStart : null
    // dom解析完成时间,此时document.readyState 变为 interactive
    performanceData.domInteractiveTime = performance.domInteractive - navigationStart
    // dom解析完成,资源加载完成,脚本完成
    performanceData.domContentLoadedEventEndTime = performance.domContentLoadedEventEnd - navigationStart
    // 页面从开始到结束的全部时间时间
    performanceData.loadPageTime = performance.loadEventEnd ? performance.loadEventEnd - navigationStart : null
  }
  return performanceData
}

// 获取first-paint及first-contentful-paint的时间
function getPaintTime() {
  let obj = {}
  if (window.performance && window.performance.getEntriesByType) {
    let paintArr = window.performance.getEntriesByType('paint')
    if (paintArr && paintArr.length) {
      paintArr.forEach(function (item) {
        obj[item.name] = item.startTime
      })
    }
  }
  return obj
}

// performanceObserver进行监控
function performanceObserver() {
  let obj = {}
  var observer = new PerformanceObserver(list => {
    list.getEntries().forEach(entry => {
      if (entry.entryType === 'paint') {
        obj[entry.name] = entry.startTime
      } else {
        let temp = handleData(entry)
        obj = Object.assign({}, obj, temp)
      }
    })
    obj.from = 'window.PerformanceObserver'
    obj.url = location.href
    obj.timestamp = Date.now()
    send(obj)
  });
  observer.observe({ entryTypes: ['navigation', 'paint'] });
}

// 初始化函数
function init() {
  if (window.PerformanceObserver) {
    performanceObserver()
  } else if (window.performance) {
    window.onload = function () {
      let timing = window.performance.timing;
      let performanceData = handleData(timing)
      performanceData.timestamp = Date.now()
      performanceData.url = location.href
      performanceData.from = 'window.performance'
      performanceData = Object.assign({}, performanceData, getPaintTime())
      send(performanceData)
    }
  }
}

let performanceMonitor = {
    init
}

export default performanceMonitor

参考资料

相关文章

  • Linux CPU性能分析与监控——vmstat、top

    Linux性能监控工具汇总: iostat 磁盘性能监控 vmstat 虚拟内存性能监控、CPU监控(进程上...

  • docker 安装 zabbix

    zabbix 的作用:具备常见的商业监控软件所具备的功能(主机的性能监控、网络设备性能监控、数据库性能监控、FTP...

  • java实现性能监控方案

    一、性能监控:要监控应用的性能,首先列出性能监控点(比如方法执行耗时,sql执行耗时等等监控),然后输出要关注的信...

  • Crash监控方案(一):Java层监控方案

    一、性能监控:要监控应用的性能,首先列出性能监控点(比如方法执行耗时,sql执行耗时等等监控),然后输出要关注的信...

  • iOS 性能优化资料整理

    iOS 性能监控方案 Wedjat(上篇) iOS 性能监控方案 Wedjat(�下篇) 【Dev Club分享】...

  • JVM性能调优实战(〇) - 简介

    收获 计划安排 基于JDK命令行工具的监控 基于Btrace的监控调试 Tomcat性能监控与调优 Nginx性能...

  • 学习笔记集合01

    一、性能优化 1.0、APM性能监控 CPU占用率、内存/磁盘使用率、卡顿监控定位、Crash防护、线程数量监控、...

  • 监控系统选型Prometheus、TICK、Open-falco

    监控系统选型 Prometheus监控场景: 业务监控、性能监控、容器监控、微服务监控、部分应用监控(能够做的应用...

  • JVM性能调优

    JVM性能调优 JVM性能监控工具介绍

  • OpManager网络性能监控

    网络性能监控 OpManager – 综合、方便、可定制的ITOM网络性能监控软件 OpManager提供综合的网...

网友评论

    本文标题:性能监控

    本文链接:https://www.haomeiwen.com/subject/eykdhqtx.html