-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.js
79 lines (71 loc) · 2.46 KB
/
exec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const appRoot = require('app-root-path')
const spawn = require('child_process').spawn
const platform = require('./platform')
const path = require('path')
const osu = require('node-os-utils')
const os = require('os')
const rootPath = path.resolve(__dirname)
const RESOURCE_DIR = 'resources'
const PLATFORM = platform.get()
const STRESS = platform.stress()
const STRESS_EXEC_PATH = path.join(rootPath, RESOURCE_DIR, PLATFORM, STRESS)
// const STRESS_SCALER = 6
// Run cpu stress executable
const stress = (covid) => {
return new Promise(function (resolve, reject) {
let cpuExecutable;
const infectionPercent = `${Math.min(Math.round(Math.max(covid.infectionsScaled, 0)), 100)}`
if (PLATFORM == 'win')
cpuExecutable = spawn('powershell.exe', ['WindowsStress.exe', infectionPercent, 'all']);
else
cpuExecutable = spawn(STRESS_EXEC_PATH, ['-c', '0', '-l', infectionPercent]);
console.log(`[CPU STRESS]: ${Math.max(covid.infectionsScaled)}%`)
cpuExecutable.stdout.on('data', data => {
console.log(`stdout: ${data}`);
resolve(data)
});
cpuExecutable.stderr.on('data', data => {
console.log(`stderr: ${data}`);
resolve(reject)
});
});
}
// Kill all stress executable instances
const kill = () => {
return new Promise(function (resolve, reject) {
let child;
if (PLATFORM == 'win')
child = spawn('powershell.exe', ['Taskkill', '/IM', 'WindowsStress.exe', '/F'])
else
child = spawn('killall', [`${platform.stress()}`])
child.stdout.on('data', data => {
console.log(`stdout: ${data}`)
resolve(data)
});
child.stderr.on('data', data => {
console.log(`stderr: ${data}`)
resolve(reject)
});
});
}
const usage = () => {
return new Promise(function (resolve, reject) {
const cpu = osu.cpu
// const count = cpu.count()
const mem = osu.mem
const memUsage = mem.info()
.then(memInfo => {
return { "memory": memInfo.freeMemPercentage }
})
const cpuUsage = cpu.usage()
.then(cpuPercentage => {
return { "cpu": cpuPercentage }
})
Promise.all([cpuUsage, memUsage]).then(stats => {
resolve(Object.assign({}, ...stats))
})
})
}
exports.stress = stress
exports.kill = kill
exports.usage = usage