UNPKG

@naturalcycles/nodejs-lib

Version:
84 lines 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const js_lib_1 = require("@naturalcycles/js-lib"); const os = require("os"); function memoryUsage() { const { rss, external, heapUsed, heapTotal } = process.memoryUsage(); return { rss: js_lib_1._mb(rss), heapTotal: js_lib_1._mb(heapTotal), heapUsed: js_lib_1._mb(heapUsed), external: js_lib_1._mb(external), }; } exports.memoryUsage = memoryUsage; function memoryUsageFull() { const { rss, external, heapUsed, heapTotal } = process.memoryUsage(); const totalMem = os.totalmem(); const freeMem = os.freemem(); return { rss: js_lib_1._mb(rss), heapTotal: js_lib_1._mb(heapTotal), heapUsed: js_lib_1._mb(heapUsed), external: js_lib_1._mb(external), totalMem: js_lib_1._mb(totalMem), freeMem: js_lib_1._mb(freeMem), usedMem: js_lib_1._mb(totalMem - freeMem), }; } exports.memoryUsageFull = memoryUsageFull; class ProcessSharedUtil { startMemoryTimer(ms) { console.log(memoryUsage()); this.timer = setInterval(() => { console.log(memoryUsage()); }, ms); } stopMemoryTimer(afterMs = 0) { setTimeout(() => clearInterval(this.timer), afterMs); } cpuAvg() { const avg = os.loadavg(); return { avg1: avg[0].toFixed(2), avg5: avg[1].toFixed(2), avg15: avg[2].toFixed(2), }; } cpuInfo() { const c = (os.cpus() || [undefined])[0]; return { count: os.cpus().length, model: c.model, speed: c.speed, }; } async cpuPercent(ms) { const stats1 = this.getCPUInfo(); const startIdle = stats1.idle; const startTotal = stats1.total; return new Promise(resolve => { setTimeout(() => { const stats2 = this.getCPUInfo(); const endIdle = stats2.idle; const endTotal = stats2.total; const idle = endIdle - startIdle; const total = endTotal - startTotal; const perc = idle / total; resolve(Math.round((1 - perc) * 100)); }, ms); }); } getCPUInfo() { return os.cpus().reduce((r, cpu) => { r['idle'] += cpu.times.idle; Object.values(cpu.times).forEach(m => (r['total'] += m)); return r; }, { idle: 0, total: 0, }); } } exports.processSharedUtil = new ProcessSharedUtil(); //# sourceMappingURL=process.shared.util.js.map