Use direct javascript in test files

Avoid relying on our own modules as we are about to split things up.
This commit is contained in:
Pierre Ossman 2019-07-03 15:55:46 +02:00
parent b46fab5608
commit 60acf3cd3c
5 changed files with 124 additions and 158 deletions

View File

@ -2,16 +2,6 @@
<head> <head>
<title>WebSockets Echo Test</title> <title>WebSockets Echo Test</title>
<script src="include/util.js"></script>
<script src="include/webutil.js"></script>
<script src="include/websock.js"></script>
<!-- Uncomment to activate firebug lite -->
<!--
<script type='text/javascript'
src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
-->
</head> </head>
<body> <body>
@ -36,7 +26,7 @@
function message(str) { function message(str) {
console.log(str); console.log(str);
cell = $D('messages'); cell = document.getElementById('messages');
cell.innerHTML += msg_cnt + ": " + str + "\n"; cell.innerHTML += msg_cnt + ": " + str + "\n";
cell.scrollTop = cell.scrollHeight; cell.scrollTop = cell.scrollHeight;
msg_cnt++; msg_cnt++;
@ -51,20 +41,21 @@
function send_msg() { function send_msg() {
var str = "Message #" + send_cnt; var str = "Message #" + send_cnt;
ws.send_string(str); var encoder = new TextEncoder();
ws.send(encoder.encode(str));
message("Sent message: '" + str + "'"); message("Sent message: '" + str + "'");
send_cnt++; send_cnt++;
} }
function update_stats() { function update_stats() {
$D('sent').innerHTML = sent; document.getElementById('sent').innerHTML = sent;
$D('received').innerHTML = received; document.getElementById('received').innerHTML = received;
$D('errors').innerHTML = errors; document.getElementById('errors').innerHTML = errors;
} }
function connect() { function connect() {
var host = $D('host').value, var host = document.getElementById('host').value,
port = $D('port').value, port = document.getElementById('port').value,
scheme = "ws://", uri; scheme = "ws://", uri;
console.log(">> connect"); console.log(">> connect");
@ -77,28 +68,29 @@
ws.close(); ws.close();
} }
if ($D('encrypt').checked) { if (document.getElementById('encrypt').checked) {
scheme = "wss://"; scheme = "wss://";
} }
uri = scheme + host + ":" + port; uri = scheme + host + ":" + port;
message("connecting to " + uri); message("connecting to " + uri);
ws = new Websock(); ws = new WebSocket(uri);
ws.open(uri); ws.binaryType = 'arraybuffer';
ws.on('message', function(e) { ws.addEventListener('message', function(e) {
//console.log(">> WebSockets.onmessage"); //console.log(">> WebSockets.onmessage");
var str = ws.rQshiftStr(); var decoder = new TextDecoder('UTF-8');
var str = decoder.decode(e.data);
message("Received message '" + str + "'"); message("Received message '" + str + "'");
//console.log("<< WebSockets.onmessage"); //console.log("<< WebSockets.onmessage");
}); });
ws.on('open', function(e) { ws.addEventListener('open', function(e) {
console.log(">> WebSockets.onopen"); console.log(">> WebSockets.onopen");
echo_ref = setInterval(send_msg, echoDelay); echo_ref = setInterval(send_msg, echoDelay);
console.log("<< WebSockets.onopen"); console.log("<< WebSockets.onopen");
}); });
ws.on('close', function(e) { ws.addEventListener('close', function(e) {
console.log(">> WebSockets.onclose"); console.log(">> WebSockets.onclose");
if (echo_ref) { if (echo_ref) {
clearInterval(echo_ref); clearInterval(echo_ref);
@ -106,7 +98,7 @@
} }
console.log("<< WebSockets.onclose"); console.log("<< WebSockets.onclose");
}); });
ws.on('error', function(e) { ws.addEventListener('error', function(e) {
console.log(">> WebSockets.onerror"); console.log(">> WebSockets.onerror");
if (echo_ref) { if (echo_ref) {
clearInterval(echo_ref); clearInterval(echo_ref);
@ -115,8 +107,8 @@
console.log("<< WebSockets.onerror"); console.log("<< WebSockets.onerror");
}); });
$D('connectButton').value = "Stop"; document.getElementById('connectButton').value = "Stop";
$D('connectButton').onclick = disconnect; document.getElementById('connectButton').onclick = disconnect;
console.log("<< connect"); console.log("<< connect");
} }
@ -130,8 +122,8 @@
clearInterval(echo_ref); clearInterval(echo_ref);
} }
$D('connectButton').value = "Start"; document.getElementById('connectButton').value = "Start";
$D('connectButton').onclick = connect; document.getElementById('connectButton').onclick = connect;
console.log("<< disconnect"); console.log("<< disconnect");
} }
@ -139,8 +131,8 @@
window.onload = function() { window.onload = function() {
console.log("onload"); console.log("onload");
var url = document.location.href; var url = document.location.href;
$D('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1]; document.getElementById('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
$D('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1]; document.getElementById('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
} }
</script> </script>

View File

@ -2,16 +2,6 @@
<head> <head>
<title>WebSockets Latency Test</title> <title>WebSockets Latency Test</title>
<script src="include/util.js"></script>
<script src="include/webutil.js"></script>
<script src="include/websock.js"></script>
<!-- Uncomment to activate firebug lite -->
<!--
<script type='text/javascript'
src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
-->
</head> </head>
<body> <body>
@ -73,7 +63,7 @@
function message(str) { function message(str) {
console.log(str); console.log(str);
cell = $D('messages'); cell = document.getElementById('messages');
msg_cnt++; msg_cnt++;
cell.innerHTML += msg_cnt + ": " + str + "\n"; cell.innerHTML += msg_cnt + ": " + str + "\n";
cell.scrollTop = cell.scrollHeight; cell.scrollTop = cell.scrollHeight;
@ -90,7 +80,7 @@
now = (new Date()).getTime(); // Early as possible now = (new Date()).getTime(); // Early as possible
arr = ws.rQshiftBytes(ws.rQlen()); arr = new Uint8Array(data);
first = String.fromCharCode(arr[0]); first = String.fromCharCode(arr[0]);
last = String.fromCharCode(arr[arr.length-1]); last = String.fromCharCode(arr[arr.length-1]);
@ -156,7 +146,7 @@
timestamp = (new Date()).getTime(); timestamp = (new Date()).getTime();
arr.pushStr("^" + send_seq + ":" + timestamp + ":" + payload + "$"); arr.pushStr("^" + send_seq + ":" + timestamp + ":" + payload + "$");
send_seq ++; send_seq ++;
ws.send(arr); ws.send(new Uint8Array(arr));
sent++; sent++;
showStats(); showStats();
@ -164,36 +154,35 @@
} }
function showStats() { function showStats() {
$D('sent').innerHTML = sent; document.getElementById('sent').innerHTML = sent;
$D('received').innerHTML = received; document.getElementById('received').innerHTML = received;
$D('laverage').innerHTML = laverage.toFixed(2); document.getElementById('laverage').innerHTML = laverage.toFixed(2);
$D('lrunning').innerHTML = lrunning.toFixed(2); document.getElementById('lrunning').innerHTML = lrunning.toFixed(2);
$D('lmin').innerHTML = lmin.toFixed(2); document.getElementById('lmin').innerHTML = lmin.toFixed(2);
$D('lmax').innerHTML = lmax.toFixed(2); document.getElementById('lmax').innerHTML = lmax.toFixed(2);
} }
function init_ws() { function init_ws() {
console.log(">> init_ws"); console.log(">> init_ws");
var scheme = "ws://"; var scheme = "ws://";
if ($D('encrypt').checked) { if (document.getElementById('encrypt').checked) {
scheme = "wss://"; scheme = "wss://";
} }
var uri = scheme + host + ":" + port; var uri = scheme + host + ":" + port;
console.log("connecting to " + uri); console.log("connecting to " + uri);
ws = new Websock(); ws = new WebSocket(uri);
ws.maxBufferedAmount = 5000; ws.binaryType = 'arraybuffer';
ws.open(uri);
ws.on('message', function() { ws.addEventListener('message', function(e) {
recvMsg(); recvMsg(e.data);
}); });
ws.on('open', function() { ws.addEventListener('open', function() {
send_ref = setTimeout(sendMsg, sendDelay); send_ref = setTimeout(sendMsg, sendDelay);
}); });
ws.on('close', function(e) { ws.addEventListener('close', function(e) {
disconnect(); disconnect();
}); });
ws.on('error', function(e) { ws.addEventListener('error', function(e) {
message("Websock error: " + e); message("Websock error: " + e);
disconnect(); disconnect();
}); });
@ -203,10 +192,10 @@
function connect() { function connect() {
console.log(">> connect"); console.log(">> connect");
host = $D('host').value; host = document.getElementById('host').value;
port = $D('port').value; port = document.getElementById('port').value;
payload_size = parseInt($D('payload_size').value, 10); payload_size = parseInt(document.getElementById('payload_size').value, 10);
sendDelay = parseInt($D('sendDelay').value, 10); sendDelay = parseInt(document.getElementById('sendDelay').value, 10);
if ((!host) || (!port)) { if ((!host) || (!port)) {
console.log("must set host and port"); console.log("must set host and port");
@ -235,8 +224,8 @@
lmin = 999999999; lmin = 999999999;
lmax = 0; lmax = 0;
$D('connectButton').value = "Stop"; document.getElementById('connectButton').value = "Stop";
$D('connectButton').onclick = disconnect; document.getElementById('connectButton').onclick = disconnect;
console.log("<< connect"); console.log("<< connect");
} }
@ -254,8 +243,8 @@
recv_seq = 0; recv_seq = 0;
send_seq = 0; send_seq = 0;
$D('connectButton').value = "Start"; document.getElementById('connectButton').value = "Start";
$D('connectButton').onclick = connect; document.getElementById('connectButton').onclick = connect;
console.log("<< disconnect"); console.log("<< disconnect");
} }
@ -263,9 +252,9 @@
window.onload = function() { window.onload = function() {
console.log("onload"); console.log("onload");
var url = document.location.href; var url = document.location.href;
$D('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1]; document.getElementById('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
$D('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1]; document.getElementById('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
$D('payload_size').value = payload_size; document.getElementById('payload_size').value = payload_size;
} }
</script> </script>

View File

@ -2,16 +2,6 @@
<head> <head>
<title>WebSockets Load Test</title> <title>WebSockets Load Test</title>
<script src="include/util.js"></script>
<script src="include/webutil.js"></script>
<script src="include/websock.js"></script>
<!-- Uncomment to activate firebug lite -->
<!--
<script type='text/javascript'
src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
-->
</head> </head>
<body> <body>
@ -47,7 +37,7 @@
function error(str) { function error(str) {
console.error(str); console.error(str);
cell = $D('error'); cell = document.getElementById('error');
cell.innerHTML += errors + ": " + str + "\n"; cell.innerHTML += errors + ": " + str + "\n";
cell.scrollTop = cell.scrollHeight; cell.scrollTop = cell.scrollHeight;
} }
@ -74,8 +64,8 @@
function check_respond(data) { function check_respond(data) {
//console.log(">> check_respond"); //console.log(">> check_respond");
var first, last, str, length, chksum, nums, arr; var first, last, str, length, chksum, nums, arr;
first = String.fromCharCode(data.shift()); first = String.fromCharCode(data[0]);
last = String.fromCharCode(data.pop()); last = String.fromCharCode(data[data.length-1]);
if (first != "^") { if (first != "^") {
errors++; errors++;
@ -87,9 +77,11 @@
error("Packet missing end char '$'"); error("Packet missing end char '$'");
return; return;
} }
arr = data.map(function(num) { text = ''
return String.fromCharCode(num); for (var i = 1; i < data.length-1; i++) {
} ).join('').split(':'); text += String.fromCharCode(data[i]);
}
arr = text.split(':');
seq = arr[0]; seq = arr[0];
length = arr[1]; length = arr[1];
chksum = arr[2]; chksum = arr[2];
@ -137,44 +129,44 @@
var nums = numlist.join(''); var nums = numlist.join('');
arr.pushStr("^" + send_seq + ":" + length + ":" + chksum + ":" + nums + "$") arr.pushStr("^" + send_seq + ":" + length + ":" + chksum + ":" + nums + "$")
send_seq ++; send_seq ++;
ws.send(arr); ws.send(new Uint8Array(arr));
sent++; sent++;
} }
function update_stats() { function update_stats() {
$D('sent').innerHTML = sent; document.getElementById('sent').innerHTML = sent;
$D('received').innerHTML = received; document.getElementById('received').innerHTML = received;
$D('errors').innerHTML = errors; document.getElementById('errors').innerHTML = errors;
} }
function init_ws() { function init_ws() {
console.log(">> init_ws"); console.log(">> init_ws");
var scheme = "ws://"; var scheme = "ws://";
if ($D('encrypt').checked) { if (document.getElementById('encrypt').checked) {
scheme = "wss://"; scheme = "wss://";
} }
var uri = scheme + host + ":" + port; var uri = scheme + host + ":" + port;
console.log("connecting to " + uri); console.log("connecting to " + uri);
ws = new Websock(); ws = new WebSocket(uri);
ws.open(uri); ws.binaryType = 'arraybuffer';
ws.on('message', function() { ws.addEventListener('message', function(e) {
//console.log(">> WebSockets.onmessage"); //console.log(">> WebSockets.onmessage");
arr = ws.rQshiftBytes(ws.rQlen()); arr = new Uint8Array(e.data);
check_respond(arr); check_respond(arr);
//console.log("<< WebSockets.onmessage"); //console.log("<< WebSockets.onmessage");
}); });
ws.on('open', function() { ws.addEventListener('open', function() {
console.log(">> WebSockets.onopen"); console.log(">> WebSockets.onopen");
send_ref = setInterval(send, sendDelay); send_ref = setInterval(send, sendDelay);
console.log("<< WebSockets.onopen"); console.log("<< WebSockets.onopen");
}); });
ws.on('close', function(e) { ws.addEventListener('close', function(e) {
console.log(">> WebSockets.onclose"); console.log(">> WebSockets.onclose");
clearInterval(send_ref); clearInterval(send_ref);
console.log("<< WebSockets.onclose"); console.log("<< WebSockets.onclose");
}); });
ws.on('error', function(e) { ws.addEventListener('error', function(e) {
console.log(">> WebSockets.onerror"); console.log(">> WebSockets.onerror");
console.log(" " + e); console.log(" " + e);
console.log("<< WebSockets.onerror"); console.log("<< WebSockets.onerror");
@ -185,9 +177,9 @@
function connect() { function connect() {
console.log(">> connect"); console.log(">> connect");
host = $D('host').value; host = document.getElementById('host').value;
port = $D('port').value; port = document.getElementById('port').value;
sendDelay = parseInt($D('sendDelay').value, 10); sendDelay = parseInt(document.getElementById('sendDelay').value, 10);
if ((!host) || (!port)) { if ((!host) || (!port)) {
console.log("must set host and port"); console.log("must set host and port");
return; return;
@ -199,8 +191,8 @@
init_ws(); init_ws();
update_ref = setInterval(update_stats, 1); update_ref = setInterval(update_stats, 1);
$D('connectButton').value = "Stop"; document.getElementById('connectButton').value = "Stop";
$D('connectButton').onclick = disconnect; document.getElementById('connectButton').onclick = disconnect;
console.log("<< connect"); console.log("<< connect");
} }
@ -215,16 +207,16 @@
recv_seq = 0; recv_seq = 0;
send_seq = 0; send_seq = 0;
$D('connectButton').value = "Start"; document.getElementById('connectButton').value = "Start";
$D('connectButton').onclick = connect; document.getElementById('connectButton').onclick = connect;
console.log("<< disconnect"); console.log("<< disconnect");
} }
window.onload = function() { window.onload = function() {
console.log("onload"); console.log("onload");
var url = document.location.href; var url = document.location.href;
$D('host').value = (url.match(/host=([^&#]*)/) || ['',''])[1]; document.getElementById('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
$D('port').value = (url.match(/port=([^&#]*)/) || ['',''])[1]; document.getElementById('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
} }
</script> </script>

View File

@ -2,15 +2,6 @@
<head> <head>
<title>WebSockets Echo Test</title> <title>WebSockets Echo Test</title>
<script src="include/util.js"></script>
<script src="include/webutil.js"></script>
<!-- Uncomment to activate firebug lite -->
<!--
<script type='text/javascript'
src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
-->
</head> </head>
<body> <body>
@ -35,7 +26,7 @@
function message(str) { function message(str) {
console.log(str); console.log(str);
cell = $D('messages'); cell = document.getElementById('messages');
cell.innerHTML += msg_cnt + ": " + str + "\n"; cell.innerHTML += msg_cnt + ": " + str + "\n";
cell.scrollTop = cell.scrollHeight; cell.scrollTop = cell.scrollHeight;
msg_cnt++; msg_cnt++;
@ -54,15 +45,16 @@
return; return;
} }
var str = "Message #" + send_cnt, arr = []; var str = "Message #" + send_cnt, arr = [];
ws.send(str); var encoder = new TextEncoder();
ws.send(encoder.encode(str));
message("Sent message: '" + str + "'"); message("Sent message: '" + str + "'");
send_cnt++; send_cnt++;
} }
function update_stats() { function update_stats() {
$D('sent').innerHTML = sent; document.getElementById('sent').innerHTML = sent;
$D('received').innerHTML = received; document.getElementById('received').innerHTML = received;
$D('errors').innerHTML = errors; document.getElementById('errors').innerHTML = errors;
} }
function init_ws() { function init_ws() {
@ -71,8 +63,8 @@
} }
function connect() { function connect() {
var host = $D('host').value, var host = document.getElementById('host').value,
port = $D('port').value, port = document.getElementById('port').value,
scheme = "ws://", uri; scheme = "ws://", uri;
console.log(">> connect"); console.log(">> connect");
@ -85,42 +77,45 @@
ws.close(); ws.close();
} }
if ($D('encrypt').checked) { if (document.getElementById('encrypt').checked) {
scheme = "wss://"; scheme = "wss://";
} }
uri = scheme + host + ":" + port; uri = scheme + host + ":" + port;
message("connecting to " + uri); message("connecting to " + uri);
ws = new WebSocket(uri); ws = new WebSocket(uri);
ws.binaryType = 'arraybuffer';
ws.onmessage = function(e) { ws.addEventListener('message', function(e) {
//console.log(">> WebSockets.onmessage"); //console.log(">> WebSockets.onmessage");
message("Received message '" + e.data + "'"); var decoder = new TextDecoder('UTF-8');
var str = decoder.decode(e.data);
message("Received message '" + str + "'");
//console.log("<< WebSockets.onmessage"); //console.log("<< WebSockets.onmessage");
}; });
ws.onopen = function(e) { ws.addEventListener('open', function(e) {
console.log(">> WebSockets.onopen"); console.log(">> WebSockets.onopen");
echo_ref = setInterval(send_msg, echoDelay); echo_ref = setInterval(send_msg, echoDelay);
console.log("<< WebSockets.onopen"); console.log("<< WebSockets.onopen");
}; });
ws.onclose = function(e) { ws.addEventListener('close', function(e) {
console.log(">> WebSockets.onclose"); console.log(">> WebSockets.onclose");
if (echo_ref) { if (echo_ref) {
clearInterval(echo_ref); clearInterval(echo_ref);
echo_ref = null; echo_ref = null;
} }
console.log("<< WebSockets.onclose"); console.log("<< WebSockets.onclose");
}; });
ws.onerror = function(e) { ws.addEventListener('error', function(e) {
console.log(">> WebSockets.onerror"); console.log(">> WebSockets.onerror");
if (echo_ref) { if (echo_ref) {
clearInterval(echo_ref); clearInterval(echo_ref);
echo_ref = null; echo_ref = null;
} }
console.log("<< WebSockets.onerror"); console.log("<< WebSockets.onerror");
}; });
$D('connectButton').value = "Stop"; document.getElementById('connectButton').value = "Stop";
$D('connectButton').onclick = disconnect; document.getElementById('connectButton').onclick = disconnect;
console.log("<< connect"); console.log("<< connect");
} }
@ -134,16 +129,16 @@
clearInterval(echo_ref); clearInterval(echo_ref);
} }
$D('connectButton').value = "Start"; document.getElementById('connectButton').value = "Start";
$D('connectButton').onclick = connect; document.getElementById('connectButton').onclick = connect;
console.log("<< disconnect"); console.log("<< disconnect");
} }
window.onload = function() { window.onload = function() {
console.log("onload"); console.log("onload");
var url = document.location.href; var url = document.location.href;
$D('host').value = (url.match(/host=([^&#]*)/) || ['',''])[1]; document.getElementById('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
$D('port').value = (url.match(/port=([^&#]*)/) || ['',''])[1]; document.getElementById('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
} }
</script> </script>

View File

@ -2,8 +2,6 @@
<head> <head>
<title>Websock Simple Client</title> <title>Websock Simple Client</title>
<script src="include/util.js"></script>
<script src="include/websock.js"></script>
</head> </head>
<body> <body>
@ -20,8 +18,8 @@
<script> <script>
var $D = function(id) { return document.getElementById(id); }, var document.getElementById = function(id) { return document.getElementById(id); },
ws = null, msgs = $D('messages'); ws = null, msgs = document.getElementById('messages');
function msg(str) { function msg(str) {
msgs.innerHTML += str + "\n"; msgs.innerHTML += str + "\n";
@ -29,38 +27,38 @@
} }
function connect() { function connect() {
var uri = $D('target').value; var uri = document.getElementById('target').value;
ws = new Websock()
msg("connecting to: " + uri); msg("connecting to: " + uri);
ws.open(uri); ws = new WebSocket(uri);
ws.on('open', function () { ws.binaryType = 'arraybuffer';
ws.addEventListener('open', function () {
msg("Connected"); msg("Connected");
}); });
ws.on('message', function () { ws.addEventListener('message', function (e) {
msg("Received: " + ws.rQshiftStr()); msg("Received: " + e.data);
}); });
ws.on('close', function () { ws.addEventListener('close', function () {
disconnect(); disconnect();
msg("Disconnected"); msg("Disconnected");
}); });
$D('connectButton').value = "Disconnect"; document.getElementById('connectButton').value = "Disconnect";
$D('connectButton').onclick = disconnect; document.getElementById('connectButton').onclick = disconnect;
$D('sendButton').disabled = false; document.getElementById('sendButton').disabled = false;
} }
function disconnect() { function disconnect() {
if (ws) { ws.close(); } if (ws) { ws.close(); }
ws = null; ws = null;
$D('connectButton').value = "Connect"; document.getElementById('connectButton').value = "Connect";
$D('connectButton').onclick = connect; document.getElementById('connectButton').onclick = connect;
$D('sendButton').disabled = true; document.getElementById('sendButton').disabled = true;
} }
function send() { function send() {
msg("Sending: " + $D('sendText').value); msg("Sending: " + document.getElementById('sendText').value);
ws.send_string($D('sendText').value); ws.send_string(document.getElementById('sendText').value);
}; };
</script> </script>