Skip to content

Commit

Permalink
fix(test): remade the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fenying committed Jun 30, 2024
1 parent c7f9a6a commit 0d150f5
Show file tree
Hide file tree
Showing 31 changed files with 776 additions and 1,287 deletions.
111 changes: 105 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,40 @@
{
"type": "node",
"request": "launch",
"name": "[Televoke] Network: Start Server",
"name": "[Televoke] Example: Memory",
"program": "${workspaceFolder}/src/examples/memory.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Thread Worker - Server on Main Thread",
"program": "${workspaceFolder}/src/examples/worker-thread/main-server.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Thread Worker - Server on Worker Thread",
"program": "${workspaceFolder}/src/examples/worker-thread/main-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Network - Start Server",
"program": "${workspaceFolder}/src/examples/network/server.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
Expand All @@ -18,7 +51,7 @@
{
"type": "node",
"request": "launch",
"name": "[Televoke] Network: Start Legacy Http Client",
"name": "[Televoke] Example: Network - Start Legacy Http Client (TCP)",
"program": "${workspaceFolder}/src/examples/network/legacy-http-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
Expand All @@ -29,7 +62,62 @@
{
"type": "node",
"request": "launch",
"name": "[Televoke] Network: Start WebSocket Client",
"name": "[Televoke] Example: Network - Start Legacy Http Client (Unix Socket)",
"program": "${workspaceFolder}/src/examples/network/legacy-http-unix-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Network - Start Legacy Http Client (TLS)",
"program": "${workspaceFolder}/src/examples/network/legacy-http-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Network - Start LwDFX Client (TCP)",
"program": "${workspaceFolder}/src/examples/network/lwdfx-tcp-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Network - Start LwDFX Client (Unix Socket)",
"program": "${workspaceFolder}/src/examples/network/lwdfx-unix-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Network - Start LwDFX Client (TLS)",
"program": "${workspaceFolder}/src/examples/network/lwdfx-tls-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Network - Start WebSocket Client (TCP)",
"program": "${workspaceFolder}/src/examples/network/ws-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
Expand All @@ -40,13 +128,24 @@
{
"type": "node",
"request": "launch",
"name": "[Televoke] Demo: HTTPS Client",
"program": "${workspaceFolder}/src/examples/Https.ts",
"name": "[Televoke] Example: Network - Start WebSocket Client (Unix Socket)",
"program": "${workspaceFolder}/src/examples/network/ws-unix-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
}
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Example: Network - Start WebSocket Client (TLS)",
"program": "${workspaceFolder}/src/examples/network/wss-client.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/**/*.js",
]
},
]
}
134 changes: 6 additions & 128 deletions src/examples/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,150 +14,28 @@
* limitations under the License.
*/

import * as Crypto from 'node:crypto';
import * as Tv from '../lib';
import * as Memory from '../lib/transporters/memory';
import { IApis } from './shared/decl';
import { doClientTest } from './shared/client';
import { router } from './shared/router';

const router = new Tv.Servers.SimpleJsonApiRouter();

const server = new Tv.Servers.TvServer(router);

interface IApis {

debug(text: string): void;

startStream2Server(): number;

startStream2Client(streamId: number): void;
}

function sumBuffer(d: Buffer, b: number): number {

// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < d.length; i++) {

b += d[i];
}

return b;
}

router
.registerApi('debug', (ctx, text: string): void => {

console.log(`[Server] Channel#${ctx.channel.id} invoked debug`);
console.log(`[Server] Client says: ${text}`);

if (Math.random() > 0.5) {
console.log('[Server] Sent a message to client');
ctx.channel.sendMessage('hello').catch(console.error);
}
})
.registerApi('startStream2Server', (ctx): number => {

console.log(`[Server] Channel#${ctx.channel.id} invoked startStream2Server`);
const stream = ctx.channel.streams.create();
console.log(`[Server] Opened stream #${stream.id}`);

testRecvStream(stream, 'Server');

return stream.id;
})
.registerApi('startStream2Client', (ctx, streamId: number): void => {

console.log(`[Server] Channel#${ctx.channel.id} invoked startStream2Client`);
testSendingStream(ctx.channel, streamId, 'Server').catch(console.error);
});
const server = new Tv.Servers.TvServer(router)
.on('error', (e) => { console.error(e); });

