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

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

Merged
merged 6 commits into from
Oct 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/tests-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ jobs:
E2E_TEST: true
- name: Build test-website project
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

yarn-berry:
Expand Down Expand Up @@ -129,6 +134,11 @@ jobs:

- name: Build test-website project
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

npm:
Expand Down Expand Up @@ -159,6 +169,11 @@ jobs:
E2E_TEST: true
- name: Build test-website project
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

pnpm:
Expand Down Expand Up @@ -192,4 +207,9 @@ jobs:
E2E_TEST: true
- name: Build test-website project
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
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ jobs:
run: yarn workspace @docusaurus/theme-common removeThemeInternalReexport
- name: Docusaurus Build
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
run: yarn workspace website test:css-order

Expand Down
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