Last active
June 9, 2020 20:14
-
-
Save jaschaio/2faecb0287d2949440e618b868551977 to your computer and use it in GitHub Desktop.
Bull Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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