Skip to content

Commit

Permalink
Stress testing
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Oct 2, 2024
1 parent f58deb0 commit 755cef8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/protocols/web/WebSocketConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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."}`,
),
);
});
Expand Down
41 changes: 41 additions & 0 deletions test/node/protocols/web/internal/_test_web_stress_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { WebSocketConnector, WorkerServer } from "tgrid";
import { sleep_for } from "tstl";

interface ICalculator {
plus(x: number, y: number): Promise<number>;
}

class ClientServant {
public async execute(): Promise<number> {
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<void> {
const connector: WebSocketConnector<null, null, ICalculator> =
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<void> => {
const worker = new WorkerServer();
await worker.open(new ClientServant());
};
main().catch(console.error);

const REPEAT = 100;
const DELAY = 10_000;
const INTERVAL = 5;
32 changes: 32 additions & 0 deletions test/node/protocols/web/test_web_stress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { WebSocketServer, WorkerConnector } from "tgrid";

export async function test_web_stress(): Promise<void> {
const server: WebSocketServer<null, Calculator, null> = 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<IServant>().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;

0 comments on commit 755cef8

Please sign in to comment.