first commit
This commit is contained in:
37
util.js
Normal file
37
util.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const { Worker } = require('worker_threads');
|
||||
let totalSuccess = 0;
|
||||
let totalError = 0;
|
||||
const startTime = Date.now();
|
||||
module.exports = {
|
||||
workerFactory: (
|
||||
workerData,
|
||||
onMessage = ({ id, payload }) => console.log("onMessage", id, payload),
|
||||
onError = ({ id, error }) => console.error("onError", id, error),
|
||||
onExit = ({ id, status }) => console.log("onExit", id, status)) => {
|
||||
return new Worker('./worker.js', { workerData })
|
||||
.on('message', (payload) => onMessage({ id: workerData.id, payload }))
|
||||
.on('error', (error) => onError({ id: workerData.id, error }))
|
||||
.on('exit', (status) => onExit({ id: workerData.id, status }));
|
||||
},
|
||||
onMessage: ({ id, payload }) => {
|
||||
switch (payload.status) {
|
||||
case 'starting':
|
||||
console.log(`${payload.id}\t⌛ Starting with url: ${payload.url}`);
|
||||
return;
|
||||
case 'success':
|
||||
totalSuccess++;
|
||||
break;
|
||||
case 'recoverable-error':
|
||||
totalError++;
|
||||
//console.warn(`\r${id}\t🤷 ${payload.error}.`);
|
||||
break;
|
||||
}
|
||||
const totalRequests = totalError + totalSuccess;
|
||||
const timeDelta = Date.now() - startTime;
|
||||
const rps = totalRequests / (timeDelta / 1000);
|
||||
const successRate = (totalSuccess/totalRequests)*100
|
||||
process.stdout.write(`\r${id}\t📄 Requests: ${totalSuccess}/${totalRequests}. 📊 Success rate: ${successRate.toFixed(2)}%. ⚡ Req/s: ${rps.toFixed(2)}. ⏲️ Req/h: ${(rps * 3600).toFixed(2)}`);
|
||||
},
|
||||
onError: ({ id, error }) => console.error(`\r${id}\t❌ ${error}. Total Successful Requests: ${totalSuccess}`),
|
||||
onExit: ({ id, status }) => status === 0 ? console.log(`${id}\t✅ Gracefully terminated.`) : console.error(`${id}\t❌ Exit status: ${status}`)
|
||||
}
|
||||
Reference in New Issue
Block a user