From 755cef81fc6fc8c763bb15adb714bcb82d259f94 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Wed, 2 Oct 2024 17:32:33 +0900 Subject: [PATCH] Stress testing --- src/protocols/web/WebSocketConnector.ts | 4 +- .../web/internal/_test_web_stress_client.ts | 41 +++++++++++++++++++ test/node/protocols/web/test_web_stress.ts | 32 +++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 test/node/protocols/web/internal/_test_web_stress_client.ts create mode 100644 test/node/protocols/web/test_web_stress.ts diff --git a/src/protocols/web/WebSocketConnector.ts b/src/protocols/web/WebSocketConnector.ts index 709fdfb..b531c7f 100644 --- a/src/protocols/web/WebSocketConnector.ts +++ b/src/protocols/web/WebSocketConnector.ts @@ -146,11 +146,11 @@ export class WebSocketConnector< this.socket_!.onclose = once((evt) => { reject(new WebSocketError(evt.code, evt.reason)); }); - this.socket_!.onerror = once(() => { + this.socket_!.onerror = once((evt) => { reject( new WebSocketError( 1006, - "Error on WebSocketConnector.connect(): connection refused.", + `Error on WebSocketConnector.connect(): ${(evt as any as ErrorEvent)?.message ?? "connection refused."}`, ), ); }); diff --git a/test/node/protocols/web/internal/_test_web_stress_client.ts b/test/node/protocols/web/internal/_test_web_stress_client.ts new file mode 100644 index 0000000..5ea970d --- /dev/null +++ b/test/node/protocols/web/internal/_test_web_stress_client.ts @@ -0,0 +1,41 @@ +import { WebSocketConnector, WorkerServer } from "tgrid"; +import { sleep_for } from "tstl"; + +interface ICalculator { + plus(x: number, y: number): Promise; +} + +class ClientServant { + public async execute(): Promise { + let success: number = 0; + await Promise.all( + new Array(REPEAT).fill(0).map(async (_, i) => { + try { + await this.individual(i); + ++success; + } catch {} + }), + ); + return success; + } + + private async individual(i: number): Promise { + const connector: WebSocketConnector = + new WebSocketConnector(null, null); + await sleep_for(INTERVAL * i); + await connector.connect(`ws://127.0.0.1:12345`); + await connector.getDriver().plus(1, 2); + await sleep_for(DELAY - INTERVAL * i); + await connector.close(); + } +} + +const main = async (): Promise => { + const worker = new WorkerServer(); + await worker.open(new ClientServant()); +}; +main().catch(console.error); + +const REPEAT = 100; +const DELAY = 10_000; +const INTERVAL = 5; diff --git a/test/node/protocols/web/test_web_stress.ts b/test/node/protocols/web/test_web_stress.ts new file mode 100644 index 0000000..1d52b00 --- /dev/null +++ b/test/node/protocols/web/test_web_stress.ts @@ -0,0 +1,32 @@ +import { WebSocketServer, WorkerConnector } from "tgrid"; + +export async function test_web_stress(): Promise { + const server: WebSocketServer = new WebSocketServer(); + await server.open(12_345, async (acceptor) => { + await acceptor.accept(new Calculator()); + }); + + const counts: number[] = await Promise.all( + new Array(VOLUME).fill(0).map(async () => { + const worker = new WorkerConnector(null, null, "process"); + await worker.connect(`${__dirname}/internal/_test_web_stress_client.js`); + const success: number = await worker.getDriver().execute(); + await worker.close(); + return success; + }), + ); + if (counts.reduce((x, y) => x + y) !== VOLUME * 100) + throw new Error("Error on stress test"); + await server.close(); +} + +interface IServant { + execute(): number; +} +class Calculator { + public plus(x: number, y: number): number { + return x + y; + } +} + +const VOLUME = 10;