-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsexp2.js
executable file
·198 lines (167 loc) · 7.42 KB
/
dnsexp2.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
///<reference path="..\..\..\..\Shared\Src\Content\Script\Declarations\Shared.d.ts" />
(function () {
var localHost = "204.79.197.200"; //FIX ME! window.location.hostname;
var localProtocol = window.location.protocol + "//";
var monitorID = "AZR"
var sharedIG = guid(); //this guid is shared across CLO, NRB, and ANY monitorIDs
var IGs = [sharedIG, sharedIG, guid(), guid(), sharedIG];
// URLs to test
var test1 = localProtocol + IGs[0] + ".clo.footprintdns.com/apc/";
var test2 = localProtocol + IGs[1] + ".nrb.footprintdns.com/apc/";
var test3 = localProtocol + IGs[2] + "." + monitorID + ".msnetworkanalytics.testanalytics.net/ap/";
var test4 = localProtocol + IGs[3] + "." + monitorID + ".msnetworkanalytics.testanalytics.net/ap/";
var test5 = localProtocol + IGs[4] + ".any.footprintdns.com/apc/";
var warmupImg = "trans.gif";
var testImg = "17k.gif";
var requestTimeout = 2000;
var requestDelay = 2000;
var reportHostMsr = "report.footprintdns.com/trans.gif?"
var reportParams = "&prot=" + window.location.protocol + "&MonitorID=" + monitorID + "&DATA=";
var reportUrls = [
localProtocol + reportHostMsr + reportParams,
];
var warmupUrls = [
test1 + warmupImg,
test2 + warmupImg,
test3 + warmupImg,
test4 + warmupImg
];
var testUrls = [
test1 + testImg,
test2 + testImg,
test3 + warmupImg + "?" + IGs[2], //use warmup image for Azure
test4 + warmupImg + "?" + IGs[3], //and here
test5 + testImg + "?" + IGs[4] //disable any caching
];
// Maps a URL in testUrls to a measurement ID by index
var urlIds = [{"name":"CLO", "guid": IGs[0]},
{"name":"NRB", "guid": IGs[1]},
{"name": monitorID, "guid": IGs[2]},
{"name": monitorID, "guid": IGs[3]},
{"name": "ANY", "guid": IGs[4]}];
var runTest = function () {
// Don't delay executing this. Don't need to pass in onComplete function.
flight(testUrls, requestTimeout, 0, reportUrls, urlIds, null);
};
// Run the DNS warmup URLs and then run test URLs.
// This is just to warm up DNS so no need for a reportUrl or url2id function
flight(warmupUrls, requestTimeout, requestDelay, null, null, runTest);
/**
* Creates a JSON measurement report.
*/
function createReport(loadTimes, urlIds) {
var results = [];
for (var i = 0; i < urlIds.length; i++) {
var urlId = urlIds[i];
results.push({'MonitorID': urlIds[i].name, 'RequestID':urlIds[i].guid, 'Result':Math.round(loadTimes[i])});
}
return JSON.stringify(results);
}
/**
* Runs a measurement test for a user provided list of URLs and sends a JSON report
* back to a report server.
*/
function flight(urlsToTest, reqTimeout, reqDelay, reportUrls, urlIds, onComplete) {
var startTime;
var timeoutId;
var loadTime = [];
var indexOfFirstProbe = Math.floor(Math.random() * urlsToTest.length);
var nextIndex = 0;
var numberProbesSent = 0;
var target;
function doProbe(nextIndex) {
if (timeoutId != null) {
sb_ct(timeoutId);
}
if (startTime != null) {
loadTime[nextIndex] = new Date().getTime() - startTime;
} else {
// Mark as timeout. If this is bootstrap call then this gets overwritten.
loadTime[nextIndex] = -1;
}
nextIndex = (numberProbesSent + indexOfFirstProbe) % urlsToTest.length;
target = new Image;
if (numberProbesSent++ < urlsToTest.length) {
// Start the next measurement probe
startTime = new Date().getTime();
target.onload = function () {
doProbe(nextIndex);
};
/*
On error and on timeout:
1. Set target.onload to null to avoid processing image if it loads
while processing timeout callback.
2. Set target.src to empty so that the browser stops the request.
3. Set startTime to null so that loadTime entry isn't populated.
*/
var errorHandler = function () {
target.onload = null;
target.onerror = null;
target.src = "";
startTime = null;
doProbe(nextIndex);
};
//Config to give up on the load after reqTimeout ms.
timeoutId = sb_st(function () {
errorHandler();
}, reqTimeout);
target.onerror = function () {
//clear timeout callback
if (timeoutId != null) {
sb_ct(timeoutId);
}
timeoutId = null;
errorHandler();
};
target.src = urlsToTest[nextIndex];
} else {
if (reportUrls != null && reportUrls.length !== 0) {
/*
Generate a report.
First look for W3C resource timings and then fall back to less
accurate timing if W3C is not supported in this browser.
Second check because Firefox doesn't support resource timing yet.
*/
if (window.performance && window.performance.getEntriesByName) {
for (var i = 0; i < urlsToTest.length; i++) {
var testUrl = urlsToTest[i];
var perfEntryArray = window.performance.getEntriesByName(testUrl);
if (perfEntryArray && perfEntryArray[0]) {
loadTime[i] = perfEntryArray[0].duration;
//console.log(testUrl);
//console.log(perfEntryArray);
}
}
}
//var pe = window.performance.getEntriesByType("resource");
//for (var i = 0; i < pe.length; i++){
// console.log("Name:" + pe[i].name + " Duration:" + pe[i].duration);
//}
// Create JSON data report
var reportStr = createReport(loadTime, urlIds);
for (var i = 0; i < reportUrls.length; i++) {
var reportUrl = reportUrls[i];
var reportImg = new Image;
reportImg.src = reportUrl + reportStr;
}
}
// Execute onComplete function now that probes are finished
if (onComplete != null) {
onComplete();
}
}
}
// wait for reqDelay(ms) to prevent confusing page load events of the parent page
// or delaying things the page itself wants to do when it finishes loading
sb_st(function () {
doProbe(0);
}, reqDelay);
};
function guid() {
//RFC4122 version 4 IDs
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4();
}
})();