Skip to content

Commit

Permalink
record peak heap memory usage (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Sep 15, 2020
1 parent 48ec5e2 commit 8d30d50
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions build/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand All @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 8d30d50

Please sign in to comment.