当前位置:网站首页>performance介绍
performance介绍
2022-07-23 03:39:00 【dralexsanderl】
performance介绍
performance是前端性能监控的API。可以获取页面中的性能相关的信息。通过window.performance可以获取performance对象。
在控制台上输入window.performance可以获取当前页面性能的一些信息。
performance属性
eventCounts: 事件数量(目前为实验阶段,MDN上仍无具体解释)。memory: 基本内存使用情况(chrome扩展的非标准属性)jsHeapSizeLimit: 内存大小限制。
totalJSHeapSize: 可使用的内存。
usedJSHeapSize: JS对象(包括V8引擎内部对象)占用的内存,不能大于totalJSHeapSize,如果大于,有可能出现了内存泄漏。
navigation: 当前页面操作相关信息(页面的加载方式及重定向次数)onresourcetimingbufferfull: 一个回调的EventTarget,当浏览器的资源时间性能缓冲区已满时会触发timeOrigin:性能测试开始时间戳(实验阶段)timing:PerformanceTiming对象,包含延迟相关的性能信息(已从web标准中删除,但一些浏览器仍支持,尽量不要使用,使用PerformanceNavigationTiming替代)
先来重点看看timing、navigation、timeOrigin、onresourcetimingbufferfull这四个。
timing
从输入url到用户可以使用页面的全过程时间统计,会返回一个PerformanceTiming对象,单位均为毫秒。
redirectStart第一次重定向开始时时间戳,若无重定向或上次重定向不同源则为0redirectEnd最后一次重定向完成,即HTTP响应的最后一个字节返回时的时间戳,若无重定向或上次重定向不同源则为0fetchStart浏览器准备通过HTTP请求去获取页面的时间戳domainLookupStart域名查询开始时间戳。若为持久连接,或从本地缓存获取则等同于fetchStartdomainLookupEnd域名查询结束时的时间戳。若为持久连接,或从本地缓存获取则等同于fetchStartconnectStart浏览器与服务器开始建立连接的时间戳,若为持久连接,则等同于fetchStartsecureConnectionStart浏览器与服务器开始安全链接握手的时间戳。若不要求安全连接则为0connectEnd浏览器与服务器连接建立完成的时间戳,即所有握手和认证过程全部完成requestStart浏览器向服务器发出HTTP请求(或开始读取本地缓存时)的时间戳responseStart浏览器从服务器收到(或从本地缓存读取)第一个字节时的时间戳responseEnd浏览器从服务器收到(或从本地缓存读取)最后一个字节时(若在此之前HTTP连接已经关闭,则返回关闭时)的时间戳domLoading当前网页DOM开始加载的时间戳,即document.readyState属性变为loading,且触发对应readyStateChange事件时。domInteractive当前网页DOM解析完成、开始加载子资源(如css、图片等)的时间戳,即document.readyState属性变为interactive,且触发对应readyStateChange事件时。domComplete当前网页DOM解析完成且内嵌资源加载完成的时间戳,即document.readyState属性变为complete,且触发对应readyStateChange事件时。domContentLoadedEventStart当前网页DOM加载完成、所有脚本开始运行的时间戳,即document的DOMContentLoaded事件被触发时。domContentLoadedEventEnd当前网页DOM加载完成、所有脚本运行完成的时间戳。loadEventStart当前网页加载完成,window的load事件回调函数开始执行的时间戳,若该事件还没发生则为0。loadEventEnd当前网页加载完成,window的load事件回调函数开始执行的时间戳,若该事件还没发生则为0。navigationStart当前浏览器窗口的前一个网页关闭,发生unload事件时的时间戳。如果没有前一个网页,则等于fetchStartunloadEventStart如果前一个网页与当前网页同域,则为前一个网页的unload事件发生时的时间戳。如果没有前一个网页或之前网页跳转不同域,则为0。unloadEventEnd如果前一个网页与当前网页同域,则为前一个网页的unload事件回调执行结束的时间戳。如果没有前一个网页或之前网页跳转不同域,则为0。
timing对象各个关键时间点的含义如下所示:

