forked from YahooArchive/boomerang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.js
119 lines (91 loc) · 2.65 KB
/
dns.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
/**
\file dns.js
Plugin to measure DNS latency.
This code is based on Carlos Bueno's guide to DNS on the YDN blog:
http://developer.yahoo.net/blog/archives/2009/11/guide_to_dns.html
*/
// w is the window object
(function(w) {
var d=w.document;
BOOMR = BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
var impl = {
complete: false,
base_url: "",
t_start: null,
t_dns: null,
t_http: null,
img: null,
start: function() {
var random = Math.floor(Math.random()*(2147483647)).toString(36),
base_url = this.base_url.replace(/\*/, random);
impl.img = new Image();
impl.img.onload = impl.A_loaded;
impl.t_start = new Date().getTime();
impl.img.src = base_url + "image-l.gif?t="
+ (new Date().getTime()) + Math.random();
},
A_loaded: function() {
impl.t_dns = new Date().getTime() - impl.t_start;
impl.img = new Image();
impl.img.onload = impl.B_loaded;
impl.t_start = new Date().getTime();
impl.img.src = base_url + "image-l.gif?t="
+ (new Date().getTime()) + Math.random();
},
B_loaded: function() {
impl.t_http = new Date().getTime() - impl.t_start;
impl.img = null;
impl.done();
},
done: function() {
// DNS time is the time to load the image with uncached DNS
// minus the time to load the image with cached DNS
var dns = impl.t_dns - impl.t_http;
BOOMR.addVar("dns", dns);
this.complete = true;
BOOMR.sendBeacon();
},
read_timing_api: function(t) {
if(typeof t.domainLookupStart === "undefined"
|| typeof t.domainLookupEnd === "undefined") {
return false;
}
// This will be 0 if we read DNS from cache, but that's what
// we want because it's what the user experiences
BOOMR.addVar("dns", t.domainLookupEnd - t.domainLookupStart);
impl.complete = true;
return true;
}
};
BOOMR.plugins.DNS = {
init: function(config) {
BOOMR.utils.pluginConfig(impl, config, "DNS", ["base_url"]);
// If this browser supports the WebTiming API, then we just
// use that and don't bother doing the test
if(w.performance && w.performance.timing) {
if(impl.read_timing_api(w.performance.timing)) {
return this;
}
}
if(!impl.base_url) {
BOOMR.warn("DNS.base_url is not set. Cannot run DNS test.", "dns");
impl.complete = true; // set to true so that is_complete doesn't
// block other plugins
return this;
}
// make sure that dns test images use the same protocol as the host page
if(w.location.protocol === 'https:') {
impl.base_url.replace(/^http:/, 'https:');
}
else {
impl.base_url.replace(/^https:/, 'http:');
}
BOOMR.subscribe("page_ready", impl.start, null, this);
return this;
},
is_complete: function() {
return impl.complete;
}
};
}(window));