feat(core): add Node.js memory perf logging (#10590)

This commit is contained in:
Sébastien Lorber 2024-10-17 21:18:52 +02:00 committed by GitHub
parent 24716787d3
commit 762f7b1927
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 83 additions and 6 deletions

View file

@ -63,6 +63,11 @@ jobs:
E2E_TEST: true E2E_TEST: true
- name: Build test-website project - name: Build test-website project
run: yarn build run: yarn build
env:
# Our website should build even with limited memory
# See https://github.com/facebook/docusaurus/pull/10590
NODE_OPTIONS: '--max-old-space-size=250'
DOCUSAURUS_PERF_LOGGER: 'true'
working-directory: ../test-website working-directory: ../test-website
yarn-berry: yarn-berry:
@ -129,6 +134,11 @@ jobs:
- name: Build test-website project - name: Build test-website project
run: yarn build run: yarn build
env:
# Our website should build even with limited memory
# See https://github.com/facebook/docusaurus/pull/10590
NODE_OPTIONS: '--max-old-space-size=300'
DOCUSAURUS_PERF_LOGGER: 'true'
working-directory: ../test-website working-directory: ../test-website
npm: npm:
@ -159,6 +169,11 @@ jobs:
E2E_TEST: true E2E_TEST: true
- name: Build test-website project - name: Build test-website project
run: npm run build run: npm run build
env:
# Our website should build even with limited memory
# See https://github.com/facebook/docusaurus/pull/10590
NODE_OPTIONS: '--max-old-space-size=250'
DOCUSAURUS_PERF_LOGGER: 'true'
working-directory: ../test-website working-directory: ../test-website
pnpm: pnpm:
@ -192,4 +207,9 @@ jobs:
E2E_TEST: true E2E_TEST: true
- name: Build test-website project - name: Build test-website project
run: pnpm run build run: pnpm run build
env:
# Our website should build even with limited memory
# See https://github.com/facebook/docusaurus/pull/10590
NODE_OPTIONS: '--max-old-space-size=250'
DOCUSAURUS_PERF_LOGGER: 'true'
working-directory: ../test-website working-directory: ../test-website

View file

@ -44,6 +44,11 @@ jobs:
run: yarn workspace @docusaurus/theme-common removeThemeInternalReexport run: yarn workspace @docusaurus/theme-common removeThemeInternalReexport
- name: Docusaurus Build - name: Docusaurus Build
run: yarn build:website:fast run: yarn build:website:fast
env:
# Our website should build even with limited memory
# See https://github.com/facebook/docusaurus/pull/10590
NODE_OPTIONS: '--max-old-space-size=350'
DOCUSAURUS_PERF_LOGGER: 'true'
- name: Docusaurus site CSS order - name: Docusaurus site CSS order
run: yarn workspace website test:css-order run: yarn workspace website test:css-order

View file

@ -178,6 +178,7 @@ const logger = {
red: (msg: string | number): string => chalk.red(msg), red: (msg: string | number): string => chalk.red(msg),
yellow: (msg: string | number): string => chalk.yellow(msg), yellow: (msg: string | number): string => chalk.yellow(msg),
green: (msg: string | number): string => chalk.green(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), bold: (msg: string | number): string => chalk.bold(msg),
dim: (msg: string | number): string => chalk.dim(msg), dim: (msg: string | number): string => chalk.dim(msg),
path, path,

View file

@ -37,6 +37,11 @@ type PerfLoggerAPI = {
) => Promise<Result>; ) => Promise<Result>;
}; };
type Memory = {
before: NodeJS.MemoryUsage;
after: NodeJS.MemoryUsage;
};
function createPerfLogger(): PerfLoggerAPI { function createPerfLogger(): PerfLoggerAPI {
if (!PerfDebuggingEnabled) { if (!PerfDebuggingEnabled) {
const noop = () => {}; const noop = () => {};
@ -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) { if (duration < Thresholds.min) {
return; 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 end: PerfLoggerAPI['end'] = (label) => {
const {duration} = performance.measure(label); const {
duration,
detail: {memoryUsage},
} = performance.measure(label);
performance.clearMarks(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) => const log: PerfLoggerAPI['log'] = (label: string) =>
@ -79,9 +121,18 @@ function createPerfLogger(): PerfLoggerAPI {
const async: PerfLoggerAPI['async'] = async (label, asyncFn) => { const async: PerfLoggerAPI['async'] = async (label, asyncFn) => {
const finalLabel = applyParentPrefix(label); const finalLabel = applyParentPrefix(label);
const before = performance.now(); const before = performance.now();
const memoryBefore = process.memoryUsage();
const result = await ParentPrefix.run(finalLabel, () => asyncFn()); const result = await ParentPrefix.run(finalLabel, () => asyncFn());
const memoryAfter = process.memoryUsage();
const duration = performance.now() - before; const duration = performance.now() - before;
logDuration(finalLabel, duration); printPerfLog({
label: finalLabel,
duration,
memory: {
before: memoryBefore,
after: memoryAfter,
},
});
return result; return result;
}; };