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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Classes and functions used during recording and playback.
*/
var Benchmark = Benchmark || {};
Benchmark.functionList = [
['setTimeout', 'setTimeout'],
['clearTimeout', 'clearTimeout'],
['setInterval', 'setInterval'],
['clearInterval', 'clearInterval'],
['XMLHttpRequest', 'XMLHttpRequest'],
['addEventListenerToWindow', 'addEventListener'],
['addEventListenerToNode', 'addEventListener', ['Node', 'prototype']],
['removeEventListenerFromNode', 'removeEventListener', ['Node', 'prototype']],
['addEventListenerToXHR', 'addEventListener',
['XMLHttpRequest', 'prototype']],
['random', 'random', ['Math']],
['Date', 'Date'],
['documentWriteln', 'writeln', ['document']],
['documentWrite', 'write', ['document']]
];
Benchmark.timeoutMapping = [];
Benchmark.ignoredListeners = ['mousemove', 'mouseover', 'mouseout'];
Benchmark.originals = {};
Benchmark.overrides = {
setTimeout: function(callback, timeout) {
var event = {type: 'timeout', timeout: timeout};
var eventId = Benchmark.agent.createAsyncEvent(event);
var timerId = Benchmark.originals.setTimeout.call(this, function() {
Benchmark.agent.fireAsyncEvent(eventId, callback);
}, Benchmark.playback ? 0 : timeout);
Benchmark.timeoutMapping[timerId] = eventId;
return timerId;
},
clearTimeout: function(timerId) {
var eventId = Benchmark.timeoutMapping[timerId];
if (eventId == undefined) return;
Benchmark.agent.cancelAsyncEvent(eventId);
Benchmark.originals.clearTimeout.call(this, timerId);
},
setInterval: function(callback, timeout) {
console.warn('setInterval');
},
clearInterval: function(timerId) {
console.warn('clearInterval');
},
XMLHttpRequest: function() {
return new Benchmark.XMLHttpRequestWrapper();
},
addEventListener: function(type, listener, useCapture, target, targetType,
originalFunction) {
var event = {type: 'addEventListener', target: targetType, eventType: type};
var eventId = Benchmark.agent.createAsyncEvent(event);
listener.eventId = eventId;
listener.wrapper = function(e) {
Benchmark.agent.fireAsyncEvent(eventId, function() {
listener.call(target, e);
});
};
originalFunction.call(target, type, listener.wrapper, useCapture);
},
addEventListenerToWindow: function(type, listener, useCapture) {
if (Benchmark.ignoredListeners.indexOf(type) != -1) return;
Benchmark.overrides.addEventListener(
type, listener, useCapture, this, 'window',
Benchmark.originals.addEventListenerToWindow);
},
addEventListenerToNode: function(type, listener, useCapture) {
if (Benchmark.ignoredListeners.indexOf(type) != -1) return;
Benchmark.overrides.addEventListener(
type, listener, useCapture, this, 'node',
Benchmark.originals.addEventListenerToNode);
},
addEventListenerToXHR: function(type, listener, useCapture) {
Benchmark.overrides.addEventListener(
type, listener, useCapture, this, 'xhr',
Benchmark.originals.addEventListenerToXHR);
},
removeEventListener: function(type, listener, useCapture, target,
originalFunction) {
Benchmark.agent.cancelAsyncEvent(listener.eventId);
originalFunction.call(target, listener.wrapper, useCapture);
},
removeEventListenerFromWindow: function(type, listener, useCapture) {
removeEventListener(type, listener, useCapture, this,
Benchmark.originals.removeEventListenerFromWindow);
},
removeEventListenerFromNode: function(type, listener, useCapture) {
removeEventListener(type, listener, useCapture, this,
Benchmark.originals.removeEventListenerFromNode);
},
removeEventListenerFromXHR: function(type, listener, useCapture) {
removeEventListener(type, listener, useCapture, this,
Benchmark.originals.removeEventListenerFromXHR);
},
random: function() {
return Benchmark.agent.random();
},
Date: function() {
var a = arguments;
var D = Benchmark.originals.Date, d;
switch(a.length) {
case 0: d = new D(Benchmark.agent.dateNow()); break;
case 1: d = new D(a[0]); break;
case 2: d = new D(a[0], a[1]); break;
case 3: d = new D(a[0], a[1], a[2]); break;
default: Benchmark.die('window.Date', arguments);
}
d.getTimezoneOffset = function() { return -240; };
return d;
},
dateNow: function() {
return Benchmark.agent.dateNow();
},
documentWriteln: function() {
console.warn('writeln');
},
documentWrite: function() {
console.warn('write');
}
};
/**
* Replaces window functions specified by Benchmark.functionList with overrides
* and optionally saves original functions to Benchmark.originals.
* @param {Object} wnd Window object.
* @param {boolean} storeOriginals When true, original functions are saved to
* Benchmark.originals.
*/
Benchmark.installOverrides = function(wnd, storeOriginals) {
// Substitute window functions with overrides.
for (var i = 0; i < Benchmark.functionList.length; ++i) {
var info = Benchmark.functionList[i], object = wnd;
var propertyName = info[1], pathToProperty = info[2];
if (pathToProperty)
for (var j = 0; j < pathToProperty.length; ++j)
object = object[pathToProperty[j]];
if (storeOriginals)
Benchmark.originals[info[0]] = object[propertyName];
object[propertyName] = Benchmark.overrides[info[0]];
}
wnd.__defineSetter__('onload', function() {
console.warn('window.onload setter')}
);
// Substitute window functions of static frames when DOM content is loaded.
Benchmark.originals.addEventListenerToWindow.call(wnd, 'DOMContentLoaded',
function() {
var frames = document.getElementsByTagName('iframe');
for (var i = 0, frame; frame = frames[i]; ++i) {
Benchmark.installOverrides(frame.contentWindow);
}
}, true);
// Substitute window functions of dynamically added frames.
Benchmark.originals.addEventListenerToWindow.call(
wnd, 'DOMNodeInsertedIntoDocument', function(e) {
if (e.target.tagName && e.target.tagName.toLowerCase() != 'iframe')
return;
if (e.target.contentWindow)
Benchmark.installOverrides(e.target.contentWindow);
}, true);
};
// Install overrides on top window.
Benchmark.installOverrides(window, true);
/**
* window.XMLHttpRequest wrapper. Notifies Benchmark.agent when request is
* opened, aborted, and when it's ready state changes to DONE.
* @constructor
*/
Benchmark.XMLHttpRequestWrapper = function() {
this.request = new Benchmark.originals.XMLHttpRequest();
this.wrapperReadyState = 0;
};
// Create XMLHttpRequestWrapper functions and property accessors using original
// ones.
(function() {
var request = new Benchmark.originals.XMLHttpRequest();
for (var property in request) {
if (property === 'channel') continue; // Quick fix for FF.
if (typeof(request[property]) == 'function') {
(function(property) {
var f = Benchmark.originals.XMLHttpRequest.prototype[property];
Benchmark.XMLHttpRequestWrapper.prototype[property] = function() {
f.apply(this.request, arguments);
};
})(property);
} else {
(function(property) {
Benchmark.XMLHttpRequestWrapper.prototype.__defineGetter__(property,
function() { return this.request[property]; });
Benchmark.XMLHttpRequestWrapper.prototype.__defineSetter__(property,
function(value) {
this.request[property] = value;
});
})(property);
}
}
})();
// Define onreadystatechange getter.
Benchmark.XMLHttpRequestWrapper.prototype.__defineGetter__('onreadystatechange',
function() { return this.clientOnReadyStateChange; });
// Define onreadystatechange setter.
Benchmark.XMLHttpRequestWrapper.prototype.__defineSetter__('onreadystatechange',
function(value) { this.clientOnReadyStateChange = value; });
Benchmark.XMLHttpRequestWrapper.prototype.__defineGetter__('readyState',
function() { return this.wrapperReadyState; });
Benchmark.XMLHttpRequestWrapper.prototype.__defineSetter__('readyState',
function() {});
/**
* Wrapper for XMLHttpRequest.open.
*/
Benchmark.XMLHttpRequestWrapper.prototype.open = function() {
var url = Benchmark.extractURL(arguments[1]);
var event = {type: 'request', method: arguments[0], url: url};
this.eventId = Benchmark.agent.createAsyncEvent(event);
var request = this.request;
var requestWrapper = this;
Benchmark.originals.XMLHttpRequest.prototype.open.apply(request, arguments);
request.onreadystatechange = function() {
if (this.readyState != 4 || requestWrapper.cancelled) return;
var callback = requestWrapper.clientOnReadyStateChange || function() {};
Benchmark.agent.fireAsyncEvent(requestWrapper.eventId, function() {
requestWrapper.wrapperReadyState = 4;
callback.call(request);
});
}
};
/**
* Wrapper for XMLHttpRequest.abort.
*/
Benchmark.XMLHttpRequestWrapper.prototype.abort = function() {
this.cancelled = true;
Benchmark.originals.XMLHttpRequest.prototype.abort.apply(
this.request, arguments);
Benchmark.agent.cancelAsyncEvent(this.eventId);
};
/**
* Driver url for reporting results.
* @const {string}
*/
Benchmark.DRIVER_URL = '/benchmark/';
/**
* Posts request as json to Benchmark.DRIVER_URL.
* @param {Object} request Request to post.
*/
Benchmark.post = function(request, async) {
if (async === undefined) async = true;
var xmlHttpRequest = new Benchmark.originals.XMLHttpRequest();
xmlHttpRequest.open("POST", Benchmark.DRIVER_URL, async);
xmlHttpRequest.setRequestHeader("Content-type", "application/json");
xmlHttpRequest.send(JSON.stringify(request));
};
/**
* Extracts url string.
* @param {(string|Object)} url Object or string representing url.
* @return {string} Extracted url.
*/
Benchmark.extractURL = function(url) {
if (typeof(url) == 'string') return url;
return url.nI || url.G || '';
};
/**
* Logs error message to console and throws an exception.
* @param {string} message Error message
*/
Benchmark.die = function(message) {
// Debugging stuff.
var position = top.Benchmark.playback ? top.Benchmark.agent.timelinePosition :
top.Benchmark.agent.timeline.length;
message = message + ' at position ' + position;
console.error(message);
Benchmark.post({error: message});
console.log(Benchmark.originals.setTimeout.call(window, function() {}, 9999));
try { (0)() } catch(ex) { console.error(ex.stack); }
throw message;
};
|