-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagemetrics.js
executable file
·182 lines (153 loc) · 4.23 KB
/
pagemetrics.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Page metrics class
* Measure all the things
*/
var PageMetrics = function( o ) {
/**
* Reference to this that won't be overriden in closures
* @var object
*/
var that = this;
/**
* URL of the page to measure
* @var string
*/
this.url = o.url;
/**
* The user role (if any) required before the page can be logged into
* @var string
*/
this.role = o.role;
/**
* The WebPage object used to "browse" the page
* @var object
*/
this.page = null;
/**
* Are we done yet?
* @var bool
*/
this.finished = true;
/**
* Number of requests this page took
* @var int
*/
this.num_requests = 0;
/**
* Number of bytes this page took
* @var int
*/
this.num_bytes = 0;
/**
* When the request started
* @var object
*/
this.start_time = null;
/**
* When the request finished
* @var object
*/
this.end_time = null;
/**
* Load time
* @var float
*/
this.load_time = 0.0;
/**
* What to do when we're done measuring
* @var callback
*/
this.callback = null;
/**
* Callback for received data event
* @param object res
*/
this.onResourceReceived = function( res ) {
if ( "start" === res.stage ) {
that.num_bytes += res.bodySize;
logger.debug( "[<--]" + res.url + " [" + res.bodySize + " bytes]");
}
};
/**
* Callback for requested data event
* @param object req
*/
this.onResourceRequested = function( req ) {
that.num_requests++;
logger.debug("[-->] " + req.url);
};
/**
* Calcluate a difference (in seconds) between two dates
* @param Date date1
* @param Date date2
* @return float
*/
this.diff_in_seconds = function( date1, date2 ) {
return ( date1.getTime() - date2.getTime() ) / 1000;
};
/**
* Finish measuring
*/
this.finish_measuring = function() {
// Page object -- this is the browser
this.page = Config.getPageObject();
// These aren't reliable yet ...
// it appears to be related to a bug in phantom and 304 (or non-200??) statuses
// https://groups.google.com/forum/?fromgroups=#!searchin/phantomjs/304/phantomjs/yPdrsS3KOJ4/U1eNRy4_yKwJ
this.page.onResourceRequested = that.onResourceRequested;
this.page.onResourceReceived = that.onResourceReceived;
// Open the page
this.start_time = new Date();
this.num_bytes = 0.0;
this.num_requests = 0;
logger.debug( "[REQUEST] Opening " + this.url );
this.page.onUrlChanged = function(url) {
logger.error("[REQUEST] URL unexpectedly changed: " + url);
}
this.page.open( this.url, function( status ) {
// Page is done
that.end_time = new Date();
// Screenshot
if ( "success" === status ) {
if ( Config.save_screenshots && !fs.exists( CryptoJS.SHA1( that.url ) ) ) {
logger.debug( "[SCREENSHOT] Saving screenshot for " + that.url + " to " + Config.screenshots_dir + "/" + CryptoJS.SHA1( that.url ) + ".png" );
that.page.render( Config.screenshots_dir + "/" + CryptoJS.SHA1( that.url ) + ".png" );
}
} else {
logger.error( "[REQUEST] " + that.url + " returned status " + status );
}
// Display the metrics
that.load_time = that.diff_in_seconds( that.end_time, that.start_time );
logger.debug( "[METRICS] URL: " + that.url + " Requests: " + that.num_requests + ", Bytes: " + that.num_bytes + ", Load time: " + that.load_time );
// Done -- we're killable now
that.finished = true;
// Callback
that.callback.call( that.callback, {
'url' : that.url,
'load_time' : that.load_time,
'num_bytes' : that.num_bytes,
'num_requests' : that.num_requests,
'screenshot' : ( Config.save_screenshots ? Config.screenshots_dir + "/" + CryptoJS.SHA1( that.url ) + ".png" : "")
});
});
};
/**
* Wire up the callbacks, start the browser
*/
this.measure = function( callback ) {
// Mark the request as busy so the main loop doesn't die until we're finished
this.finished = false;
// Save the callback ref
this.callback = callback;
// Login first
if ( undefined !== this.role && null !== this.role ) {
var login = new Login( this.role, function ( ) {
logger.debug( "[REQUEST] Login complete." );
that.finish_measuring();
} );
// Or just measure
} else {
this.finish_measuring();
}
};
};