forked from jmervine/phantomjs-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiming.js
executable file
·100 lines (86 loc) · 2.28 KB
/
timing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env phantomjs
/***********************************************************
* Author: @mervinej
* Licence: MIT
* Date: 11/27/2013
*
* Run with:
*
* $ phantomjs ./timing.js ./urls.txt
*
* or
*
* $ phantomjs ./timing.js \
* "http://foo.com, http://foo.com/bar"
*
* '--json' returns JSON output for parsing with Phapper
* (http://github.com/jmervine/phapper).
*
* Note: As a bonus, I left the page timing as well from
* the example script I started this from.
*
***********************************************************/
var webpage = require('webpage');
var system = require('system');
var util = require('../common/util');
var args = system.args.copyArgs();
/***********************************************************
* Add any domains you wish to exclude to this array.
*
* TODO: Support populating this array from an external
* source.
***********************************************************/
function usage() {
console.log('Usage: timing.js <URL(s)>|<URL(s) file> [--json]');
phantom.exit();
}
if (args.length === 0) {
usage();
}
var json = args.getArg(['--json', '-j'], false);
var addresses = util.parsePaths(args.shift());
var finished = 0;
if (addresses.length === 0) {
usage();
}
var results = [];
var limit = 15;
var running = 1;
function launcher(){
running--;
while(running < limit && addresses.length > 0){
running++;
collectData(addresses.shift());
}
if(running < 1 && addresses.length < 1){
if (json) {
console.dir(results);
}
phantom.exit();
}
}
function collectData(address) {
var t = Date.now();
var page = webpage.create();
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
if (json) {
results.push({
address: address,
complete: t
});
} else {
console.log('Regarding: ' + address);
console.log('> took ' + t + ' msec');
console.log(' ');
}
}
(page.close||page.release)();
launcher();
return;
});
}
launcher();