This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 448
/
ipv6.js
172 lines (139 loc) · 4.28 KB
/
ipv6.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
/*
* Copyright (c) 2011, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the BSD License. See the accompanying LICENSE.txt file for terms.
*/
/**
\file ipv6.js
Plugin to measure various ipv6 related metrics.
This plugin tries to do a few things:
- Check if the client can connect to an ipv6 address
- Check if the client can resolve DNS that points to an ipv6 address
- Check latency of connecting to an ipv6 address
- Check avg latency of doing dns lookup to an ipv6 address (not worstcase)
You'll need a server that has an ipv6 address, and a DNS name to point to it.
Additionally, this server needs to be configured to serve content requested
from the IPv6 address and should not require a virtual host name. This means
that you probably cannot use shared hosting that puts multiple hosts on the
same IP address.
All beacon parameters are prefixed with ipv6_
Beacon parameters:
- ipv6_latency: Latency in milliseconds of getting data from an ipv6 host when
connecting to the IP. Set to NA if the client cannot connect
to the ipv6 host.
- ipv6_lookup: Latency of getting data from a hostname that resolves to an
ipv6 address. Set to NA if the client cannot resolve or connect
to the ipv6 host.
*/
(function(w) {
BOOMR = BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
/*
Algorithm:
1. Try to load a sizeless image from an IPv6 host
- onerror, flag no IPv6 connect support and end
- onload, measure load time
2. Try to load a sizeless image from a hostname that resolves to an IPv6 address
- onerror, flag no IPv6 DNS resolver and end
- onload, measure load time
*/
var impl = {
complete: false,
ipv6_url: "",
host_url: "",
timeout: 1200,
timers: {
ipv6: { start: null, end: null },
host: { start: null, end: null }
},
start: function() {
this.load_img('ipv6', 'host');
},
load_img: function() {
var img,
rnd = "?t=" + (new Date().getTime()) + Math.random(),
timer=0, error = null,
that = this,
which = Array.prototype.shift.call(arguments),
a = arguments;
// Terminate if we've reached end of test list
if(!which || !(which in this.timers)) {
this.done();
return false;
}
// Skip if URL wasn't set for this test
if(!this[which + '_url']) {
return this.load_img.apply(this, a);
}
img = new Image();
img.onload = function() {
that.timers[which].end = new Date().getTime();
clearTimeout(timer);
img.onload = img.onerror = null;
img = null;
that.load_img.apply(that, a);
that = a = null;
};
error = function() {
that.timers[which].supported = false;
clearTimeout(timer);
img.onload = img.onerror = null;
img = null;
that.done();
that = a = null;
};
img.onerror = error;
timer = setTimeout(error, this.timeout);
this.timers[which].start = new Date().getTime();
img.src = this[which + '_url'] + rnd;
return true;
},
done: function() {
if(this.complete) {
return;
}
BOOMR.removeVar('ipv6_latency', 'ipv6_lookup');
if(this.timers.ipv6.end !== null) {
BOOMR.addVar('ipv6_latency', this.timers.ipv6.end - this.timers.ipv6.start);
}
else {
BOOMR.addVar('ipv6_latency', 'NA');
}
if(this.timers.host.end !== null) {
BOOMR.addVar('ipv6_lookup', this.timers.host.end - this.timers.host.start);
}
else {
BOOMR.addVar('ipv6_lookup', 'NA');
}
this.complete = true;
BOOMR.sendBeacon();
}
};
BOOMR.plugins.IPv6 = {
init: function(config) {
BOOMR.utils.pluginConfig(impl, config, "IPv6", ["ipv6_url", "host_url", "timeout"]);
if(!impl.ipv6_url) {
BOOMR.warn("IPv6.ipv6_url is not set. Cannot run IPv6 test.", "ipv6");
impl.complete = true; // set to true so that is_complete doesn't
// block other plugins
return this;
}
if(!impl.host_url) {
BOOMR.warn("IPv6.host_url is not set. Will skip hostname test.", "ipv6");
}
// make sure that test images use the same protocol as the host page
if(w.location.protocol === 'https:') {
impl.ipv6_url = impl.ipv6_url.replace(/^http:/, 'https:');
impl.host_url = impl.host_url.replace(/^http:/, 'https:');
}
else {
impl.ipv6_url = impl.ipv6_url.replace(/^https:/, 'http:');
impl.host_url = impl.host_url.replace(/^https:/, 'http:');
}
BOOMR.subscribe("page_ready", impl.start, null, impl);
return this;
},
is_complete: function() {
return impl.complete;
}
};
}(window));