Created
June 26, 2012 03:12
-
-
Save qzaidi/2993032 to your computer and use it in GitHub Desktop.
Phantomjs page load measurements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var page = require('webpage').create(), | |
system = require('system'), | |
address,time,tries,j; | |
if (system.args.length === 1) { | |
console.log('Usage: loadspeed.js <some URL>'); | |
phantom.exit(); | |
} else { | |
address = system.args[1]; | |
tries = Number(system.args[2]) || 10; | |
j = tries; | |
time = 0; | |
(function repeat(lasttime) { | |
if (lasttime) | |
time += lasttime; | |
if (j--) { | |
loadpage(address,repeat); | |
} else { | |
console.log ('Average Load time is ' + time/tries); | |
phantom.exit(); | |
} | |
}(0)); | |
} | |
function loadpage(address,next) { | |
console.log('loading address ' + address); | |
var 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('Page title is ' + page.evaluate(function () { | |
return document.title; | |
})); | |
console.log('Loading time ' + t + ' msec'); | |
} | |
next(t); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment