-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
234 lines (200 loc) · 5.33 KB
/
index.html
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
<!DOCTYPE html>
<title>Service Worker Performance Tests</title>
<template id="childScript">
<script>
window.onmessage = function(event) {
if (event.data.what == 'start') {
start(event.data);
}
}
var done;
var targetNumRequests;
var targetNumConcurrentRequests;
var numTimedRequestsSent;
var numTimedRequestsFinished;
var responseTimes;
function start(options) {
done = false;
targetNumRequests = options.numRequests || 400;
targetNumConcurrentRequests = options.numConcurrentRequests;
numTimedRequestsSent = 0;
numTimedRequestsFinished = 0;
responseTimes = [];
for (var i = 0; i < targetNumConcurrentRequests; i++) {
rampRequest();
}
timedRequest();
}
function sendXhr(xhr) {
xhr.timeout = 500;
xhr.open('GET', '/request-latency/x', true);
xhr.send();
}
// Used when ramping the load up and down.
function rampRequest() {
var xhr = new XMLHttpRequest();
xhr.addEventListener('loadend', next, false);
sendXhr(xhr);
}
function timedRequest() {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', timedRequestCompleted, false);
xhr.addEventListener('error', timedRequestFailed, false);
xhr.addEventListener('abort', timedRequestFailed, false);
xhr.startTime = performance.now();
sendXhr(xhr);
numTimedRequestsSent++;
}
function timedRequestCompleted(event) {
var endTime = performance.now();
responseTimes.push(endTime - event.target.startTime);
numTimedRequestsFinished++;
next();
}
function timedRequestFailed(event) {
numTimedRequestsFinished++;
next();
}
function next() {
// Ramp-down requests finishing.
if (done) {
return;
}
// All timed requests have finished.
if (numTimedRequestsFinished == targetNumRequests) {
done = true;
computeResults();
return;
}
// All timed requests have started, ramping down.
if (numTimedRequestsSent == targetNumRequests) {
rampRequest();
return;
}
// Still generating timed requests.
timedRequest();
}
function computeResults() {
var results = {};
results['concurrent_' + targetNumConcurrentRequests + '_failed_requests'] = {
value: numTimedRequestsSent - responseTimes.length,
units: 'requests'
};
responseTimes.sort();
reportPercentile(50);
reportPercentile(90);
reportPercentile(99);
complete(results);
// Gets the n-th percentile response time, or ten seconds if that response did
// not succeed.
function percentile(n) {
var i = Math.floor(n * numTimedRequestsSent / 100);
if (i >= responseTimes.length) {
return 10 * 1000;
}
return responseTimes[i];
}
function reportPercentile(n) {
results['concurrent_' + targetNumConcurrentRequests + '_response_' + n + '_percentile'] = {
value: percentile(n),
units: 'ms'
};
}
}
function complete(results) {
var message = {
what: 'done',
results: results
};
window.parent.postMessage(message, '*');
}
</script>
</template>
<body>
<script>
function waitUntilActive(registration) {
return new Promise(function(resolve, reject) {
var serviceWorker;
if (registration.active) {
resolve(registration.active);
} else if (registration.waiting) {
serviceWorker = registration.waiting;
serviceWorker.onstatechange = checkState;
} else {
serviceWorker = registration.installing;
serviceWorker.onstatechange = checkState;
}
function checkState() {
if (serviceWorker.state == 'activated') {
serviceWorker.removeEventListener('statechange', checkState);
resolve(serviceWorker);
}
}
});
}
function setUpFrame(path) {
return function() {
return new Promise(function(resolve, _) {
var frame = document.createElement('iframe');
frame.onload = function() {
frame.contentDocument.body.appendChild(
frame.contentDocument.importNode(
document.querySelector('#childScript').content, true));
resolve(frame);
};
frame.src = path;
document.body.appendChild(frame);
});
};
}
function testRequestLatency(numConcurrentRequests) {
return function(frame) {
return new Promise(function(resolve, _) {
window.addEventListener('message', message);
function message(event) {
if (event.data.what == 'done') {
window.removeEventListener('message', message);
addResults(event.data.results);
resolve(frame);
}
}
var message = {
what: 'start',
numConcurrentRequests: numConcurrentRequests
};
frame.contentWindow.postMessage(message, '*');
});
};
}
function addResults(partial) {
for (key in partial) {
if (partial.hasOwnProperty(key)) {
results[key] = partial[key];
}
}
}
function cleanUp(frame) {
frame.remove();
return navigator.serviceWorker.getRegistration(workerOptions['scope'])
.then(function(r) {
return r.unregister();
});
}
function complete() {
console.log('complete');
console.log(results);
done = true;
}
done = false;
results = { };
var workerScriptPath = 'request-latency-worker.js';
var workerOptions = { scope: '/request-latency/' };
navigator.serviceWorker.register(workerScriptPath, workerOptions)
.then(waitUntilActive)
.then(setUpFrame('/request-latency/blank'))
.then(testRequestLatency(1))
.then(testRequestLatency(10))
.then(testRequestLatency(100))
.then(cleanUp)
.then(complete);
</script>