根据timing对象提供的时间点,可以进行以下计算:
DNS查询耗时:domainLookupEnd-domainLookupStartTCP链接耗时:connectEnd-connectStartrequest请求耗时:responseEnd-responseStart- 解析
dom树耗时:domComplete-domInteractive - 白屏时间:
responseStart-navigationStart domready时间(用户可操作时间节点):domContentLoadedEventEnd-navigationStartonload时间(总下载时间):loadEventEnd-navigationStart
封装以上几个方法:
function getPerformanceTiming() {
const performance = window.performance;
if(!performance) {
console.log('该浏览器暂不支持performance');
return;
}
const timing = performance.timing;
return {
dnsTime: timing.domainLookupEnd - timing.domainLookupStart,
tcpTime: timing.connectEnd - timing.connectStart,
requestTime: timing.responseEnd - timing.responseStart,
domParse: timing.domComplete - timing.domInteractive,
blankTime: timing.responseStart - timing.navigationStart,
domReady: timing.domContentLoadedEventEnd - timing.navigationStart,
loadTime: timing.loadEventEnd - timing.navigationStart
}
}
navigation
该属性是一个对象,有两个属性值
redirectCount: 重定向次数(但是这个接口有同源策略限制,即仅能检测同源的重定向)type: 操作类型TYPE_NAVIGATE(0): 当前页面是通过点击链接,书签和表单提交,或者脚本操作,或者在url中直接输入地址
TYPE_RELOAD(1): 点击刷新页面按钮或者通过Location.reload()方法显示的页面
TYPE_BACK_FORWARD(2): 页面通过历史记录和前进后退访问时
TYPE_UNDEFINED(255): 任何未由上述值定义的导航类型
timeOrigin
返回性能测量开始时的时间的高精度时间戳。
resourcetimingbufferfull
performance方法
getEntries
获取所有资源请求的时间数据,这个函数返回一个按startTime排序的对象数组,数组成员除了会自动根据所请求资源的变化而改变以外,还可以用mark(),measure()方法自定义添加,该对象的属性中除了包含资源加载时间还有以下五个属性。
name: 资源名称,是资源的绝对路径或调用mark、measure方法自定义的名称startTime: 开始时间duration:加载时间entryType: 资源类型,entryType类型不同数组中的对象结构也不同initiatorType: 谁发起的请求
entryType类型:
| 值 | 对象类型 | 描述 |
|---|---|---|
| mark | PerformanceMark | 通过mark方法添加到数组中的对象 |
| measure | PerformanceMeasure | 通过measure方法添加 |
| paint | PerformancePaintTiming | 值为first-paint’首次绘制、'first-contentful-paint’首次内容绘制。 |
| resource | PerformanceResourceTiming | 所有资源加载时间,用处最多 |
| navigation | PerformanceNavigationTiming | 导航相关信息 |
| element | PerformanceElementTiming | 元素的加载时间 |
| longtask | PerformanceLongTaskTiming | 长任务实例 |
//根据entryType类型返回的不同对象
PerformanceMark:{
//通过mark()方法添加的对象
entryType:"mark"
name:调用mark()方法时自定义的名字
startTime: 做标记的时间
duration:0
}
PerformanceMeasure:{
//通过measure()方法添加的对象
entryType:"measure"
name:调用measure()方法时自定义的名字
startTime: 开始量的时间
duration:标记的两个量的时间间隔
}
PerformancePaintTiming:{
// Chrome 60 新增类型
entryType:"paint"
name:first-paint" 或 "first-contentful-paint"
startTime: 绘制的时间戳
duration: 0
}
PerformanceResourceTiming:{
//可以用来做一个精准的进度条
entryType:"resource"
name:资源的绝对路径,即URL
startTime: 即将抓取资源的时间,
duration: responseEnd - startTime
initiatorType:略!/:傲娇脸
//其他属性请参考performance.timing
}
PerformanceNavigationTiming:{
entryType:"navigation"
name:本页路由,即地址栏看到的地址
startTime: 0
duration: loadEventEnd - startTime
initiatorType:"navigation"
//其他属性请参考performance.timing
}
initiatorType类型:
| 发起对象 | 值 | 描述 |
|---|---|---|
| a Element | link/script/img/iframe等 | 通过标签形式加载的资源,值是该节点名的小写形式 |
| a CSS resourc | css | 通过css样式加载的资源,比如background的url方式加载资源 |
| a XMLHttpRequest object | xmlhttprequest/fetch | 通过xhr加载的资源 |
| a PerformanceNavigationTiming object | navigation | 当对象是PerformanceNavigationTiming时返回 |
getEntriesByName(name,type[optional]),getEntriesByType(type)
name: 想要筛选出的资源名type:entryType的值中一个
返回值仍是一个数组,这个数组相当于getEntries()方法经过所填参数筛选后的一个子集
clearResourceTimings()
该方法无参数无返回值,可以清楚目前所有entryType为"resource"的数据,用于写单页应用的统计脚本非常有用。
setResourceTimingBufferSize
setResourceTimingBufferSize(maxSize):设置浏览器性能测量缓冲区中 可维持的resource 类型 entry 对象的最大数量。
标记相关
mark: 使用name名称创建一个当前时间戳标记。mesure: 使用name名称创建一个从startMark开始到endMark结束的测量clearMarks: 清除标记clearMeasures: 清除测量
mark、clearMarks
语法:
performance.mark(name);
// 创建两个mark
performance.mark('start');
performance.mark('end');
perform.clearMarks();
clearMarks语法有两种
performance.clearMarks();
performance.clearMarks(name);
当没有传递name参数时,删除所有的mark,传递name则删除指定的mark。
measure、clearMeasures
measure语法
performance.measure(name, startMark, endMark);
performance.mark('start');
setTimeout(() => {
performance.mark('end');
performance.measure('time', 'start', 'end')
}, 2000)
clearMeasures语法也有两种
performance.clearMeasures();
performance.clearMeasures(name);
当没有传递name参数时,删除所有的measure,传递name则删除指定的measure。
now
performance.now()是当前时间与performance.timing.navigationStart的时间差,以微秒(百万分之一秒)为单位的时间,与 Date.now()-performance.timing.navigationStart的区别是不受系统程序执行阻塞的影响,因此更加精准。
toJSON
一个 JSON 格式转化器,返回 Performance 对象的 JSON 对象。
边栏推荐
- Langue C - quelques exercices classiques de langue C
- Three goals and eight tasks of intelligent construction pilot city notice
- 【VSCODE】当前工作目录非当前文件夹/pathlib打印cwd路径错误
- How switch statements work
- 实现城市治理一网统管,必须这 4 个关键技术
- redis token记录用户登录设计求解?
- 【汇总篇】
- Leetcode-99. restore binary search tree
- Arcgis 计算两个栅格图层相关性
- 金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(五))
猜你喜欢

