gecko-dev/testing/web-platform/tests/tools/pywebsocket/example/util.js
Geoffrey Sneddon 8b783a7e69 Bug 1439967 [wpt PR 9606] - Update pywebsockets, a=testonly
Automatic update from web-platform-testsMerge pull request #9606 from gsnedders/update_pywebsockets

Update pywebsockets

wpt-commits: 38811892842f1db4a029ee0d4d42764c5e12ae6d
wpt-pr: 9606
wpt-commits: 38811892842f1db4a029ee0d4d42764c5e12ae6d
wpt-pr: 9606


--HG--
rename : testing/web-platform/tests/tools/pywebsocket/src/COPYING => testing/web-platform/tests/tools/pywebsocket/LICENSE
rename : testing/web-platform/tests/tools/pywebsocket/src/MANIFEST.in => testing/web-platform/tests/tools/pywebsocket/MANIFEST.in
rename : testing/web-platform/tests/tools/pywebsocket/src/README => testing/web-platform/tests/tools/pywebsocket/README
rename : testing/web-platform/tests/tools/pywebsocket/src/example/abort_handshake_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/abort_handshake_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/abort_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/abort_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/arraybuffer_benchmark.html => testing/web-platform/tests/tools/pywebsocket/example/arraybuffer_benchmark.html
rename : testing/web-platform/tests/tools/pywebsocket/src/example/bench_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/bench_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/benchmark_helper_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/benchmark_helper_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/close_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/close_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/console.html => testing/web-platform/tests/tools/pywebsocket/example/console.html
rename : testing/web-platform/tests/tools/pywebsocket/src/example/cookie_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/cookie_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/echo_client.py => testing/web-platform/tests/tools/pywebsocket/example/echo_client.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/echo_noext_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/echo_noext_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/echo_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/echo_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/eventsource.cgi => testing/web-platform/tests/tools/pywebsocket/example/eventsource.cgi
rename : testing/web-platform/tests/tools/pywebsocket/src/example/eventsource.html => testing/web-platform/tests/tools/pywebsocket/example/eventsource.html
rename : testing/web-platform/tests/tools/pywebsocket/src/example/handler_map.txt => testing/web-platform/tests/tools/pywebsocket/example/handler_map.txt
rename : testing/web-platform/tests/tools/pywebsocket/src/example/hsts_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/hsts_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/internal_error_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/internal_error_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/origin_check_wsh.py => testing/web-platform/tests/tools/pywebsocket/example/origin_check_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/example/pywebsocket.conf => testing/web-platform/tests/tools/pywebsocket/example/pywebsocket.conf
rename : testing/web-platform/tests/tools/pywebsocket/src/example/special_headers.cgi => testing/web-platform/tests/tools/pywebsocket/example/special_headers.cgi
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/__init__.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/__init__.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/_stream_base.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/_stream_base.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/_stream_hixie75.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/_stream_hixie75.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/dispatch.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/dispatch.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/fast_masking.i => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/fast_masking.i
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/handshake/__init__.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/handshake/__init__.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/handshake/_base.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/handshake/_base.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/handshake/hybi.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/handshake/hybi.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/handshake/hybi00.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/handshake/hybi00.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/http_header_util.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/http_header_util.py
rename : testing/web-platform/tests/tools/pywebsocket/src/mod_pywebsocket/msgutil.py => testing/web-platform/tests/tools/pywebsocket/mod_pywebsocket/msgutil.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/cert/cacert.pem => testing/web-platform/tests/tools/pywebsocket/test/cert/cacert.pem
rename : testing/web-platform/tests/tools/pywebsocket/src/test/cert/cert.pem => testing/web-platform/tests/tools/pywebsocket/test/cert/cert.pem
rename : testing/web-platform/tests/tools/pywebsocket/src/test/cert/client_cert.p12 => testing/web-platform/tests/tools/pywebsocket/test/cert/client_cert.p12
rename : testing/web-platform/tests/tools/pywebsocket/src/test/cert/key.pem => testing/web-platform/tests/tools/pywebsocket/test/cert/key.pem
rename : testing/web-platform/tests/tools/pywebsocket/src/test/client_for_testing.py => testing/web-platform/tests/tools/pywebsocket/test/client_for_testing.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/endtoend_with_external_server.py => testing/web-platform/tests/tools/pywebsocket/test/endtoend_with_external_server.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/mock.py => testing/web-platform/tests/tools/pywebsocket/test/mock.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/mux_client_for_testing.py => testing/web-platform/tests/tools/pywebsocket/test/mux_client_for_testing.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/run_all.py => testing/web-platform/tests/tools/pywebsocket/test/run_all.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/set_sys_path.py => testing/web-platform/tests/tools/pywebsocket/test/set_sys_path.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_dispatch.py => testing/web-platform/tests/tools/pywebsocket/test/test_dispatch.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_endtoend.py => testing/web-platform/tests/tools/pywebsocket/test/test_endtoend.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_handshake.py => testing/web-platform/tests/tools/pywebsocket/test/test_handshake.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_handshake_hybi00.py => testing/web-platform/tests/tools/pywebsocket/test/test_handshake_hybi00.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_http_header_util.py => testing/web-platform/tests/tools/pywebsocket/test/test_http_header_util.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_memorizingfile.py => testing/web-platform/tests/tools/pywebsocket/test/test_memorizingfile.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_mock.py => testing/web-platform/tests/tools/pywebsocket/test/test_mock.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_stream.py => testing/web-platform/tests/tools/pywebsocket/test/test_stream.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_stream_hixie75.py => testing/web-platform/tests/tools/pywebsocket/test/test_stream_hixie75.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/test_util.py => testing/web-platform/tests/tools/pywebsocket/test/test_util.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/README => testing/web-platform/tests/tools/pywebsocket/test/testdata/README
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/abort_by_user_wsh.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/abort_by_user_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/blank_wsh.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/blank_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/origin_check_wsh.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/origin_check_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/sub/exception_in_transfer_wsh.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/sub/exception_in_transfer_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/sub/no_wsh_at_the_end.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/sub/no_wsh_at_the_end.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/sub/non_callable_wsh.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/sub/non_callable_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/sub/plain_wsh.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/sub/plain_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/sub/wrong_handshake_sig_wsh.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/sub/wrong_handshake_sig_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/handlers/sub/wrong_transfer_sig_wsh.py => testing/web-platform/tests/tools/pywebsocket/test/testdata/handlers/sub/wrong_transfer_sig_wsh.py
rename : testing/web-platform/tests/tools/pywebsocket/src/test/testdata/hello.pl => testing/web-platform/tests/tools/pywebsocket/test/testdata/hello.pl
2018-03-31 22:24:31 +01:00

