初探performance.timing API

 

浏览器新提供的performance接口精确的告诉我们当访问一个网站页面时当前网页每个处理阶段的精确时间(timestamp),以方便我们进行前端分析。

它是浏览器的直接实现,比之前在网页中用js设置Date.time或者cookie来分析网页时间上要精确很多。

以下是w3c提供的performance.timing各阶段api图

暂时的缺点:

Navigation Timing stops at the window.onload event

现代的网站很多是在onload之后再发触发更多的异步请求,而navigation Timing统计却只在window.onload之后就不统计了 。

为什么不在所有的网络请求完成后统计timing呢?

因为要考虑到有些网页有轮询或者长链接的情况。所以情况就复杂了,w3c还在草案阶段。如果你够牛想出好的解决方案,也可以直接发邮件到w3c去,贡献你的一份力量。

为方便查看统计值,自己写了一个简单的统计表插件

performanceTracer

performance API 耗时统计

统计点:

readyStart = timing.fetchStart - timing.navigationStart;
redirectTime = timing.redirectEnd - timing.redirectStart;
appcacheTime = timing.domainLookupStart - timing.fetchStart;
unloadEventTime = timing.unloadEventEnd - timing.unloadEventStart;
lookupDomainTime = timing.domainLookupEnd - timing.domainLookupStart;
connectTime = timing.connectEnd - timing.connectStart;
requestTime = timing.responseEnd - timing.requestStart;
initDomTreeTime = timing.domInteractive - timing.responseEnd;
domReadyTime = timing.domComplete - timing.domInteractive; //过早获取时 domComplete有时会是0
loadEventTime = timing.loadEventEnd - timing.loadEventStart;
loadTime = timing.loadEventEnd - timing.navigationStart;//过早获取时 loadEventEnd有时会是0

结果:

console.log('准备新页面时间耗时: ' + readyStart);
console.log('redirect 重定向耗时: ' + redirectTime);
console.log('Appcache 耗时: ' + appcacheTime);
console.log('unload 前文档耗时: ' + unloadEventTime);
console.log('DNS 查询耗时: ' + lookupDomainTime);
console.log('TCP连接耗时: ' + connectTime);
console.log('request请求耗时: ' + requestTime);
console.log('请求完毕至DOM加载: ' + initDomTreeTime);
console.log('解释dom树耗时: ' + domReadyTime);
console.log('load事件耗时: ' + loadEventTime);
console.log('从开始至load总耗时: ' + loadTime);

使用方法:

可以直接在html底部引入performance-min.js

或下载chrome 插件.crx包,

注意事项

由于window.performance.timing还处于w3c完善过程中,当你的网站有异步请求时,请在所有异步请求完成后再点击chrome上的插件按钮,以确保数据正确

效果图:

=======================================================================

js及chrome插件下载地址

github: https://github.com/willian12345/performanceTracer

关于performance timing 未完善功能老外的讨论:http://www.stevesouders.com/blog/2012/10/30/qa-nav-timing-and-post-onload-requests/

==========================

转载处请注明:博客园偷饭猫willian12345@126.com

 
 

初探performance.timing API的更多相关文章

  1. 浏览器性能接口performance.timing说明

    原文来自于 https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html 下图描述了该接口的各个时间 ...

  2. 初探 performance – 监控网页与程序性能

    使用 window.performance 提供了一组精确的数据,经过简单的计算就能得出一些网页性能数据. 配合上报一些客户端浏览器的设备类型等数据,就可以实现简单的统计啦! 额,先看下兼容性如何:h ...

  3. 【转载】利用window.performance.timing进行性能分析

    利用window.performance.timing进行性能分析   性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备 ...

  4. 利用window.performance.timing进行性能分析

    性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备加载页面的起始时间 // .unloadEventStart 如果前一个 ...

  5. performance.timing检测页面加载速度

    with(performance){ readyStart = timing.fetchStart - timing.navigationStart; redirectTime = timing.re ...

  6. 初探.Net Core API 网关Ocelot(一)

    一.介绍 Ocelot 是基于.NetCore实现的开源的API网关,支持IdentityServer认证.Ocelot具有路由.请求聚合.服务发现.认证.鉴权.限流熔断等功能,并内置了负载均衡器与S ...

  7. 使用WebView监控网页加载状况,PerformanceMonitor,WebViewClient生命周期

    原理:WebView加载Url完成后,注入js脚本,脚本代码使用W3C的PerformanceTimingAPI, 往js脚本传入一个Android对象(代码中为AndroidObject),在js脚 ...

  8. 优化关键渲染路径CRP

    什么是关键渲染路径? 从收到 HTML.CSS 和 JavaScript 字节到对其进行必需的处理,从而将它们转变成渲染的像素这一过程中有一些中间步骤 浏览器渲染页面前需要先构建 DOM 和 CSSO ...

  9. Web 动画帧率(FPS)计算

    我们知道,动画其实是由一帧一帧的图像构成的.有 Web 动画那么就会存在该动画在播放运行时的帧率.而帧率在不同设备不同情况下又是不一样的. 有的时候,一些复杂或者重要动画,我们需要实时监控它们的帧率, ...

随机推荐

  1. 1023 Train Problem II(卡特兰数)

    Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...

  2. Object.extend

    原文:[转载]Object.extend Object.extend = function (destination, source) { for ( var property in source) ...

  3. hightmaps 按地图上显示的统计数据

    离extjs 至 easyui 到html5到hightchars 再到hightmaps.Exjts和easyui很相似,extjs是重量级的,easyui轻量级的.比extjs容易上手.照着dem ...

  4. 【百度地图API】如何获取行政区域的边界?

    原文:[百度地图API]如何获取行政区域的边界? 摘要:以前教过大家如何自行获取行政区域,或者自定义获取一个区域的边界值.今天来教大家直接调用百度地图API1.3(目前最新版本)来获取行政区域的边界值 ...

  5. 微信公众平台消息接口PHP版开发教程

    原文:微信公众平台消息接口PHP版开发教程  一.写好接口程序 在你的服务器上上传好一个接口程序文件,如http://www.yourdomain.com/weixin.php  内容如下: &l ...

  6. Appium基于安卓的各种FindElement的控件定位方法实践和建议

    AppiumDriver的各种findElement方法的尝试,尝试的目标应用是SDK自带的Notepad应用. 1. findElementByName 1.1 示例 el = driver.fin ...

  7. POJ 1947 树DP获得冠军

    特定N点,N-1的关系.建立了一棵树,问至少减去几个边缘节点可以被作为得到P树.树典型DP称号 dp[cur][j] :记录cur节点,为了得到一个j除去该子树的节点的边的最小数目 对当前树的每个子树 ...

  8. YII编码规范

    类名称: 驼峰式 首字母大字 class PointController class PointRatioController 公共成员方法: 驼峰式 首字母小写 public function ge ...

  9. Aspose.Words 直接写response导出docx文档显示文件已损坏需要修复的解决办法

    使用Aspose.Words的Document.Save(HttpResponse response, string fileName, ContentDisposition contentDispo ...

  10. GIMP也疯狂之动态图的制作(三)

    Note:本篇文章内容为linux下利用gimp和其它工具组合完成截取制作GIF图的一种实现,制作更为灵活,但并不方便,如果不需要那么高的定制性,并追求方便高效.可以使用类似QQ影音之类的软件,自带截 ...