初探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. 框架搭建资源 (一) V(视图)C(控制)模式

    pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncodin ...

  2. SpringMVC1

    itRed You are never too old to set another goal or to dream a new dream. SpringMVC一路总结(一) SpringMVC听 ...

  3. java_maven_linux_windows下项目运行jar

    我才用的是eclipse自带的export功能,暂时没把maven的打包插件研究清楚 导出jar包后,需要用解压缩打开,把配置文件copy到压缩包的内的顶级目录 保存 写 bat 脚本 @author ...

  4. 【BZOJ3450】【Tyvj1952】Easy 可能DP

    联系: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  5. git合并远端分支到本地分支的两种方式

    作者:zhanhailiang 日期:2014-11-21 在使用版本号工具提交改动之前,都须要通过update先将本地代码更新到最新版本号.SVN通过svn update就能够实现,那么git怎样实 ...

  6. 专访雷水果国:离1.5K至18K 一个程序猿5每年的成长之路

    我只是一个小菜鸟,对于自主学习和交流PHP(jquery,linux,lamp,shell,javascript,server)等一系列的知识.小菜鸟创建了一个群.希望光临本博客的人能够进来交流. 寻 ...

  7. 理解iOS软件开发框架

    iOS软件开发框架理解 这个东西是硬伤,框架?自带的mvc? 自带的UIViewController UIView UINavigationController 这些算不算?当然算的,cocoa框架嘛 ...

  8. Ubuntu 14.04 关机键无效解决方法

         这几天開始研究ubuntu 14.04软件,安装Cairo-Dock后发现右上角的关机.重新启动.注销菜单点击都没了反应仅仅能通过命令实现,后来经过研究,发现仅仅要设置了 Cairo-Doc ...

  9. 1_BLE nRF51822 UART 与 BLE转发

    去年Noridc出了集成蓝牙4.0并能开口说话24L01通信芯片,这部电影可以非常小包装.和低功耗.非常适合于可穿戴设备,然后挖了一个免费的手在不久的将来AK II,又没了一个Becon的板子.先玩了 ...

  10. C++ ofstream和ifstream具体的方法和C语言file说明

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,事实上所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,全部的I/O都以这个"流"类为基础的,包含我 ...