327 lines
9.4 KiB
JavaScript

// Copyright 2013, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Utilities for example applications (for both main and worker thread).
var results = {};
function getTimeStamp() {
return Date.now();
}
function formatResultInKiB(size, timePerMessageInMs, stddevTimePerMessageInMs,
speed, printSize) {
if (printSize) {
return (size / 1024) +
'\t' + timePerMessageInMs.toFixed(3) +
(stddevTimePerMessageInMs == -1 ?
'' :
'\t' + stddevTimePerMessageInMs.toFixed(3)) +
'\t' + speed.toFixed(3);
} else {
return speed.toString();
}
}
function clearAverageData() {
results = {};
}
function reportAverageData(config) {
config.addToSummary(
'Size[KiB]\tAverage time[ms]\tStddev time[ms]\tSpeed[KB/s]');
for (var size in results) {
var averageTimePerMessageInMs = results[size].sum_t / results[size].n;
var speed = calculateSpeedInKB(size, averageTimePerMessageInMs);
// Calculate sample standard deviation
var stddevTimePerMessageInMs = Math.sqrt(
(results[size].sum_t2 / results[size].n -
averageTimePerMessageInMs * averageTimePerMessageInMs) *
results[size].n /
(results[size].n - 1));
config.addToSummary(formatResultInKiB(
size, averageTimePerMessageInMs, stddevTimePerMessageInMs, speed,
true));
}
}
function calculateSpeedInKB(size, timeSpentInMs) {
return Math.round(size / timeSpentInMs * 1000) / 1000;
}
function calculateAndLogResult(config, size, startTimeInMs, totalSize,
isWarmUp) {
var timeSpentInMs = getTimeStamp() - startTimeInMs;
var speed = calculateSpeedInKB(totalSize, timeSpentInMs);
var timePerMessageInMs = timeSpentInMs / (totalSize / size);
if (!isWarmUp) {
config.measureValue(timePerMessageInMs);
if (!results[size]) {
results[size] = {n: 0, sum_t: 0, sum_t2: 0};
}
results[size].n ++;
results[size].sum_t += timePerMessageInMs;
results[size].sum_t2 += timePerMessageInMs * timePerMessageInMs;
}
config.addToLog(formatResultInKiB(size, timePerMessageInMs, -1, speed,
config.printSize));
}
function repeatString(str, count) {
var data = '';
var expChunk = str;
var remain = count;
while (true) {
if (remain % 2) {
data += expChunk;
remain = (remain - 1) / 2;
} else {
remain /= 2;
}
if (remain == 0)
break;
expChunk = expChunk + expChunk;
}
return data;
}
function fillArrayBuffer(buffer, c) {
var i;
var u32Content = c * 0x01010101;
var u32Blocks = Math.floor(buffer.byteLength / 4);
var u32View = new Uint32Array(buffer, 0, u32Blocks);
// length attribute is slow on Chrome. Don't use it for loop condition.
for (i = 0; i < u32Blocks; ++i) {
u32View[i] = u32Content;
}
// Fraction
var u8Blocks = buffer.byteLength - u32Blocks * 4;
var u8View = new Uint8Array(buffer, u32Blocks * 4, u8Blocks);
for (i = 0; i < u8Blocks; ++i) {
u8View[i] = c;
}
}
function verifyArrayBuffer(buffer, expectedChar) {
var i;
var expectedU32Value = expectedChar * 0x01010101;
var u32Blocks = Math.floor(buffer.byteLength / 4);
var u32View = new Uint32Array(buffer, 0, u32Blocks);
for (i = 0; i < u32Blocks; ++i) {
if (u32View[i] != expectedU32Value) {
return false;
}
}
var u8Blocks = buffer.byteLength - u32Blocks * 4;
var u8View = new Uint8Array(buffer, u32Blocks * 4, u8Blocks);
for (i = 0; i < u8Blocks; ++i) {
if (u8View[i] != expectedChar) {
return false;
}
}
return true;
}
function verifyBlob(config, blob, expectedChar, doneCallback) {
var reader = new FileReader(blob);
reader.onerror = function() {
config.addToLog('FileReader Error: ' + reader.error.message);
doneCallback(blob.size, false);
}
reader.onloadend = function() {
var result = verifyArrayBuffer(reader.result, expectedChar);
doneCallback(blob.size, result);
}
reader.readAsArrayBuffer(blob);
}
function verifyAcknowledgement(config, message, size) {
if (typeof message != 'string') {
config.addToLog('Invalid ack type: ' + typeof message);
return false;
}
var parsedAck = parseInt(message);
if (isNaN(parsedAck)) {
config.addToLog('Invalid ack value: ' + message);
return false;
}
if (parsedAck != size) {
config.addToLog(
'Expected ack for ' + size + 'B but received one for ' + parsedAck +
'B');
return false;
}
return true;
}
function cloneConfig(obj) {
var newObj = {};
for (key in obj) {
newObj[key] = obj[key];
}
return newObj;
}
var tasks = [];
function runNextTask(config) {
var task = tasks.shift();
if (task == undefined) {
config.addToLog('Finished');
cleanup();
return;
}
timerID = setTimeout(task, 0);
}
function buildLegendString(config) {
var legend = ''
if (config.printSize)
legend = 'Message size in KiB, Time/message in ms, ';
legend += 'Speed in kB/s';
return legend;
}
function addTasks(config, stepFunc) {
for (var i = 0;
i < config.numWarmUpIterations + config.numIterations; ++i) {
var multiplierIndex = 0;
for (var size = config.startSize;
size <= config.stopThreshold;
++multiplierIndex) {
var task = stepFunc.bind(
null,
size,
config,
i < config.numWarmUpIterations);
tasks.push(task);
var multiplier = config.multipliers[
multiplierIndex % config.multipliers.length];
if (multiplier <= 1) {
config.addToLog('Invalid multiplier ' + multiplier);
config.notifyAbort();
throw new Error('Invalid multipler');
}
size = Math.ceil(size * multiplier);
}
}
}
function addResultReportingTask(config, title) {
tasks.push(function(){
timerID = null;
config.addToSummary(title);
reportAverageData(config);
clearAverageData();
runNextTask(config);
});
}
function sendBenchmark(config) {
config.addToLog('Send benchmark');
config.addToLog(buildLegendString(config));
tasks = [];
clearAverageData();
addTasks(config, sendBenchmarkStep);
addResultReportingTask(config, 'Send Benchmark ' + getConfigString(config));
startBenchmark(config);
}
function receiveBenchmark(config) {
config.addToLog('Receive benchmark');
config.addToLog(buildLegendString(config));
tasks = [];
clearAverageData();
addTasks(config, receiveBenchmarkStep);
addResultReportingTask(config,
'Receive Benchmark ' + getConfigString(config));
startBenchmark(config);
}
function stop(config) {
clearTimeout(timerID);
timerID = null;
tasks = [];
config.addToLog('Stopped');
cleanup();
}
var worker;
function initWorker(connectionType, origin) {
var scriptPath =
connectionType === 'WebSocket' ? '/benchmark.js' :
connectionType === 'XHR' ? '/xhr_benchmark.js' :
'/fetch_benchmark.js'; // connectionType === 'fetch'
worker = new Worker(origin + scriptPath);
}
function doAction(config, isWindowToWorker, action) {
if (isWindowToWorker) {
worker.onmessage = function(addToLog, addToSummary,
measureValue, notifyAbort, message) {
if (message.data.type === 'addToLog')
addToLog(message.data.data);
else if (message.data.type === 'addToSummary')
addToSummary(message.data.data);
else if (message.data.type === 'measureValue')
measureValue(message.data.data);
else if (message.data.type === 'notifyAbort')
notifyAbort();
}.bind(undefined, config.addToLog, config.addToSummary,
config.measureValue, config.notifyAbort);
config.addToLog = undefined;
config.addToSummary = undefined;
config.measureValue = undefined;
config.notifyAbort = undefined;
worker.postMessage({type: action, config: config});
} else {
if (action === 'sendBenchmark')
sendBenchmark(config);
else if (action === 'receiveBenchmark')
receiveBenchmark(config);
else if (action === 'batchBenchmark')
batchBenchmark(config);
else if (action === 'stop')
stop(config);
}
}