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

page.settings.resourceTimeout = 120000; // 2 minutes

page.onResourceTimeout = function(e) {
    console.log('ERROR= loadspeed.js <some URL>');
    localStorage.clear();
    phantom.exit(1);
};

if (system.args.length === 1) {
    console.log('ERROR= loadspeed.js <some URL>');
    localStorage.clear();
    phantom.exit();
}

page.onError = function(msg, err) {
    localStorage.clear();
};

t = Date.now();
address = system.args[1];

page.open(address, function(status) {
    if (status !== 'success') {
        localStorage.clear();
        console.log('ERROR=FAIL to load the address');
    } else {
        t = Date.now() - t;
        localStorage.clear();
        console.log('TIME=' + t);
    }
    page.release();
    localStorage.clear();
    phantom.exit();
});
