Skip to content

Commit

Permalink
add head memory to perf logging
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Oct 17, 2024
1 parent 2471678 commit eebcb64
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const logger = {
red: (msg: string | number): string => chalk.red(msg),
yellow: (msg: string | number): string => chalk.yellow(msg),
green: (msg: string | number): string => chalk.green(msg),
cyan: (msg: string | number): string => chalk.cyan(msg),
bold: (msg: string | number): string => chalk.bold(msg),
dim: (msg: string | number): string => chalk.dim(msg),
path,
Expand Down
63 changes: 57 additions & 6 deletions packages/docusaurus-logger/src/perfLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ type PerfLoggerAPI = {
) => Promise<Result>;
};

type Memory = {
before: NodeJS.MemoryUsage;
after: NodeJS.MemoryUsage;
};

function createPerfLogger(): PerfLoggerAPI {
if (!PerfDebuggingEnabled) {
const noop = () => {};
Expand All @@ -58,19 +63,56 @@ function createPerfLogger(): PerfLoggerAPI {
}
};

const logDuration = (label: string, duration: number) => {
const formatMemory = (memory: Memory): string => {
const fmtHead = (bytes: number) =>
logger.cyan(`${(bytes / 1000000).toFixed(0)}mb`);
return logger.dim(
`(${fmtHead(memory.before.heapUsed)} -> ${fmtHead(
memory.after.heapUsed,
)})`,
);
};

const printPerfLog = ({
label,
duration,
memory,
}: {
label: string;
duration: number;
memory: Memory;
}) => {
if (duration < Thresholds.min) {
return;
}
console.log(`${PerfPrefix + label} - ${formatDuration(duration)}`);
console.log(
`${PerfPrefix + label} - ${formatDuration(duration)} - ${formatMemory(
memory,
)}`,
);
};

const start: PerfLoggerAPI['start'] = (label) => performance.mark(label);
const start: PerfLoggerAPI['start'] = (label) =>
performance.mark(label, {
detail: {
memoryUsage: process.memoryUsage(),
},
});

const end: PerfLoggerAPI['end'] = (label) => {
const {duration} = performance.measure(label);
const {
duration,
detail: {memoryUsage},
} = performance.measure(label);
performance.clearMarks(label);
logDuration(applyParentPrefix(label), duration);
printPerfLog({
label: applyParentPrefix(label),
duration,
memory: {
before: memoryUsage,
after: process.memoryUsage(),
},
});
};

const log: PerfLoggerAPI['log'] = (label: string) =>
Expand All @@ -79,9 +121,18 @@ function createPerfLogger(): PerfLoggerAPI {
const async: PerfLoggerAPI['async'] = async (label, asyncFn) => {
const finalLabel = applyParentPrefix(label);
const before = performance.now();
const memoryBefore = process.memoryUsage();
const result = await ParentPrefix.run(finalLabel, () => asyncFn());
const memoryAfter = process.memoryUsage();
const duration = performance.now() - before;
logDuration(finalLabel, duration);
printPerfLog({
label: finalLabel,
duration,
memory: {
before: memoryBefore,
after: memoryAfter,
},
});
return result;
};

Expand Down

0 comments on commit eebcb64

Please sign in to comment.