Phantomjs loads pages slowly

2020-05-16 00:37发布

问题:

I'm new into phantomjs, trying it on a standard centOS server (with httpd etc installed, but no modified settings apart from nameservers set to 8.8.8.8 and 8.8.4.4).

I'm using the default loadspeed.js file (be it renamed). However, page speeds appear to be extremely slow. Here's an example:

$ 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

When I load any url on the server (such as http://something.be ), loadtime is 141msec:

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

Does anyone have a clue what causes my connection to be this slow? The connection itself is fine, wget takes seconds to download a file of several MB.

Also, when I run the exact same script on OSX locally for Google, this is the output:

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

回答1:

Found it - seems like ipv6 was the culprit.

I disabled it temporarily by running the following:

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

Testing confirms:

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


回答2:

Well, in my case, the page was waiting for some GET requests and was not able to reach the requests' server and it kept waiting for long. I could only figure it out when i used the remote debugger option.

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

and inside the loadspeed.js file

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

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

and then loading localhost:9000 in any webkit browser (safari/chrome) and seeing the console logs where i could figure out it was waiting for some failed requests for a long time.

TO BYPASS THIS - REDUCE THE TIMEOUT:

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

and things were very quick after that. Hope this helps