From 8d30d504f8dafa1bcbb20b3f6ff143cd9e9b3906 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 15 Sep 2020 16:05:40 -0400 Subject: [PATCH] record peak heap memory usage (#1266) --- build/cli.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/build/cli.js b/build/cli.js index bbc26e4507be..1745e4db0228 100644 --- a/build/cli.js +++ b/build/cli.js @@ -50,6 +50,8 @@ async function buildDocuments() { throw new Error("No documents to build found"); } + let peakHeapBytes = 0; + !options.noProgressbar && progressBar.start(documents.count); for (const document of documents.iter()) { const outPath = path.join(BUILD_OUT_ROOT, slugToFolder(document.url)); @@ -99,6 +101,10 @@ async function buildDocuments() { } else { console.log(outPath); } + const heapBytes = process.memoryUsage().heapUsed; + if (heapBytes > peakHeapBytes) { + peakHeapBytes = heapBytes; + } } !options.noProgressbar && progressBar.stop(); @@ -123,13 +129,22 @@ async function buildDocuments() { JSON.stringify(items) ); } - return slugPerLocale; + return { slugPerLocale, peakHeapBytes }; +} + +function humanFileSize(size) { + if (size < 1024) return size + " B"; + let i = Math.floor(Math.log(size) / Math.log(1024)); + let num = size / Math.pow(1024, i); + let round = Math.round(num); + num = round < 10 ? num.toFixed(2) : round < 100 ? num.toFixed(1) : round; + return `${num} ${"KMGTPEZY"[i - 1]}B`; } if (require.main === module) { const t0 = new Date(); buildDocuments() - .then((slugPerLocale) => { + .then(({ slugPerLocale, peakHeapBytes }) => { const t1 = new Date(); const count = Object.values(slugPerLocale).reduce( (a, b) => a + b.length, @@ -145,6 +160,7 @@ if (require.main === module) { count / seconds ).toFixed(1)} documents per second.` ); + console.log(`Peak heap memory usage: ${humanFileSize(peakHeapBytes)}`); }) .catch((error) => { console.error("error while building documents:", error);