隐适美invisAlign口扫转诊方式(导出口扫数据+线上问诊)

数据中台、BI业务访谈(三):如何选择合适的访谈对象

新的项目实现的技术点如有需要可以指导

leetcode-99.恢复二叉搜索树

宇视NVR设备接入EasyCVR平台,离线后无法上线该如何解决?

What is the experience of writing concurrent tool classes (semaphore, cyclicbarrier, countdownlatch) by yourself in line 30?

How to build and use redis fragment cluster

MySQL three table query problem

转行软件测试薪资10K | 手中有粮心中有底,在任何时候都是真理

Airtest脚本的点击位置与点击偏移
随机推荐
What are you doing at two in the morning?
Click position and click offset of airtest script
Scala object
Illustration and text demonstrate the movable range of the applet movable view
C语言柔性数组
These four key technologies are necessary to realize the unified management of urban governance through one network
error MSB4181: “QtRunWork”任务返回了 false,但未记录错误
目标检测xml文件实现mixup数据增强(修改文件路径直接能用,非常方便)
多线程中的「lost wake up 问题」| 为什么wait()和notify()需要搭配synchonized关键字使用?
图文并茂演示小程序movable-view的可移动范围
How to do the system security test? Let's talk about it in detail
c# 字节数组和类相互转换
Compose原理解析系列之一Compose的设计原理
What is per title encoding?
客户至上 | 国产BI领跑者,思迈特软件完成C轮融资
【汇总篇】
金仓数据库 KingbaseES SQL 语言参考手册 (4. 伪列)
QT error: error c2039 "value": not a member of "`global namespace"
C language -- several classic exercises of C language
The gospel of small and medium-sized enterprises is coming! Jnpf is becoming popular, helping business digital upgrading