Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(progress): re-vamps the performance log for ease of use
- Loading branch information
Showing
9 changed files
with
214 additions
and
96 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,85 @@ | ||
const chalk = require("chalk"); | ||
|
||
const MS_PER_SECOND = 1000; | ||
const DECIMAL_BASE = 10; | ||
const MAX_EXPECTED_LENGTH = 9; | ||
const MS_PER_MICRO_SECOND = 0.001; | ||
const MAX_LENGTH_EXPECTED = 12; | ||
const NUMBER_OF_COLUMNS = 8; | ||
const K = 1024; | ||
/* | ||
* using `undefined` as the first parameter to Intl.NumberFormat so | ||
* it will fall back to the 'current' locale. Another way to | ||
* accomplish this is to use a non-existent language (e.g. `zz`) also works, | ||
* but this seems to be the lesser of the two evil as `undefined` is closer | ||
* to the intent of just skipping the optional parameter. | ||
*/ | ||
// eslint-disable-next-line no-undefined | ||
const LOCALE = undefined; | ||
|
||
const gTimeFormat = new Intl.NumberFormat(LOCALE, { | ||
style: "unit", | ||
unit: "millisecond", | ||
unitDisplay: "narrow", | ||
maximumFractionDigits: 0, | ||
}).format; | ||
const gSizeFormat = new Intl.NumberFormat(LOCALE, { | ||
signDisplay: "exceptZero", | ||
style: "unit", | ||
unit: "kilobyte", | ||
unitDisplay: "narrow", | ||
maximumFractionDigits: 0, | ||
}).format; | ||
|
||
const pad = (pString) => pString.padStart(MAX_LENGTH_EXPECTED); | ||
|
||
function formatTime(pNumber) { | ||
return Math.round(MS_PER_SECOND * pNumber) | ||
.toString(DECIMAL_BASE) | ||
.concat("ms") | ||
.padStart(MAX_EXPECTED_LENGTH); | ||
function formatHeader() { | ||
return chalk | ||
.bold( | ||
`${pad("elapsed real")} ${pad("user")} ${pad("system")} ${pad( | ||
"∆ rss" | ||
)} ${pad("∆ heapTotal")} ${pad("∆ heapUsed")} ${pad( | ||
"∆ external" | ||
)} after step...\n` | ||
) | ||
.concat( | ||
`${`${"-".repeat(MAX_LENGTH_EXPECTED)} `.repeat(NUMBER_OF_COLUMNS)}\n` | ||
); | ||
} | ||
|
||
function formatTime(pNumber, pConversionMultiplier = MS_PER_SECOND) { | ||
return gTimeFormat(pConversionMultiplier * pNumber).padStart( | ||
MAX_LENGTH_EXPECTED | ||
); | ||
} | ||
|
||
function formatMemory(pBytes) { | ||
return Math.round(pBytes / K / K) | ||
.toString(DECIMAL_BASE) | ||
.concat("Mb") | ||
.padStart(MAX_EXPECTED_LENGTH); | ||
const lReturnValue = gSizeFormat(pBytes / K).padStart(MAX_LENGTH_EXPECTED); | ||
|
||
return pBytes < 0 ? chalk.blue(lReturnValue) : lReturnValue; | ||
} | ||
|
||
function formatPerfLine(pTime, pPreviousTime, pMessage) { | ||
return `${formatTime(pTime - pPreviousTime)} ${formatMemory( | ||
process.memoryUsage().rss | ||
)} ${formatMemory(process.memoryUsage().heapTotal)} ${formatMemory( | ||
process.memoryUsage().heapUsed | ||
)} ${formatMemory(process.memoryUsage().external)} ${pMessage}\n`; | ||
function formatPerfLine({ | ||
elapsedTime, | ||
elapsedUser, | ||
elapsedSystem, | ||
deltaRss, | ||
deltaHeapUsed, | ||
deltaHeapTotal, | ||
deltaExternal, | ||
message, | ||
}) { | ||
return `${formatTime(elapsedTime)} ${formatTime( | ||
elapsedUser, | ||
MS_PER_MICRO_SECOND | ||
)} ${formatTime(elapsedSystem, MS_PER_MICRO_SECOND)} ${formatMemory( | ||
deltaRss | ||
)} ${formatMemory(deltaHeapTotal)} ${formatMemory( | ||
deltaHeapUsed | ||
)} ${formatMemory(deltaExternal)} ${message}\n`; | ||
} | ||
|
||
module.exports = { | ||
formatTime, | ||
formatMemory, | ||
formatPerfLine, | ||
formatHeader, | ||
}; |
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
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
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
Oops, something went wrong.