Skip to content

Instantly share code, notes, and snippets.

@sompylasar
Last active January 11, 2019 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sompylasar/210fead4632807270b10f6d3f077c6da to your computer and use it in GitHub Desktop.
Save sompylasar/210fead4632807270b10f6d3f077c6da to your computer and use it in GitHub Desktop.
Progress tracking and time estimate for a queue of similarly sized tasks.
const progressDefaultColor = typeof chalk !== 'undefined' ? chalk.magenta : (x) => x;
const progressErrorColor = typeof chalk !== 'undefined' ? chalk.red : (x) => x;
function formatProgress(startTime, queuedCount, finishedCount, errorsCount) {
const progressPercent = queuedCount > 0 ? (100 * finishedCount) / queuedCount : 100;
const errorsPercent = queuedCount > 0 ? (100 * errorsCount) / queuedCount : 0;
const elapsedTimeSeconds = (Date.now() - startTime) / 1000;
const itemsPerSecond = elapsedTimeSeconds > 0 ? finishedCount / elapsedTimeSeconds : 0;
const estimatedTimeSeconds = itemsPerSecond > 0 ? Math.max(0, (queuedCount - finishedCount) / itemsPerSecond) : 0;
return (
progressDefaultColor(
`${finishedCount} of ${queuedCount} (${progressPercent.toFixed(2)}%, ${itemsPerSecond.toFixed(2)}/sec) `,
) +
(errorsCount > 0 ? progressErrorColor(`${errorsCount} with errors (${errorsPercent.toFixed(2)}%) `) : '') +
progressDefaultColor(`${elapsedTimeSeconds.toFixed(0)}sec elapsed, ${estimatedTimeSeconds.toFixed(0)}sec estimated`)
);
}

Overview

Progress tracking and time estimate for a queue of similarly sized tasks.

Usage

const doTaskAsync = require('./doTaskAsync');

async function doManyTasksAsync() {
  const manyTaskInputs = new Array(1000);
  let startTime = Date.now();
  let totalPlanned = manyTaskInputs.length;
  let totalDone = 0;
  let totalWithErrors = 0;
  for (const taskInput of manyTaskInputs) {
    try {
      await doTaskAsync(taskInput);
    } catch (error) {
      totalWithErrors++;
    }
    totalDone++;
    console.log(formatProgress(startTime, totalPlanned, totalDone, totalWithErrors));
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment