1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.processSharedUtil = exports.memoryUsageFull = exports.memoryUsage = void 0;
|
4 | const js_lib_1 = require("@naturalcycles/js-lib");
|
5 | const os = require("os");
|
6 |
|
7 | function memoryUsage() {
|
8 | const { rss, external, heapUsed, heapTotal } = process.memoryUsage();
|
9 | return {
|
10 | rss: (0, js_lib_1._mb)(rss),
|
11 | heapTotal: (0, js_lib_1._mb)(heapTotal),
|
12 | heapUsed: (0, js_lib_1._mb)(heapUsed),
|
13 | external: (0, js_lib_1._mb)(external),
|
14 | };
|
15 | }
|
16 | exports.memoryUsage = memoryUsage;
|
17 | function memoryUsageFull() {
|
18 | const { rss, external, heapUsed, heapTotal } = process.memoryUsage();
|
19 | const totalMem = os.totalmem();
|
20 | const freeMem = os.freemem();
|
21 | return {
|
22 | rss: (0, js_lib_1._mb)(rss),
|
23 | heapTotal: (0, js_lib_1._mb)(heapTotal),
|
24 | heapUsed: (0, js_lib_1._mb)(heapUsed),
|
25 | external: (0, js_lib_1._mb)(external),
|
26 | totalMem: (0, js_lib_1._mb)(totalMem),
|
27 | freeMem: (0, js_lib_1._mb)(freeMem),
|
28 | usedMem: (0, js_lib_1._mb)(totalMem - freeMem),
|
29 | };
|
30 | }
|
31 | exports.memoryUsageFull = memoryUsageFull;
|
32 | class ProcessUtil {
|
33 | startMemoryTimer(intervalMillis = 1000) {
|
34 | console.log(memoryUsage());
|
35 | this.timer = setInterval(() => {
|
36 | console.log(memoryUsage());
|
37 | }, intervalMillis);
|
38 | }
|
39 | stopMemoryTimer(afterMillis = 0) {
|
40 | setTimeout(() => clearInterval(this.timer), afterMillis);
|
41 | }
|
42 | cpuAvg() {
|
43 | const avg = os.loadavg();
|
44 | return {
|
45 | avg1: avg[0].toFixed(2),
|
46 | avg5: avg[1].toFixed(2),
|
47 | avg15: avg[2].toFixed(2),
|
48 | };
|
49 | }
|
50 | cpuInfo() {
|
51 | const c = os.cpus()[0];
|
52 | return {
|
53 | count: os.cpus().length,
|
54 | model: c.model,
|
55 | speed: c.speed,
|
56 | };
|
57 | }
|
58 | async cpuPercent(ms) {
|
59 | const stats1 = this.getCPUInfo();
|
60 | const startIdle = stats1.idle;
|
61 | const startTotal = stats1.total;
|
62 | return new Promise(resolve => {
|
63 | setTimeout(() => {
|
64 | const stats2 = this.getCPUInfo();
|
65 | const endIdle = stats2.idle;
|
66 | const endTotal = stats2.total;
|
67 | const idle = endIdle - startIdle;
|
68 | const total = endTotal - startTotal;
|
69 | const perc = idle / total;
|
70 | resolve(Math.round((1 - perc) * 100));
|
71 | }, ms);
|
72 | });
|
73 | }
|
74 | getCPUInfo() {
|
75 |
|
76 | return os.cpus().reduce((r, cpu) => {
|
77 | r['idle'] += cpu.times.idle;
|
78 | Object.values(cpu.times).forEach(m => (r['total'] += m));
|
79 | return r;
|
80 | }, {
|
81 | idle: 0,
|
82 | total: 0,
|
83 | });
|
84 | }
|
85 | }
|
86 | exports.processSharedUtil = new ProcessUtil();
|