nope/test/testAmqp.ts

115 lines
3.0 KiB
TypeScript
Raw Normal View History

import 'reflect-metadata';
2020-09-15 05:58:54 +00:00
import { AmqpLayer, } from "../lib/communication/amqpLayer";
2020-09-12 20:23:55 +00:00
import { generateBenchmarkFunction } from "../modules/funcs/generateBenchmarkFunction";
2020-09-15 05:58:54 +00:00
import { nopeDispatcher } from "../lib/dispatcher/nopeDispatcher";
import { promisify } from 'util'
import { getLogger } from "../lib/logger/getLogger";
import { nopeObservable } from "../lib/observables/nopeObservable";
2020-09-12 20:23:55 +00:00
2020-09-15 05:58:54 +00:00
const client1 = new AmqpLayer('localhost');
const client2 = new AmqpLayer('localhost');
const client3 = new AmqpLayer('localhost');
2020-09-12 20:23:55 +00:00
2020-09-15 05:58:54 +00:00
const sleep = promisify(setTimeout)
2020-09-28 19:40:33 +00:00
const logger = getLogger('info','DISPATCHER')
2020-09-15 05:58:54 +00:00
function genFunc(name, delay) {
const logger = getLogger('debug',name)
return (idx: number) => {
return new Promise((resolve, reject) => {
try {
logger.info('started ' + idx.toString());
setTimeout(() => {
logger.info('finished ' + idx.toString());
resolve();
}, delay);
} catch(e){
reject(e)
}
})
}
}
2020-09-28 19:40:33 +00:00
2020-09-15 05:58:54 +00:00
const main = async () => {
2020-09-30 06:25:03 +00:00
let max = 10000;
await sleep(1000);
const dispatcher1 = new nopeDispatcher({ communicator: client1, logger }, () => new nopeObservable());
const dispatcher2 = new nopeDispatcher({ communicator: client2, logger }, () => new nopeObservable());
2020-09-30 06:25:03 +00:00
2020-09-28 19:40:33 +00:00
const benchmark = generateBenchmarkFunction(1000, 'Executed')
2020-09-15 05:58:54 +00:00
// Register Functions
dispatcher1.registerFunction(genFunc('dispatcher-1-slow', 2000), {
2020-09-15 05:58:54 +00:00
id: 'func'
});
dispatcher2.registerFunction(genFunc('dispatcher-2-fast', 500), {
2020-09-15 05:58:54 +00:00
id: 'func'
});
2020-09-28 19:40:33 +00:00
dispatcher1.registerFunction(async () => benchmark(), {
id: 'benchmark'
})
2020-09-30 06:25:03 +00:00
await sleep(500);
const dispatcher3 = new nopeDispatcher({ communicator: client3, logger }, () => new nopeObservable());
await sleep(500);
2020-09-15 05:58:54 +00:00
let c = 0
// Call Tasks Parallel!
while (c < 10) {
dispatcher3.performCall('func', [c++]).catch(e => console.error(e));
2020-09-12 20:23:55 +00:00
}
2020-09-28 19:40:33 +00:00
// Benchmark:
c = 0
// Call Tasks Parallel!
2020-09-30 06:25:03 +00:00
while (c < max) {
2020-09-28 19:40:33 +00:00
await dispatcher3.performCall('benchmark', [], {
paramsHasNoCallback: true,
});
c++;
}
2020-09-30 06:25:03 +00:00
await sleep(500);
const obs01 = new nopeObservable<number>();
dispatcher1.registerObservable(obs01, {
mode: 'publish',
topic: 'test.var'
});
const obs02 = new nopeObservable<number>();
dispatcher2.registerObservable(obs02, {
mode: 'subscribe',
topic: 'test.var'
});
obs02.subscribe(benchmark);
obs01.setContent(1200);
c = 0;
while (c < max) {
obs01.setContent(c++);
await sleep(0)
}
await client1.on('subscribe','test_01', (...args) => {
console.log('c1',...args);
})
2020-09-15 05:58:54 +00:00
2020-09-30 06:25:03 +00:00
await client2.on('subscribe','test_02', benchmark)
2020-09-15 05:58:54 +00:00
2020-09-30 06:25:03 +00:00
c = 0;
while(c < max){
await client1.emit('publish','test_02', c++);
}
2020-09-15 05:58:54 +00:00
2020-09-12 20:23:55 +00:00
2020-09-15 05:58:54 +00:00
}
2020-09-12 20:23:55 +00:00
2020-09-15 05:58:54 +00:00
main();