简体   繁体   English

如何在浏览器中监控渲染时间?

[英]How can I monitor the rendering time in a browser?

I work on an internal corporate system that has a web front-end using Tomcat. 我在一个内部企业系统上工作,该系统具有使用Tomcat的Web前端。

  1. How can I monitor the rendering time of specific pages in a browser (IE6)? 如何在浏览器(IE6)中监控特定页面的渲染时间?
  2. I would like to be able to record the results in a log file (separate log file or the Tomcat access log). 我希望能够将结果记录在日志文件中(单独的日志文件或Tomcat访问日志)。

EDIT: Ideally, I need to monitor the rendering on the clients accessing the pages. 编辑:理想情况下,我需要监控访问页面的客户端上的呈现。

The Navigation Timing API is available in modern browsers (IE9+) except Safari: 导航时序API在现代浏览器(IE9 +)中可用,但Safari除外:

function onLoad() { 
  var now = new Date().getTime();
  var page_load_time = now - performance.timing.navigationStart;
  console.log("User-perceived page loading time: " + page_load_time);
}

In case a browser has JavaScript enabled one of the things you could do is to write an inline script and send it first thing in your HTML. 如果浏览器启用了JavaScript,您可以做的一件事就是编写内联脚本并在HTML中首先发送它。 The script would do two things: 该脚本将做两件事:

  1. Record current system time in a JS variable (if you're lucky the time could roughly correspond to the page rendering start time). 在JS变量中记录当前系统时间(如果幸运的话,时间可能大致对应于页面呈现开始时间)。
  2. Attach JS function to the page onLoad event. 将JS函数附加到页面onLoad事件。 This function will then query the current system time once again, subtract the start time from step 1 and send it to the server along with the page location (or some unique ID you could insert into the inline script dynamically on your server). 然后,此函数将再次查询当前系统时间,从步骤1中减去开始时间,并将其与页面位置一起发送到服务器(或者您可以在服务器上动态插入内联脚本的某个唯一ID)。
<script language="JavaScript">
var renderStart = new Date().getTime();
window.onload=function() { 
   var elapsed = new Date().getTime()-renderStart;
   // send the info to the server 
   alert('Rendered in ' + elapsed + 'ms'); 
} 

</script>

... usual HTML starts here ...

You'd need to make sure that the page doesn't override onload later in the code, but adds to the event handlers list instead. 您需要确保页面稍后不会覆盖代码中的onload,而是添加到事件处理程序列表中。

As far as non-invasive techniques are concerned, Hammerhead measures complete load time (including JavaScript execution), albeit in Firefox only. 就非侵入性技术而言, Hammerhead测量完整的加载时间(包括JavaScript执行),尽管仅在Firefox中。

I've seen usable results when a JavaScript snippet could be added globally to measure the start and end of each page load operation. 当全局添加JavaScript代码段来衡量每个页面加载操作的开始和结束时,我看到了可用的结果。

Have a look at Selenium - they offer a remote control that can automatically start different browsers (eg IE6), load pages, test for specific content on the page. 看看Selenium - 他们提供了一个遥控器,可以自动启动不同的浏览器(例如IE6),加载页面,测试页面上的特定内容。 At the end reports are generated that also show the rendering times. 最后生成报告,同时显示渲染时间。

Since others are posting answers that use other browsers, I guess I will too. 由于其他人发布使用其他浏览器的答案,我想我也会。 Chrome has a very detailed profiling system that breaks down the rendering time of the page and shows the time it took for each step along the way. Chrome有一个非常详细的分析系统,它可以分解页面的渲染时间,并显示每个步骤所花费的时间。

As for IE, you might want to consider writing a plugin. 至于IE,你可能想考虑编写一个插件。 There seems to be few tools like this on the market. 市场上似乎没有这样的工具。 Maybe you could sell it. 也许你可以卖掉它。

On Firefox you can use Firebug to monitor load time. 在Firefox上,您可以使用Firebug来监控加载时间。 With the YSlow plugin you can even get recommendations how to improve the performance. 使用YSlow插件,您甚至可以获得如何提高性能的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM