napi-rs/memory-testing/util.js

24 lines
823 B
JavaScript
Raw Normal View History

2022-01-24 12:33:51 +09:00
const { whiteBright, red, green, gray } = require('colorette')
2021-05-28 19:50:16 +09:00
const prettyBytes = require('pretty-bytes')
const { table } = require('table')
module.exports = function displayMemoryUsageFromNode(initialMemoryUsage) {
const finalMemoryUsage = process.memoryUsage()
2022-01-24 12:33:51 +09:00
const titles = Object.keys(initialMemoryUsage).map((k) => whiteBright(k))
2021-05-28 19:50:16 +09:00
const tableData = [titles]
const diffColumn = []
for (const [key, value] of Object.entries(initialMemoryUsage)) {
const diff = finalMemoryUsage[key] - value
const prettyDiff = prettyBytes(diff, { signed: true })
if (diff > 0) {
2022-01-24 12:33:51 +09:00
diffColumn.push(red(prettyDiff))
2021-05-28 19:50:16 +09:00
} else if (diff < 0) {
2022-01-24 12:33:51 +09:00
diffColumn.push(green(prettyDiff))
2021-05-28 19:50:16 +09:00
} else {
2022-01-24 12:33:51 +09:00
diffColumn.push(gray(prettyDiff))
2021-05-28 19:50:16 +09:00
}
}
tableData.push(diffColumn)
console.info(table(tableData))
}