Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(performance-log): improves readability of code #708

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 27 additions & 22 deletions src/cli/listeners/performance-log/format-helpers.js
Expand Up @@ -7,10 +7,9 @@ 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.
* it will fall back to the 'current' locale. Using a non-existent language
* (e.g. `zz`) also works, but `undefined` seems to be the lesser of the two
* evil as it is closer to the intent (skip the optional parameter).
*/
// eslint-disable-next-line no-undefined
const LOCALE = undefined;
Expand All @@ -29,32 +28,36 @@ const gSizeFormat = new Intl.NumberFormat(LOCALE, {
maximumFractionDigits: 0,
}).format;

const pad = (pString) => pString.padStart(MAX_LENGTH_EXPECTED);
const pad = (pString) => pString.padStart(MAX_LENGTH_EXPECTED).concat(" ");

function formatHeader() {
return chalk
.bold(
`${pad("elapsed real")} ${pad("user")} ${pad("system")} ${pad(
"∆ rss"
)} ${pad("∆ heapTotal")} ${pad("∆ heapUsed")} ${pad(
"∆ external"
)} after step...\n`
`${
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
);
return gTimeFormat(pConversionMultiplier * pNumber)
.padStart(MAX_LENGTH_EXPECTED)
.concat(" ");
}

function formatMemory(pBytes) {
const lReturnValue = gSizeFormat(pBytes / K).padStart(MAX_LENGTH_EXPECTED);

return pBytes < 0 ? chalk.blue(lReturnValue) : lReturnValue;
return (pBytes < 0 ? chalk.blue(lReturnValue) : lReturnValue).concat(" ");
}

function formatPerfLine({
Expand All @@ -67,14 +70,16 @@ function formatPerfLine({
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`;
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 = {
Expand Down
26 changes: 14 additions & 12 deletions test/cli/listeners/performance-log-listener/format-helpers.spec.mjs
Expand Up @@ -20,27 +20,27 @@ import formatHelpers from "../../../../src/cli/listeners/performance-log/format-

describe("[U] cli/listeners/performance-log/format-helpers - formatTime", () => {
it("converts to ms, left pads & adds the unit at the end", () => {
expect(formatHelpers.formatTime(14.88041018)).to.equal(" 14,880ms");
expect(formatHelpers.formatTime(14.88041018)).to.equal(" 14,880ms ");
});

it("converts to ms, left pads & adds the unit at the end (0)", () => {
expect(formatHelpers.formatTime(0)).to.equal(" 0ms");
expect(formatHelpers.formatTime(0)).to.equal(" 0ms ");
});

it("converts to ms, left pads & adds the unit at the end (negative numbers)", () => {
expect(formatHelpers.formatTime(-3.1415926535)).to.equal(" -3,142ms");
expect(formatHelpers.formatTime(-3.1415926535)).to.equal(" -3,142ms ");
});

it("converts to ms, left pads & adds the unit at the end (null treatment => 0)", () => {
expect(formatHelpers.formatTime(null)).to.equal(" 0ms");
expect(formatHelpers.formatTime(null)).to.equal(" 0ms ");
});

it("converts to ms, left pads & adds the unit at the end (undefined treatment => NaN)", () => {
expect(formatHelpers.formatTime()).to.equal(" NaNms");
expect(formatHelpers.formatTime()).to.equal(" NaNms ");
});

it("converts to ms, left pads & adds the unit at the end (non-number treatment => NaN)", () => {
expect(formatHelpers.formatTime("not a number")).to.equal(" NaNms");
expect(formatHelpers.formatTime("not a number")).to.equal(" NaNms ");
});
});

Expand All @@ -56,27 +56,29 @@ describe("[U] cli/listeners/performance-log/format-helpers - formatMemory", () =
});

it("converts to kB, left pads & adds the unit at the end", () => {
expect(formatHelpers.formatMemory(4033856)).to.equal(" +3,939kB");
expect(formatHelpers.formatMemory(4033856)).to.equal(" +3,939kB ");
});

it("converts to kB, left pads & adds the unit at the end (0)", () => {
expect(formatHelpers.formatMemory(0)).to.equal(" 0kB");
expect(formatHelpers.formatMemory(0)).to.equal(" 0kB ");
});

it("converts to kB, left pads & adds the unit at the end (negative numbers)", () => {
expect(formatHelpers.formatMemory(-403385623)).to.equal(" -393,931kB");
expect(formatHelpers.formatMemory(-403385623)).to.equal(" -393,931kB ");
});

it("converts to kB, left pads & adds the unit at the end (null)", () => {
expect(formatHelpers.formatMemory(0)).to.equal(" 0kB");
expect(formatHelpers.formatMemory(0)).to.equal(" 0kB ");
});

it("converts to kB, left pads & adds the unit at the end (undefined)", () => {
expect(formatHelpers.formatMemory()).to.equal(" NaNkB");
expect(formatHelpers.formatMemory()).to.equal(" NaNkB ");
});

it("converts to kB, left pads & adds the unit at the end (not a number)", () => {
expect(formatHelpers.formatMemory("not a number")).to.equal(" NaNkB");
expect(formatHelpers.formatMemory("not a number")).to.equal(
" NaNkB "
);
});
});

Expand Down