Phantomjs缓慢加载页面

时间:2013-03-28 14:16:22

标签: connection phantomjs

我是phantomjs的新手,在标准的centOS服务器上尝试(安装了httpd等,但除了名称服务器设置为8.8.8.8和8.8.4.4之外没有修改过的设置)。

我正在使用默认的loadspeed.js文件(重命名)。但是,页面速度似乎非常慢。这是一个例子:

$ phantomjs phantomjs.js  http://www.google.com/
starting
Loading time 90928 msec

$ phantomjs phantomjs.js http://173.194.67.138/ #(one of google's public ips)
starting
Loading time 30204 msec

当我在服务器上加载任何网址时(例如http://something.be),加载时间为141毫秒:

 $ phantomjs phantomjs.js http://something.be
 starting
 Loading time 141 msec

有没有人知道是什么导致我的连接变慢?连接本身很好,wget需要几秒钟才能下载几MB的文件。

另外,当我在OSX本地运行OSX上的完全相同的脚本时,这就是输出:

 phantomjs phantomjs.js http://google.com/
 starting
 Loading time 430 msec

2 个答案:

答案 0 :(得分:23)

发现它 - 似乎ipv6是罪魁祸首。

我通过运行以下内容暂时禁用它:

echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6 
echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6

测试确认:

$ phantomjs phantomjs.js http://google.com
starting
Loading time 230 msec

答案 1 :(得分:8)

嗯,在我的情况下,页面正在等待一些GET请求,并且无法访问请求的服务器并且它一直等待很长时间。我只能在使用远程调试器选项时解决这个问题。

phantomjs --remote-debugger-port=9000 loadspeed.js <some_url>

并在loadspeed.js文件中

page.onResourceRequested = function (req) {
    console.log('requested: ' + JSON.stringify(req, undefined, 4));
};

page.onResourceReceived = function (res) {
    console.log('received: ' + JSON.stringify(res, undefined, 4));
};

然后在任何webkit浏览器(safari / chrome)中加载localhost:9000并查看控制台日志,我可以在其中找出它长时间等待一些失败的请求。

通过旁观 - 减少超时:

page.settings.resourceTimeout = 3000; //in secs

之后事情很快。希望这有帮助

相关问题