forked from YahooArchive/boomerang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boomerang.js
1270 lines (1040 loc) · 32.4 KB
/
boomerang.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
*/
/**
\file boomerang.js
boomerang measures various performance characteristics of your user's browsing
experience and beacons it back to your server.
\details
To use this you'll need a web site, lots of users and the ability to do
something with the data you collect. How you collect the data is up to
you, but we have a few ideas.
*/
// beaconing section
// the parameter is the window
(function(w) {
var impl, boomr, k, d=w.document;
// Short namespace because I don't want to keep typing BOOMERANG
if(typeof BOOMR === "undefined") {
BOOMR = {};
}
// don't allow this code to be included twice
if(BOOMR.version) {
return;
}
BOOMR.version = "0.9";
// impl is a private object not reachable from outside the BOOMR object
// users can set properties by passing in to the init() method
impl = {
// properties
beacon_url: "",
// strip out everything except last two parts of hostname.
// This doesn't work well for domains that end with a country tld,
// but we allow the developer to override site_domain for that.
site_domain: w.location.hostname.
replace(/.*?([^.]+\.[^.]+)\.?$/, '$1').
toLowerCase(),
//! User's ip address determined on the server. Used for the BA cookie
user_ip: '',
events: {
"page_ready": [],
"page_unload": [],
"visibility_changed": [],
"before_beacon": []
},
vars: {},
disabled_plugins: {},
fireEvent: function(e_name, data) {
var i, h, e;
if(!this.events.hasOwnProperty(e_name)) {
return false;
}
e = this.events[e_name];
for(i=0; i<e.length; i++) {
h = e[i];
h[0].call(h[2], data, h[1]);
}
return true;
},
addListener: function(el, sType, fn, capture) {
if(el.addEventListener) {
el.addEventListener(sType, fn, (capture));
}
else if(el.attachEvent) {
el.attachEvent("on" + sType, fn);
}
}
};
// We create a boomr object and then copy all its properties to BOOMR so that
// we don't overwrite anything additional that was added to BOOMR before this
// was called... for example, a plugin.
boomr = {
// Utility functions
utils: {
getCookie: function(name) {
if(!name) {
return null;
}
name = ' ' + name + '=';
var i, cookies;
cookies = ' ' + d.cookie + ';';
if ( (i=cookies.indexOf(name)) >= 0 ) {
i += name.length;
cookies = cookies.substring(i, cookies.indexOf(';', i));
return cookies;
}
return null;
},
setCookie: function(name, subcookies, max_age, path, domain, sec) {
var value = "",
k, nameval, c,
exp = "";
if(!name) {
return false;
}
for(k in subcookies) {
if(subcookies.hasOwnProperty(k)) {
value += '&' + encodeURIComponent(k)
+ '=' + encodeURIComponent(subcookies[k]);
}
}
value = value.replace(/^&/, '');
if(max_age) {
exp = new Date();
exp.setTime(exp.getTime() + max_age*1000);
exp = exp.toGMTString();
}
nameval = name + '=' + value;
c = nameval +
((max_age) ? "; expires=" + exp : "" ) +
((path) ? "; path=" + path : "") +
((typeof domain !== "undefined") ? "; domain="
+ (domain !== null ? domain : impl.site_domain ) : "") +
((sec) ? "; secure" : "");
if ( nameval.length < 4000 ) {
d.cookie = c;
// confirm cookie was set (could be blocked by user's settings, etc.)
return ( value === this.getCookie(name) );
}
return false;
},
getSubCookies: function(cookie) {
var cookies_a,
i, l, kv,
cookies={};
if(!cookie) {
return null;
}
cookies_a = cookie.split('&');
if(cookies_a.length === 0) {
return null;
}
for(i=0, l=cookies_a.length; i<l; i++) {
kv = cookies_a[i].split('=');
kv.push(""); // just in case there's no value
cookies[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
}
return cookies;
},
removeCookie: function(name) {
return this.setCookie(name, {}, 0, "/", null);
},
pluginConfig: function(o, config, plugin_name, properties) {
var i, props=0;
if(!config || !config[plugin_name]) {
return false;
}
for(i=0; i<properties.length; i++) {
if(typeof config[plugin_name][properties[i]] !== "undefined") {
o[properties[i]] = config[plugin_name][properties[i]];
props++;
}
}
return (props>0);
}
},
init: function(config) {
var i, k,
properties = ["beacon_url", "site_domain", "user_ip"];
if(!config) {
config = {};
}
for(i=0; i<properties.length; i++) {
if(typeof config[properties[i]] !== "undefined") {
impl[properties[i]] = config[properties[i]];
}
}
if(typeof config.log !== "undefined") {
this.log = config.log;
}
if(!this.log) {
this.log = function(m,l,s) { };
}
for(k in this.plugins) {
// config[pugin].enabled has been set to false
if( config[k]
&& typeof config[k].enabled !== "undefined"
&& config[k].enabled === false
) {
impl.disabled_plugins[k] = 1;
continue;
}
else if(impl.disabled_plugins[k]) {
delete impl.disabled_plugins[k];
}
// plugin exists and has an init method
if(this.plugins.hasOwnProperty(k)
&& typeof this.plugins[k].init === "function"
) {
this.plugins[k].init(config);
}
}
// The developer can override onload by setting autorun to false
if(typeof config.autorun === "undefined" || config.autorun !== false) {
impl.addListener(w, "load",
function() {
impl.fireEvent("page_ready");
}
);
}
// webkitvisibilitychange is useful to detect if the page loaded through prerender
impl.addListener(d, "webkitvisibilitychange",
function() {
impl.fireEvent("visibility_changed");
}
);
// This must be the last one to fire
impl.addListener(w, "unload", function() { w=null; });
return this;
},
// The page dev calls this method when they determine the page is usable.
// Only call this if autorun is explicitly set to false
page_ready: function() {
impl.fireEvent("page_ready");
return this;
},
subscribe: function(e_name, fn, cb_data, cb_scope) {
var i, h, e;
if(!impl.events.hasOwnProperty(e_name)) {
return this;
}
e = impl.events[e_name];
// don't allow a handler to be attached more than once to the same event
for(i=0; i<e.length; i++) {
h = e[i];
if(h[0] === fn && h[1] === cb_data && h[2] === cb_scope) {
return this;
}
}
e.push([ fn, cb_data || {}, cb_scope || null ]);
// Attach unload handlers directly to the window.onunload and
// window.onbeforeunload events. The first of the two to fire will clear
// fn so that the second doesn't fire. We do this because technically
// onbeforeunload is the right event to fire, but all browsers don't
// support it. This allows us to fall back to onunload when onbeforeunload
// isn't implemented
if(e_name === 'page_unload') {
impl.addListener(w, "unload",
function() {
if(fn) {
fn.call(cb_scope, null, cb_data);
}
fn=cb_scope=cb_data=null;
}
);
impl.addListener(w, "beforeunload",
function() {
if(fn) {
fn.call(cb_scope, null, cb_data);
}
fn=cb_scope=cb_data=null;
}
);
}
return this;
},
addVar: function(name, value) {
if(typeof name === "string") {
impl.vars[name] = value;
}
else if(typeof name === "object") {
var o = name, k;
for(k in o) {
if(o.hasOwnProperty(k)) {
impl.vars[k] = o[k];
}
}
}
return this;
},
removeVar: function() {
var i, params;
if(!arguments.length) {
return this;
}
if(arguments.length === 1
&& Object.prototype.toString.apply(arguments[0]) === "[object Array]") {
params = arguments[0];
}
else {
params = arguments;
}
for(i=0; i<params.length; i++) {
if(impl.vars.hasOwnProperty(params[i])) {
delete impl.vars[params[i]];
}
}
return this;
},
sendBeacon: function() {
var k, url, img, nparams=0;
// At this point someone is ready to send the beacon. We send
// the beacon only if all plugins have finished doing what they
// wanted to do
for(k in this.plugins) {
if(this.plugins.hasOwnProperty(k)) {
if(impl.disabled_plugins[k]) {
continue;
}
if(!this.plugins[k].is_complete()) {
return this;
}
}
}
// If we reach here, all plugins have completed
impl.fireEvent("before_beacon", impl.vars);
// Don't send a beacon if no beacon_url has been set
if(!impl.beacon_url) {
return this;
}
// use document.URL instead of location.href because of a safari bug
url = impl.beacon_url + '?v=' + encodeURIComponent(BOOMR.version) +
'&u=' + encodeURIComponent(d.URL.replace(/#.*/, ''));
for(k in impl.vars) {
if(impl.vars.hasOwnProperty(k)) {
nparams++;
url += "&" + encodeURIComponent(k)
+ "=" + encodeURIComponent(impl.vars[k]);
}
}
// only send beacon if we actually have something to beacon back
if(nparams) {
img = new Image();
img.src=url;
}
return this;
}
};
var make_logger = function(l) {
return function(m, s) {
this.log(m, l, "boomerang" + (s?"."+s:"")); return this;
};
};
boomr.debug = make_logger("debug");
boomr.info = make_logger("info");
boomr.warn = make_logger("warn");
boomr.error = make_logger("error");
if(w.YAHOO && w.YAHOO.widget && w.YAHOO.widget.Logger) {
boomr.log = w.YAHOO.log;
}
else if(typeof w.Y !== "undefined" && typeof w.Y.log !== "undefined") {
boomr.log = w.Y.log;
}
else if(typeof console !== "undefined" && typeof console.log !== "undefined") {
boomr.log = function(m,l,s) { console.log(s + ": [" + l + "] ", m); };
}
for(k in boomr) {
if(boomr.hasOwnProperty(k)) {
BOOMR[k] = boomr[k];
}
}
BOOMR.plugins = BOOMR.plugins || {};
}(window));
// end of boomerang beaconing section
// Now we start built in plugins.
// This is the Round Trip Time plugin. Abbreviated to RT
// the parameter is the window
(function(w) {
var d=w.document;
BOOMR = BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
// private object
var impl = {
complete: false, //! Set when this plugin has completed
timers: {}, //! Custom timers that the developer can use
// Format for each timer is { start: XXX, end: YYY, delta: YYY-XXX }
cookie: 'RT', //! Name of the cookie that stores the start time and referrer
cookie_exp:600, //! Cookie expiry in seconds
strict_referrer: true, //! By default, don't beacon if referrers don't match.
// If set to false, beacon both referrer values and let
// the back end decide
navigationStart: undefined,
responseStart: undefined,
// The start method is fired on page unload. It is called with the scope
// of the BOOMR.plugins.RT object
start: function() {
var t_end, t_start = new Date().getTime();
// Disable use of RT cookie by setting its name to a falsy value
if(!this.cookie) {
return this;
}
// We use document.URL instead of location.href because of a bug in safari 4
// where location.href is URL decoded
if(!BOOMR.utils.setCookie(this.cookie,
{ s: t_start, r: d.URL.replace(/#.*/, '') },
this.cookie_exp,
"/", null)
) {
BOOMR.error("cannot set start cookie", "rt");
return this;
}
t_end = new Date().getTime();
if(t_end - t_start > 50) {
// It took > 50ms to set the cookie
// The user Most likely has cookie prompting turned on so
// t_start won't be the actual unload time
// We bail at this point since we can't reliably tell t_done
BOOMR.utils.removeCookie(this.cookie);
// at some point we may want to log this info on the server side
BOOMR.error("took more than 50ms to set cookie... aborting: "
+ t_start + " -> " + t_end, "rt");
}
return this;
},
initNavTiming: function() {
var ti, p;
if(this.navigationStart) {
return;
}
// Get start time from WebTiming API see:
// https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html
// http://blogs.msdn.com/b/ie/archive/2010/06/28/measuring-web-page-performance.aspx
// http://blog.chromium.org/2010/07/do-you-know-how-slow-your-web-page-is.html
p = w.performance || w.msPerformance || w.webkitPerformance || w.mozPerformance;
if(p && p.timing) {
ti = p.timing;
}
else if(w.chrome && w.chrome.csi) {
// Older versions of chrome also have a timing API that's sort of documented here:
// http://ecmanaut.blogspot.com/2010/06/google-bom-feature-ms-since-pageload.html
// source here:
// http://src.chromium.org/viewvc/chrome/trunk/src/chrome/renderer/loadtimes_extension_bindings.cc?view=markup
ti = {
navigationStart: w.chrome.csi().startE,
responseStart: undefined
};
BOOMR.addVar("rt.start", "csi");
}
else if(w.gtbExternal) {
// The Google Toolbar exposes navigation start time similar to old versions of chrome
// This would work for any browser that has the google toolbar installed
ti = {
navigationStart: w.gtbExternal.startE(),
responseStart: undefined
};
BOOMR.addVar("rt.start", "gtb");
}
if(ti) {
// Always use navigationStart since it falls back to fetchStart
// If not set, we leave t_start alone so that timers that depend
// on it don't get sent back. Never use requestStart since if
// the first request fails and the browser retries, it will contain
// the value for the new request.
BOOMR.addVar("rt.start", "navigation");
this.navigationStart = ti.navigationStart || undefined;
this.responseStart = ti.responseStart || undefined;
}
else {
BOOMR.warn("This browser doesn't support the WebTiming API", "rt");
}
return;
}
};
BOOMR.plugins.RT = {
// Methods
init: function(config) {
impl.complete = false;
impl.timers = {};
BOOMR.utils.pluginConfig(impl, config, "RT",
["cookie", "cookie_exp", "strict_referrer"]);
BOOMR.subscribe("page_ready", this.done, null, this);
BOOMR.subscribe("page_unload", impl.start, null, impl);
return this;
},
startTimer: function(timer_name, time_value) {
if(timer_name) {
if (timer_name === 't_page') {
this.endTimer('t_resp', time_value);
}
impl.timers[timer_name] = {start: (typeof time_value === "number" ? time_value : new Date().getTime())};
impl.complete = false;
}
return this;
},
endTimer: function(timer_name, time_value) {
if(timer_name) {
impl.timers[timer_name] = impl.timers[timer_name] || {};
if(typeof impl.timers[timer_name].end === "undefined") {
impl.timers[timer_name].end =
(typeof time_value === "number" ? time_value : new Date().getTime());
}
}
return this;
},
setTimer: function(timer_name, time_delta) {
if(timer_name) {
impl.timers[timer_name] = { delta: time_delta };
}
return this;
},
// Called when the page has reached a "usable" state. This may be when the
// onload event fires, or it could be at some other moment during/after page
// load when the page is usable by the user
done: function() {
var t_start, r, r2,
subcookies, basic_timers = { t_done: 1, t_resp: 1, t_page: 1},
ntimers = 0, t_name, timer, t_other=[];
if(impl.complete) {
return this;
}
impl.initNavTiming();
if(document.webkitVisibilityState && document.webkitVisibilityState === "prerender") {
// This means that onload fired through a pre-render. We'll capture this
// time, but wait for t_done until after the page has become either visible
// or hidden (ie, it moved out of the pre-render state)
// http://code.google.com/chrome/whitepapers/pagevisibility.html
// http://www.w3.org/TR/2011/WD-page-visibility-20110602/
// http://code.google.com/chrome/whitepapers/prerender.html
this.startTimer("t_load", impl.navigationStart);
this.endTimer("t_load"); // this will measure actual onload time for a prerendered page
this.startTimer("t_prerender", impl.navigationStart);
this.startTimer("t_postrender"); // time from prerender to visible or hidden
BOOMR.subscribe("visibility_changed", this.done, null, this);
return this;
}
// If the dev has already called endTimer, then this call will do nothing
// else, it will stop the page load timer
this.endTimer("t_done");
if(impl.responseStart) {
// Use NavTiming API to figure out resp latency and page time
this.setTimer("t_resp", impl.responseStart - impl.navigationStart);
if(impl.timers.t_load) {
this.setTimer("t_page", impl.timers.t_load.end - impl.responseStart);
}
else {
this.setTimer("t_page", new Date().getTime() - impl.responseStart);
}
}
else if(impl.timers.hasOwnProperty('t_page')) {
// If the dev has already started t_page timer, we can end it now as well
this.endTimer("t_page");
}
// If a prerender timer was started, we can end it now as well
if(impl.timers.hasOwnProperty('t_postrender')) {
this.endTimer("t_postrender");
this.endTimer("t_prerender");
}
// A beacon may be fired automatically on page load or if the page dev fires
// it manually with their own timers. It may not always contain a referrer
// (eg: XHR calls). We set default values for these cases
r = r2 = d.referrer.replace(/#.*/, '');
// If impl.cookie is not set, the dev does not want to use cookie time
if(impl.cookie) {
subcookies = BOOMR.utils.getSubCookies(BOOMR.utils.getCookie(impl.cookie));
BOOMR.utils.removeCookie(impl.cookie);
if(subcookies !== null
&& typeof subcookies.s !== "undefined"
&& typeof subcookies.r !== "undefined"
) {
r = subcookies.r;
if(!impl.strict_referrer || r === r2) {
t_start = parseInt(subcookies.s, 10);
}
}
}
if(t_start) {
BOOMR.addVar("rt.start", "cookie");
}
else {
t_start = impl.navigationStart;
}
// make sure old variables don't stick around
BOOMR.removeVar('t_done', 't_page', 't_resp', 'r', 'r2');
for(t_name in impl.timers) {
if(!impl.timers.hasOwnProperty(t_name)) {
continue;
}
timer = impl.timers[t_name];
// if delta is a number, then it was set using setTimer
// if not, then we have to calculate it using start & end
if(typeof timer.delta !== "number") {
if(typeof timer.start !== "number") {
timer.start = t_start;
}
timer.delta = timer.end - timer.start;
}
// If the caller did not set a start time, and if there was no start cookie
// then timer.delta will be NaN, in which case we discard it.
if(isNaN(timer.delta)) {
continue;
}
if(basic_timers.hasOwnProperty(t_name)) {
BOOMR.addVar(t_name, timer.delta);
}
else {
t_other.push(t_name + '|' + timer.delta);
}
ntimers++;
}
// At this point we decide whether the beacon should be sent or not
if(ntimers) {
BOOMR.addVar("r", r);
if(r2 !== r) {
BOOMR.addVar("r2", r2);
}
if(t_other.length) {
BOOMR.addVar("t_other", t_other.join(','));
}
}
impl.timers = {};
impl.complete = true;
BOOMR.sendBeacon(); // we call sendBeacon() anyway because some other plugin
// may have blocked waiting for RT to complete
return this;
},
is_complete: function() { return impl.complete; }
};
}(window));
// End of RT plugin
// This is the Bandwidth & Latency plugin abbreviated to BW
// the parameter is the window
(function(w) {
var d=w.document;
BOOMR = BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
// We choose image sizes so that we can narrow down on a bandwidth range as
// soon as possible the sizes chosen correspond to bandwidth values of
// 14-64kbps, 64-256kbps, 256-1024kbps, 1-2Mbps, 2-8Mbps, 8-30Mbps & 30Mbps+
// Anything below 14kbps will probably timeout before the test completes
// Anything over 60Mbps will probably be unreliable since latency will make up
// the largest part of download time. If you want to extend this further to
// cover 100Mbps & 1Gbps networks, use image sizes of 19,200,000 & 153,600,000
// bytes respectively
// See https://spreadsheets.google.com/ccc?key=0AplxPyCzmQi6dDRBN2JEd190N1hhV1N5cHQtUVdBMUE&hl=en_GB
// for a spreadsheet with the details
var images=[
{ name: "image-0.png", size: 11483, timeout: 1400 },
{ name: "image-1.png", size: 40658, timeout: 1200 },
{ name: "image-2.png", size: 164897, timeout: 1300 },
{ name: "image-3.png", size: 381756, timeout: 1500 },
{ name: "image-4.png", size: 1234664, timeout: 1200 },
{ name: "image-5.png", size: 4509613, timeout: 1200 },
{ name: "image-6.png", size: 9084559, timeout: 1200 }
];
images.end = images.length;
images.start = 0;
// abuse arrays to do the latency test simply because it avoids a bunch of
// branches in the rest of the code.
// I'm sorry Douglas
images.l = { name: "image-l.gif", size: 35, timeout: 1000 };
// private object
var impl = {
// properties
base_url: 'images/',
timeout: 15000,
nruns: 5,
latency_runs: 10,
user_ip: '',
cookie_exp: 7*86400,
cookie: 'BA',
// state
results: [],
latencies: [],
latency: null,
runs_left: 0,
aborted: false,
complete: false,
running: false,
// methods
// numeric comparator. Returns negative number if a < b, positive if a > b and 0 if they're equal
// used to sort an array numerically
ncmp: function(a, b) { return (a-b); },
// Calculate the interquartile range of an array of data points
iqr: function(a)
{
var l = a.length-1, q1, q3, fw, b = [], i;
q1 = (a[Math.floor(l*0.25)] + a[Math.ceil(l*0.25)])/2;
q3 = (a[Math.floor(l*0.75)] + a[Math.ceil(l*0.75)])/2;
fw = (q3-q1)*1.5;
l++;
for(i=0; i<l && a[i] < q3+fw; i++) {
if(a[i] > q1-fw) {
b.push(a[i]);
}
}
return b;
},
calc_latency: function()
{
var i, n,
sum=0, sumsq=0,
amean, median,
std_dev, std_err,
lat_filtered;
// We first do IQR filtering and use the resulting data set
// for all calculations
lat_filtered = this.iqr(this.latencies.sort(this.ncmp));
n = lat_filtered.length;
BOOMR.debug(lat_filtered, "bw");
// First we get the arithmetic mean, standard deviation and standard error
// We ignore the first since it paid the price of DNS lookup, TCP connect
// and slow start
for(i=1; i<n; i++) {
sum += lat_filtered[i];
sumsq += lat_filtered[i] * lat_filtered[i];
}
n--; // Since we started the loop with 1 and not 0
amean = Math.round(sum / n);
std_dev = Math.sqrt( sumsq/n - sum*sum/(n*n));
// See http://en.wikipedia.org/wiki/1.96 and http://en.wikipedia.org/wiki/Standard_error_%28statistics%29
std_err = (1.96 * std_dev/Math.sqrt(n)).toFixed(2);
std_dev = std_dev.toFixed(2);
n = lat_filtered.length-1;
median = Math.round(
(lat_filtered[Math.floor(n/2)] + lat_filtered[Math.ceil(n/2)]) / 2
);
return { mean: amean, median: median, stddev: std_dev, stderr: std_err };
},
calc_bw: function()
{
var i, j, n=0,
r, bandwidths=[], bandwidths_corrected=[],
sum=0, sumsq=0, sum_corrected=0, sumsq_corrected=0,
amean, std_dev, std_err, median,
amean_corrected, std_dev_corrected, std_err_corrected, median_corrected,
nimgs, bw, bw_c;
for(i=0; i<this.nruns; i++) {
if(!this.results[i] || !this.results[i].r) {
continue;
}
r=this.results[i].r;
// the next loop we iterate through backwards and only consider the largest
// 3 images that succeeded that way we don't consider small images that
// downloaded fast without really saturating the network
nimgs=0;
for(j=r.length-1; j>=0 && nimgs<3; j--) {
// if we hit an undefined image time, we skipped everything before this
if(typeof r[j] === 'undefined') {
break;
}
if(r[j].t === null) {
continue;
}
n++;
nimgs++;
// multiply by 1000 since t is in milliseconds and not seconds
bw = images[j].size*1000/r[j].t;
bandwidths.push(bw);
bw_c = images[j].size*1000/(r[j].t - this.latency.mean);
bandwidths_corrected.push(bw_c);
}
}
BOOMR.debug('got ' + n + ' readings', "bw");
BOOMR.debug('bandwidths: ' + bandwidths, "bw");
BOOMR.debug('corrected: ' + bandwidths_corrected, "bw");
// First do IQR filtering since we use the median here
// and should use the stddev after filtering.
if(bandwidths.length > 3) {
bandwidths = this.iqr(bandwidths.sort(this.ncmp));
bandwidths_corrected = this.iqr(bandwidths_corrected.sort(this.ncmp));
} else {
bandwidths = bandwidths.sort(this.ncmp);
bandwidths_corrected = bandwidths_corrected.sort(this.ncmp);
}
BOOMR.debug('after iqr: ' + bandwidths, "bw");
BOOMR.debug('corrected: ' + bandwidths_corrected, "bw");
// Now get the mean & median.
// Also get corrected values that eliminate latency
n = Math.max(bandwidths.length, bandwidths_corrected.length);
for(i=0; i<n; i++) {
if(i<bandwidths.length) {
sum += bandwidths[i];
sumsq += Math.pow(bandwidths[i], 2);
}
if(i<bandwidths_corrected.length) {
sum_corrected += bandwidths_corrected[i];
sumsq_corrected += Math.pow(bandwidths_corrected[i], 2);
}
}
n = bandwidths.length;
amean = Math.round(sum/n);
std_dev = Math.sqrt(sumsq/n - Math.pow(sum/n, 2));
std_err = Math.round(1.96 * std_dev/Math.sqrt(n));
std_dev = Math.round(std_dev);
n = bandwidths.length-1;
median = Math.round(
(bandwidths[Math.floor(n/2)] + bandwidths[Math.ceil(n/2)]) / 2
);
n = bandwidths_corrected.length;
amean_corrected = Math.round(sum_corrected/n);
std_dev_corrected = Math.sqrt(sumsq_corrected/n - Math.pow(sum_corrected/n, 2));
std_err_corrected = (1.96 * std_dev_corrected/Math.sqrt(n)).toFixed(2);
std_dev_corrected = std_dev_corrected.toFixed(2);
n = bandwidths_corrected.length-1;
median_corrected = Math.round(
(
bandwidths_corrected[Math.floor(n/2)]
+ bandwidths_corrected[Math.ceil(n/2)]
) / 2
);
BOOMR.debug('amean: ' + amean + ', median: ' + median, "bw");
BOOMR.debug('corrected amean: ' + amean_corrected + ', '
+ 'median: ' + median_corrected, "bw");
return {
mean: amean,
stddev: std_dev,
stderr: std_err,
median: median,
mean_corrected: amean_corrected,
stddev_corrected: std_dev_corrected,
stderr_corrected: std_err_corrected,
median_corrected: median_corrected
};
},
defer: function(method)
{
var that=this;
return setTimeout(function() { method.call(that); that=null;}, 10);
},
load_img: function(i, run, callback)
{
var url = this.base_url + images[i].name
+ '?t=' + (new Date().getTime()) + Math.random(),
timer=0, tstart=0,
img = new Image(),