我是新手,刚刚学会使用树莓派和phantomjs。现在,我正在使用phantomjs来测量网站的加载速度测试,因此我想知道如何使用bash脚本同时加载多个url(website)。请帮助我,谢谢,谢谢。
这里是phantomjs给出的原始代码。
var page = require('webpage').create(),
system = require('system'),
t, address;
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('Page title is ' + page.evaluate(function () {
return document.title;
}));
console.log('Loading time ' + t + ' msec');
}
phantom.exit();
});
}
我想做与您尝试的完全相同的事情-我创建了一个副项目来测量实际加载时间。它使用Node和Nightmare(以前在引擎盖下使用PhantomJS,但现在使用Electron)来操纵无头(“不可见”)Web浏览器。加载所有资源后,它将报告完全加载页面所花费的毫秒数。
对您有用的一个不错的功能是,它会反复加载网页。因此,您可以多次加载页面,然后取平均值以得到一个不错的整数值。另外,由于此脚本在命令行上运行,因此在不同计算机上安装并从不同位置获取实际加载时间很简单。 (加载时间不仅取决于服务器,还取决于客户端和中介程序)
示例用法:
website-loading-time rinogo$ node website-loading-time.js https://google.com
1657
967
1179
1005
1084
1076
...
https://github.com/rinogo/website-loading-time
披露:我是这个项目的作者。