const memoryGateway = Memory.createServer({ name: 'hello', 'server': server }, (s) => {

server.registerChannel(s);
});

function sleep(ms: number): Promise<void> {

return new Promise((resolve) => {

setTimeout(resolve, ms);
});
}

// setInterval(() => { return; }, 1000);

function testRecvStream(stream: Tv.IBinaryReadStream, endpoint: 'Client' | 'Server'): void {

let sum = 0;
stream.on('data', (chunk) => {

sum = sumBuffer(chunk, sum);

}).on('end', () => {

console.log(`[${endpoint}] Stream #${stream.id} all received, sum = ${sum}`);
});
}

async function testSendingStream(
ch: Pick<Tv.Clients.IClient<IApis>, 'sendBinaryChunk'>,
streamId: number,
endpoint: 'Client' | 'Server'
): Promise<void> {

const buffers = new Array(Math.ceil(Math.random() * 10))
.fill(0)
.map(() => Crypto.randomBytes(Math.ceil(Math.random() * 1024)));

let sum = 0;

for (const [i, p] of buffers.entries()) {

sum = sumBuffer(p, sum);

await ch.sendBinaryChunk(streamId, i, p);

await sleep(Math.floor(1000 * Math.random()));
}

await ch.sendBinaryChunk(streamId, buffers.length, null);

console.log(`[${endpoint}]: Stream #${streamId} all sent, sum = ${sum}`);
}

(async () => {

await memoryGateway.start();

console.log(`Server [${memoryGateway.name}] started.`);

const client: Tv.Clients.IClient<IApis> = Tv.Clients.createJsonApiClient<IApis>(
Memory.createConnector(memoryGateway.name)
);

client.on('push_message', (msg) => {

console.log('[Client] Message from server: ' + Buffer.concat(msg).toString());
});

do {

await sleep(100);

switch ((['debug', 'startStream2Server', 'startStream2Client'] as const)[Math.floor(Math.random() * 3)]) {
case 'debug':
await client.invoke('debug', new Date() + ': Hello, world!');
break;
case 'startStream2Server':
testSendingStream(client, await client.invoke('startStream2Server'), 'Client')
.catch(console.error);
break;
case 'startStream2Client': {
const stream = client.streams.create();
await client.invoke('startStream2Client', stream.id);
testRecvStream(stream, 'Client');
break;
}
}
}
while (1);
doClientTest(client);

})().catch(console.error);
5 changes: 3 additions & 2 deletions src/examples/network/legacy-http-benchmark-unix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

import * as Tv from '../../lib';
import { IApis, getClaOption } from './shared';
import { getClaOption } from '../shared/test-utils';
import { IApis } from '../shared/decl';

const cases = new Array(10000).fill(0) as number[];

Expand All @@ -29,7 +30,7 @@ const cases = new Array(10000).fill(0) as number[];

for (let i = 0; i < 10; i++) {
console.time('[televoke1/http] 10000 requests');
await Promise.all(cases.map(() => client.invoke('say', 'test')));
await Promise.all(cases.map(() => client.invoke('echo', 'test')));
console.timeEnd('[televoke1/http] 10000 requests');
}

Expand Down
5 changes: 3 additions & 2 deletions src/examples/network/legacy-http-benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

import * as Tv from '../../lib';
import { IApis, getClaOption } from './shared';
import { getClaOption } from '../shared/test-utils';
import { IApis } from '../shared/decl';

const cases = new Array(10000).fill(0) as number[];

Expand All @@ -30,7 +31,7 @@ const cases = new Array(10000).fill(0) as number[];

for (let i = 0; i < 10; i++) {
console.time('[televoke1/http] 10000 requests');
await Promise.all(cases.map(() => client.invoke('say', 'test')));
await Promise.all(cases.map(() => client.invoke('echo', 'test')));
console.timeEnd('[televoke1/http] 10000 requests');
}

Expand Down
Loading

0 comments on commit 0d150f5

Please sign in to comment.