nope/test/testAmqp.ts

72 lines
2.0 KiB
TypeScript
Raw Normal View History

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";
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)
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)
}
})
}
}
const main = async () => {
await sleep(1000);
const dispatcher3 = new nopeDispatcher({ communicator: client3 });
const dispatcher1 = new nopeDispatcher({ communicator: client1 });
const dispatcher2 = new nopeDispatcher({ communicator: client2 });
await sleep(1000);
2020-09-12 20:23:55 +00:00
const benchmark = generateBenchmarkFunction(10000, 'Executed')
2020-09-15 05:58:54 +00:00
// Register Functions
dispatcher1.registerFunction(genFunc('dispatcher-1-slow', 1000), {
id: 'func'
});
dispatcher2.registerFunction(genFunc('dispatcher-2-fast', 2000), {
id: 'func'
});
await sleep(1000);
2020-09-12 20:23:55 +00:00
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-15 05:58:54 +00:00
// await client1.on('subscribe','test_01', (...args) => {
// console.log('c1',...args);
// })
// await client2.on('subscribe','test_02', (...args) => {
// benchmark()
// })
// let i = 0;
// let max = 100000;
// while(i < max){
// await client1.emit('publish','test_02', i)
// i++;
// }
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();