【问题标题】:loadspeed.js timings : onload, DomContentLoaded or something else?loadspeed.js 计时:onload、DomContentLoaded 还是其他?
【发布时间】:2013-04-03 21:21:51
【问题描述】:

loadspeed.js 的时间是否正确?

因为我在 loaspeed.js 和 Chrome 中的 developerpor 工具栏之间有不同的结果。

var page = require('webpage').create(),
    system = require('system'),
    t, address;

page.viewportSize = { width: 1024, height: 768 };

if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');
    phantom.exit(1);
} else {
    t = Date.now();
    address = system.args[1];
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } else {
            t = Date.now() - t;
            console.log('#1 Loading time ' + t + ' msec');
            t = Date.now();
             page.open(address, function (status) {
            if (status !== 'success') {
                console.log('FAIL to load the address');
            } else {
                t = Date.now() - t;
                console.log('#2 Loading time ' + t + ' msec');  
            }
            phantom.exit();
        });
        }
    });
}

运行脚本给我

>phantomjs.exe loadspeed.js http://www.google.com
#1 Loading time 348 msec
#2 Loading time 202 msec

在私有模式下使用 Chrome 开发人员工具栏,我可以看到这一点(两次运行几乎相同)

如您所见,我没有相同的结果(注意:每次),它最终建议 加载速度测量 DOMContentLoaded 事件

该脚本中是否有任何“未配置”功能?

也许我错了,但很简单,我怎样才能测量页面加载时间?

【问题讨论】:

  • 比较Date.now() 的值充满了问题,#1 是 JavaScript Date 对象的分辨率。请参阅stackoverflow.com/questions/131068/… 了解更多信息。
  • 谢谢,但我只想要一个大概的加载时间:分辨率是否为 50 毫秒并不重要。

标签: javascript phantomjs headless


【解决方案1】:

在调用 page.open 之前试试这个:

page.onInitialized = function() {
    page.evaluate(function() {
        document.addEventListener('load', function() {
            t = Date.now() - t;
            console.log('#1 Loading time ' + t + ' msec');
        }, false);
    });
};

【讨论】:

  • 感谢您的建议,但您无法在文档上添加负载。你必须调用 window.addEventListener
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 2010-09-11
  • 2022-03-07
  • 2010-11-08
  • 1970-01-01
相关资源
最近更新 更多