-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
test/node/protocols/web/internal/_test_web_stress_client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |