Skip to content

Commit

Permalink
perf(encoder): fixed the debuff of encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
fenying committed Apr 15, 2020
1 parent 81ef3da commit f64a9b8
Show file tree
Hide file tree
Showing 42 changed files with 1,034 additions and 205 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/test/
/dist/
/benchmarks/
/*.log
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@
"${workspaceFolder}/examples/**/*.js"
]
},
{
"type": "node",
"request": "launch",
"name": "[Televoke] Http",
"program": "${workspaceFolder}/src/examples/Http.ts",
"sourceMaps": true,
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/lib/*.js",
"${workspaceFolder}/lib/**/*.js",
"${workspaceFolder}/examples/*.js",
"${workspaceFolder}/examples/**/*.js"
]
},
{
"type": "node",
"request": "launch",
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Changes Logs

## v0.2.0

- refactor(global): Improved the experience.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@litert/televoke",
"version": "0.1.1",
"version": "0.2.0",
"description": "A simple RPC service framework.",
"main": "lib/index.js",
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions src/benchmarks/Decoder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/**
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as $Televoke from '../lib';

const decoder = $Televoke.createDecoder();
Expand Down
22 changes: 20 additions & 2 deletions src/benchmarks/Encoder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/**
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as $Televoke from '../lib';

const encoder = $Televoke.createEncoder();
Expand All @@ -6,7 +22,8 @@ const writer: $Televoke.IWritable = {
write(b, c) {

if (c) { c(); }
}
},
writable: true
};

const data = {
Expand Down Expand Up @@ -56,7 +73,8 @@ function testAsync(times: number): void {
write(b, c) {

if (c) { setImmediate(c); }
}
},
writable: true
}, data);

if (!(times-- && setImmediate(testAsync, times))) {
Expand Down
34 changes: 34 additions & 0 deletions src/benchmarks/Http/API.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as $Televoke from '../../lib';

export const BENCHMARK_SERVER_HOST = '127.0.0.1';
export const BENCHMARK_SERVER_PORT = 9988;

export interface IGreetArguments {

name: string;
}

export interface IGa extends $Televoke.IServiceAPIs {

hi(data: IGreetArguments): string;

Hello(data: IGreetArguments): string;

TestError(data: IGreetArguments): string;
}
50 changes: 50 additions & 0 deletions src/benchmarks/Http/Client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as $Televoke from '../../lib';
import { IGa, BENCHMARK_SERVER_PORT, BENCHMARK_SERVER_HOST } from './API';

(async () => {

const client = $Televoke.createHttpClient<IGa>(BENCHMARK_SERVER_HOST, BENCHMARK_SERVER_PORT, Math.random);

await client.connect();

console.time('TCP Invoke Concurrent');
await Promise.all(Array(10000).fill(0).map(() => client.invoke('hi', {name: 'Angus'})));
console.timeEnd('TCP Invoke Concurrent');

console.time('TCP Invoke Sequence');
for (let i = 0; i < 10000; i++) {

await client.invoke('hi', {name: 'Angus'});
}
console.timeEnd('TCP Invoke Sequence');

console.time('TCP Call Concurrent');
await Promise.all(Array(10000).fill(0).map(() => client.call('hi', {name: 'Angus'})));
console.timeEnd('TCP Call Concurrent');

console.time('TCP Call Sequence');
for (let i = 0; i < 10000; i++) {

await client.call('hi', {name: 'Angus'});
}
console.timeEnd('TCP Call Sequence');

await client.close();

})().catch(console.error);
48 changes: 48 additions & 0 deletions src/benchmarks/Http/Server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as $Televoke from '../../lib';
import { IGa, BENCHMARK_SERVER_PORT, BENCHMARK_SERVER_HOST } from './API';

(async () => {

const router = $Televoke.createSimpleRouter();

router.add<IGa['hi']>('hi', async function(data) {

return `Hi, ${data.name}`;
});

router.register<IGa['Hello']>('Hello', async function(_) {

return `Hello, ${_.args[0].name}`;
});

router.add<IGa['TestError']>('TestError', async function(data) {

throw `Hello, ${data.name}`;
});

const server = $Televoke.createServer();

server.setRouter(router);
server.on('error', console.error);
server.on('handler_error', console.error);
server.addGateway('tcp', $Televoke.createHttpGateway(BENCHMARK_SERVER_HOST, BENCHMARK_SERVER_PORT));

await server.start();

})().catch(console.error);
34 changes: 34 additions & 0 deletions src/benchmarks/TCP/API.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as $Televoke from '../../lib';

export const BENCHMARK_SERVER_HOST = '127.0.0.1';
export const BENCHMARK_SERVER_PORT = 9988;

export interface IGreetArguments {

name: string;
}

export interface IGa extends $Televoke.IServiceAPIs {

hi(data: IGreetArguments): string;

Hello(data: IGreetArguments): string;

TestError(data: IGreetArguments): string;
}
65 changes: 65 additions & 0 deletions src/benchmarks/TCP/Client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as $Televoke from '../../lib';
import { IGa, BENCHMARK_SERVER_PORT, BENCHMARK_SERVER_HOST } from './API';

const ridGenerator = (function() {

let i = 0;
return () => i++;
})();

const CONCURRENCY = 10000;

(async () => {

const client = $Televoke.createTCPClient<IGa>(BENCHMARK_SERVER_HOST, BENCHMARK_SERVER_PORT, ridGenerator);

await client.connect();

await Promise.all(Array(CONCURRENCY).fill(0).map(() => client.invoke('hi', {name: 'Angus'})));

for (let i = 0; i < CONCURRENCY; i++) {

await client.invoke('hi', {name: 'Angus'});
}

console.time(`TCP ${CONCURRENCY} Invokes Concurrent`);
await Promise.all(Array(CONCURRENCY).fill(0).map(() => client.invoke('hi', {name: 'Angus'})));
console.timeEnd(`TCP ${CONCURRENCY} Invokes Concurrent`);

console.time(`TCP ${CONCURRENCY} Invokes Sequence`);
for (let i = 0; i < CONCURRENCY; i++) {

await client.invoke('hi', {name: 'Angus'});
}
console.timeEnd(`TCP ${CONCURRENCY} Invokes Sequence`);

console.time(`TCP ${CONCURRENCY} Calls Concurrent`);
await Promise.all(Array(CONCURRENCY).fill(0).map(() => client.call('hi', {name: 'Angus'})));
console.timeEnd(`TCP ${CONCURRENCY} Calls Concurrent`);

console.time(`TCP ${CONCURRENCY} Calls Sequence`);
for (let i = 0; i < CONCURRENCY; i++) {

await client.call('hi', {name: 'Angus'});
}
console.timeEnd(`TCP ${CONCURRENCY} Calls Sequence`);

await client.close();

})().catch(console.error);
48 changes: 48 additions & 0 deletions src/benchmarks/TCP/Server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as $Televoke from '../../lib';
import { IGa, BENCHMARK_SERVER_PORT, BENCHMARK_SERVER_HOST } from './API';

(async () => {

const router = $Televoke.createSimpleRouter();

router.add<IGa['hi']>('hi', async function(data) {

return `Hi, ${data.name}`;
});

router.register<IGa['Hello']>('Hello', async function(_) {

return `Hello, ${_.args[0].name}`;
});

router.add<IGa['TestError']>('TestError', async function(data) {

throw `Hello, ${data.name}`;
});

const server = $Televoke.createServer();

server.setRouter(router);
server.on('error', console.error);
server.on('handler_error', console.error);
server.addGateway('tcp', $Televoke.createTCPGateway(BENCHMARK_SERVER_HOST, BENCHMARK_SERVER_PORT));

await server.start();

})().catch(console.error);
Loading

0 comments on commit f64a9b8

Please sign in to comment.