<commit_before>var system = require('system');
var args = system.args;
var page = require('webpage').create();

log = [];
count = 0;

page.settings.resourceTimeout = 45000;
page.onResourceTimeout = function(e) {
    console.error(e.errorCode, ': ', e.errorString);
    phantom.exit(1);
};

page.onLoadFinished = function(status) {
    count++;
    if(args[2] == '--detail-log') {
        log.push({"request": count, "timeSpend": timeDiff, "content": page.plainText});
    }

    if(page.plainText.search("Time") != -1 || page.plainText.search("Invalid") != -1) {
        timeEndPage = new Date();
        output = {
            "totalRequests": count,
            "timeStart": timeStartPage.getTime()/1000,
            "timeEnd": timeEndPage.getTime()/1000,
            "timeSpend": (timeEndPage - timeStartPage)/1000
        };
        if(args[2] == '--detail-log') {
            output.log = log;
        }
        console.log(JSON.stringify(output));
        phantom.exit();
    }
};

page.onResourceRequested = function(requestData, networkRequest) {
    timeStartRequest = requestData['time'];
};

page.onResourceReceived = function(response) {
    if(response.stage == 'end') {
        timeDiff = (response['time'] - timeStartRequest)/1000;
    }
};

timeStartPage = new Date();
page.open(args[1]);
<commit_msg>add error handling in client.js<commit_after>var system = require('system');
var args = system.args;
var page = require('webpage').create();

log = [];
count = 0;

page.settings.resourceTimeout = 45000;
page.onResourceTimeout = function(e) {
    console.error('Resource Timeout: ' + e.errorString + ' (' + e.errorCode + ')');
};

page.onResourceError = function(e) {
    console.error('Resource Error: ' + e.errorString + ' (' + e.errorCode + ')');
};

page.onLoadFinished = function(status) {
    if(status != 'success') {
        phantom.exit(1);
    }
    else {
        count++;
        if(args[2] == '--detail-log') {
            log.push({'request': count, 'timeSpend': timeDiff, 'content': page.plainText});
        }

        if(page.plainText.search('Time') != -1 || page.plainText.search('Invalid') != -1) {
            timeEndPage = new Date();
            timeSpend = (timeEndPage - timeStartPage)/1000;

            output = {
                'totalRequests': count,
                'timeStart': timeStartPage/1000,
                'timeEnd': timeEndPage/1000,
                'timeSpend': timeSpend
            };
            if(args[2] == '--detail-log') {
                output.log = log;
            }

            console.log(JSON.stringify(output));
            console.error(args[1].match(/(\d+\.){3}\d+/g) + ': ' + timeSpend + 's');

            phantom.exit();
        }
    }
};

page.onResourceRequested = function(requestData, networkRequest) {
    timeStartRequest = requestData['time'];
};

page.onResourceReceived = function(response) {
    if(response.stage == 'end') {
        timeDiff = (response['time'] - timeStartRequest)/1000;
    }
};

timeStartPage = new Date();
page.open(args[1]);

{'fraction_non_alphanumeric': 0.17445687952600394, 'fraction_numerical': 0.019091507570770244, 'mean_word_length': 2.5752941176470587, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '848436', 'n_tokens_mistral': 992, 'n_tokens_neox': 938, 'n_words': 158}