Skip to content

Commit

Permalink
Merge pull request #678 from TryQuiet/add/computer-usage-data-to-file…
Browse files Browse the repository at this point in the history
…-script

computer usage data script
  • Loading branch information
Dinek007 authored Aug 31, 2022
2 parents 90a77c4 + 1693600 commit 45b426f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/integration-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules
lib/
lib/
src/bot/cpuUsage.txt
src/bot/diskUsage.txt
src/bot/memoryUsage.txt
3 changes: 2 additions & 1 deletion packages/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"jest": "^27.4.5",
"prettier": "^2.1.0",
"prettier-config-standard": "^1.0.1",
"typescript": "^4.1.5"
"typescript": "^4.1.5",
"check-disk-space": "^3.3.0"
},
"dependencies": {
"@peculiar/webcrypto": "1.2.2",
Expand Down
118 changes: 118 additions & 0 deletions packages/integration-tests/src/bot/computerUsageData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
var os = require("os")
var fs = require('fs')
const checkDiskSpace = require('check-disk-space').default

let totalDiskSpace

checkDiskSpace('/').then((diskSpace) => {
totalDiskSpace= diskSpace.size
})

const totalRAM = os.totalmem()

fs.writeFile('cpuUsage.txt', '', () => { console.log('created cpu usage file') })
fs.writeFile('memoryUsage.txt', '', () => { console.log('created memory usage file') })
fs.writeFile('diskUsage.txt', '', () => { console.log('created disk space usage file') })

const cpuCoresAverage = () => {
let totalIdle = 0, totalTick = 0;
let cpus = os.cpus();

for (let i = 0, len = cpus.length; i < len; i++) {
let cpu = cpus[i]

for (type in cpu.times) {
totalTick += cpu.times[type];
}
totalIdle += cpu.times.idle;
}

return { idle: totalIdle / cpus.length, total: totalTick / cpus.length };
}

const wrtieDataToFiles = (cpuUsageSecondsList, ramUsageSecondsList, diskUsageSecondsList) => {
let secondsCpuUsageSum = 0
let secondsRAMUsageSum = 0
let secondsDiskUsageSum = 0
console.log('cpuUsageSecondsList', cpuUsageSecondsList)
for (let i = 0; i < cpuUsageSecondsList.length; i++) {
secondsCpuUsageSum += cpuUsageSecondsList[i]
}

console.log('RAMUsageSecondsList', ramUsageSecondsList)
for (let i = 0; i < ramUsageSecondsList.length; i++) {
secondsRAMUsageSum += ramUsageSecondsList[i]
}

console.log('diskUsageSecondsList', diskUsageSecondsList)
for (let i = 0; i < diskUsageSecondsList.length; i++) {
secondsDiskUsageSum += diskUsageSecondsList[i]
}

let averageOfCpuUsageSeconds = secondsCpuUsageSum / cpuUsageSecondsList.length
let averageOfRAMUsageSeconds = secondsRAMUsageSum / ramUsageSecondsList.length
let averageOfDiskUsageSeconds = secondsDiskUsageSum / diskUsageSecondsList.length
console.log(averageOfCpuUsageSeconds, '% Cpu minute')
console.log(averageOfRAMUsageSeconds, '% RAM minute')
console.log(averageOfDiskUsageSeconds, '% Disk minute')

fs.appendFile('cpuUsage.txt', `${averageOfCpuUsageSeconds}\n`, function (err) {
if (err) throw err
console.log('saved cpu usage')
})

fs.appendFile('memoryUsage.txt', `${averageOfRAMUsageSeconds}\n`, function (err) {
if (err) throw err
console.log('saved memory usage')
})

fs.appendFile('diskUsage.txt', `${averageOfDiskUsageSeconds}\n`, function (err) {
if (err) throw err
console.log('saved disk usage')
})
}

let measurementStart = cpuCoresAverage()
let cpuUsageSecondsList = []
let ramUsageSecondsList = []
let diskUsageSecondsList = []

const numberOfAvarageSeconds = 60
let seconds = 0
let minutes = 1

setInterval(() => {
let measurementEnd = cpuCoresAverage()

let idleDifference = measurementEnd.idle - measurementStart.idle
let totalDifference = measurementEnd.total - measurementStart.total
let percentageCPU = 100 - ~~(100 * idleDifference / totalDifference)
cpuUsageSecondsList.push(percentageCPU)
console.log(percentageCPU + " % CPU usage second")

let freeRAM = os.freemem()
let usedRAM = totalRAM - freeRAM
let percentageRAM = (100 * usedRAM) / totalRAM
ramUsageSecondsList.push(percentageRAM)
console.log(percentageRAM + " % RAM usage in second")

checkDiskSpace('/').then((diskSpace) => {
let freeDiskSpace = diskSpace.free
let usedDiskSpace = totalDiskSpace - freeDiskSpace
percentageUsedDiskSpace = (100 * usedDiskSpace) / totalDiskSpace
diskUsageSecondsList.push(percentageUsedDiskSpace)
console.log(percentageUsedDiskSpace + " % Disk space usage in second")
})

if (seconds == numberOfAvarageSeconds) {
wrtieDataToFiles(cpuUsageSecondsList, ramUsageSecondsList, diskUsageSecondsList)
cpuUsageSecondsList = []
ramUsageSecondsList = []
diskUsageSecondsList = []
seconds = 0
minutes++
}

measurementStart = cpuCoresAverage();
seconds++
}, 1000)

0 comments on commit 45b426f

Please sign in to comment.