Skip to content

Instantly share code, notes, and snippets.

@jaschaio
Last active Jun 9, 2020
Embed
What would you like to do?
Bull Test
const cluster = require( 'cluster' );
const Bull = require( 'bull' );
if ( cluster.isMaster ) {
var hrstart;
for ( let i = 0; i++ < 4; ) {
let worker = cluster.fork();
worker.on( 'message', ( msg ) => {
const { start, end } = msg;
if ( start ) {
hrstart = process.hrtime();
console.log( "started queue" );
} else {
var hrend = process.hrtime( hrstart );
console.log( `finished queue in ${ hrend[ 0 ] }s ${ hrend[ 1 ] / 1000000 }ms` );
}
} );
}
} else {
( async () => {
const queue = new Bull( 'queue' );
if ( cluster.worker.id === 1 ) {
var promises = [];
for ( let i = 0; i++ < 10000; )
promises.push( queue.add( 'job', { i } ) );
await Promise.all( promises );
} else {
queue.process( 'job', async ( job ) => {
const { i } = job.data;
var timeout = new Promise( ( resolve ) => setTimeout( () => resolve(), 10 ) );
await timeout;
if ( i === 1 )
process.send( { start: true } );
else if ( i === 10000 )
process.send( { end: true } )
} );
}
} )();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment