More testing

This commit is contained in:
Alex Hultman 2019-01-26 16:26:51 +01:00
parent 282609bfb4
commit adc5324360

View File

@ -15,6 +15,11 @@ const port = 9001;
let openedClientConnections = 0; let openedClientConnections = 0;
let closedClientConnections = 0; let closedClientConnections = 0;
function printStatistics() {
console.log('Opened clients ' + openedClientConnections);
console.log('Closed clients ' + closedClientConnections + '\n');
}
let listenSocket; let listenSocket;
/* 0 to 1-less than max */ /* 0 to 1-less than max */
@ -27,14 +32,14 @@ function establishNewConnection() {
ws.on('open', () => { ws.on('open', () => {
/* Open more connections */ /* Open more connections */
if (++openedClientConnections < 1000) { if (++openedClientConnections < 100) {
establishNewConnection(); establishNewConnection();
} else { } else {
/* Stop listening */ /* Stop listening */
uWS.us_listen_socket_close(listenSocket); uWS.us_listen_socket_close(listenSocket);
} }
console.log('Opened ' + openedClientConnections + ' client connections so far.'); printStatistics();
performRandomClientAction(ws); performRandomClientAction(ws);
}); });
@ -44,15 +49,27 @@ function establishNewConnection() {
}); });
ws.on('close', () => { ws.on('close', () => {
console.log("client was closed"); closedClientConnections++;
printStatistics();
//performRandomClientAction(ws); //performRandomClientAction(ws);
}); });
} }
const maxBackpressure = 50 * 1024 * 1024;
const maxPayloadSize = 5 * 1024 * 1024;
const largeBuffer = new ArrayBuffer(maxPayloadSize);
/* Perform random websockets/ws action */ /* Perform random websockets/ws action */
function performRandomClientAction(ws) { function performRandomClientAction(ws) {
/* 0, 1 but never 2 */ /* 0, 1 but never 2 */
switch (getRandomInt(2)) { let action = getRandomInt(2);
/* Sending a message should have higher probability */
if (getRandomInt(100) < 80) {
action = 1;
}
switch (action) {
case 0: { case 0: {
ws.close(); ws.close();
break; break;
@ -76,16 +93,32 @@ function performRandomClientAction(ws) {
} }
/* Perform random uWebSockets.js action */ /* Perform random uWebSockets.js action */
function performRandomServerAction(ws) { function performRandomServerAction(ws, uniform) {
let action = getRandomInt(2);
/* Sending a message should have higher probability,
* except for when we are draining. */
if (!uniform) {
if (getRandomInt(100) < 80) {
action = 1;
}
}
/* 0, 1 but never 2 */ /* 0, 1 but never 2 */
switch (getRandomInt(2)) { switch (action) {
case 0: { case 0: {
ws.close(); ws.close();
break; break;
} }
case 1: { case 1: {
/* Length should be random from small to huge */ /* If we have very high backpressure we skip sending more */
ws.send('a test message', false); if (ws.getBufferedAmount() > maxBackpressure) {
/* Do something else instead */
performRandomServerAction(ws);
} else {
/* Length should be random from small to huge, ArrayBuffer.slice is a copy (bad but we don't care I guess) */
ws.send(largeBuffer.slice(0, getRandomInt(maxPayloadSize + 1)));
}
break; break;
} }
case 2: { case 2: {
@ -102,23 +135,32 @@ const app = uWS./*SSL*/App({
passphrase: '1234' passphrase: '1234'
}).ws('/*', { }).ws('/*', {
compression: 0, compression: 0,
maxPayloadLength: 16 * 1024 * 1024, /* We intentionally accept less than what might be sent so to test force closing */
idleTimeout: 10, maxPayloadLength: maxPayloadSize / 2,
idleTimeout: 100,
open: (ws, req) => { open: (ws, req) => {
/* Doing a terminate here will be interesting */ /* Doing a terminate here will be interesting */
performRandomServerAction(ws); performRandomServerAction(ws, false);
}, },
message: (ws, message, isBinary) => { message: (ws, message, isBinary) => {
performRandomServerAction(ws); performRandomServerAction(ws, false);
}, },
drain: (ws) => { drain: (ws) => {
// todo: dont send over a certain backpressure /* We only perform actions while draining rarely (5%), and we do it uniformly.
//performRandomServerAction(ws); * There's no real NEED to do anything here, as it will eventually result in
* message event for client. */
if (getRandomInt(100) < 5) {
performRandomServerAction(ws, true);
}
}, },
close: (ws, code, message) => { close: (ws, code, message) => {
/* TODO: We SHOULD TECHNICALLY allow sending here.
* It should not throw, but just silently ignore it
* as it makes no sense to send on a closed socket
* while still being technically valid */
try { try {
performRandomServerAction(ws); performRandomServerAction(ws, false);
} catch (e) { } catch (e) {
/* We expect to land here always, since socket is invalid */ /* We expect to land here always, since socket is invalid */
return; return;
@ -143,5 +185,6 @@ const app = uWS./*SSL*/App({
/* Yes we do this crazy thing here */ /* Yes we do this crazy thing here */
process.on('beforeExit', () => { process.on('beforeExit', () => {
uWS.print('Exiting now');
app.forcefully_free(); app.forcefully_free();
}); });