diff --git a/plugins/speed.js b/plugins/speed.js index 6e89a49..c77a762 100644 --- a/plugins/speed.js +++ b/plugins/speed.js @@ -1,96 +1,77 @@ import { cpus as _cpus, totalmem, freemem } from 'os' import os from 'os' -import util from 'util' +import { performance } from 'perf_hooks' import { sizeFormatter } from 'human-readable' -import { join } from 'path' -import { promises } from 'fs' -import moment from 'moment-timezone' - -const more = String.fromCharCode(8206) -const readMore = more.repeat(4001) - let format = sizeFormatter({ - std: 'JEDEC', // 'SI' (default) | 'IEC' | 'JEDEC' + std: 'JEDEC', decimalPlaces: 2, keepTrailingZeroes: false, render: (literal, symbol) => `${literal} ${symbol}B`, }) - -let handler = async (m, { conn, usedPrefix, __dirname, text, command }) => { - let date = moment.tz('Asia/Jakarta').format("dddd, Do MMMM, YYYY") - let time = moment.tz('Asia/Jakarta').format('HH:mm:ss') - let _package = JSON.parse(await promises.readFile(join(__dirname, '../package.json')).catch(_ => ({}))) || {} - let _uptime = process.uptime() * 1000 - let uptime = clockString(_uptime) - let totalreg = Object.keys(global.db.data.users).length - let rtotalreg = Object.values(global.db.data.users).filter(user => user.registered == true).length - const chats = Object.entries(conn.chats).filter(([id, data]) => id && data.isChats) - const groupsIn = chats.filter(([id]) => id.endsWith('@g.us')) //groups.filter(v => !v.read_only) +var handler = async (m, { conn, isRowner }) => { + let _muptime + if (process.send) { + process.send('uptime') + _muptime = await new Promise(resolve => { + process.once('message', resolve) + setTimeout(resolve, 1000) + }) * 1000 + } + let muptime = clockString(_muptime) const used = process.memoryUsage() - - // Hanya menggunakan satu core CPU - const cpu = _cpus()[0]; - - let start = process.hrtime(); - let speed; - let end; - let cpuUsage; - - // Mengukur waktu yang dibutuhkan untuk melakukan pengukuran - end = process.hrtime(start); - speed = Math.round((end[0] * 1000 + end[1] / 1000000)); - - // Menghitung persentase penggunaan CPU - cpuUsage = ((cpu.times.user + cpu.times.nice + cpu.times.sys + cpu.times.irq) / cpu.times.idle) * 100; - - let capti = `*Name*: ${_package.name} -🧩 *Version*: ${_package.version} -📚 *Library*: ${_package.description} - -⏳ *Uptime*:\n ${uptime} -📈 *Database*: ${totalreg} - -📅 *Date*: ${date} -⌚ *Time*: ${time} - -🖥️ SERV INFO : -⮕ *Ping*: ${speed} MS -⮕ *Hostname*: ${os.hostname()} -⮕ *Platform:* ${os.platform()} -⮕ *Ram*: ${format(totalmem() - freemem())} / ${format(totalmem())} + const cpus = _cpus().map(cpu => { + cpu.total = Object.keys(cpu.times).reduce((last, type) => last + cpu.times[type], 0) + return cpu + }) + const cpu = cpus.reduce((last, cpu, _, { length }) => { + last.total += cpu.total + last.speed += cpu.speed / length + last.times.user += cpu.times.user + last.times.nice += cpu.times.nice + last.times.sys += cpu.times.sys + last.times.idle += cpu.times.idle + last.times.irq += cpu.times.irq + return last + }, { + speed: 0, + total: 0, + times: { + user: 0, + nice: 0, + sys: 0, + idle: 0, + irq: 0 + } + }) -💬 Status : -⮕ ${groupsIn.length} - Group Chats -⮕ ${groupsIn.length} - Groups Joined -⮕ ${groupsIn.length - groupsIn.length} - Groups Left -⮕ ${chats.length - groupsIn.length} - Personal Chats -⮕ ${chats.length} - Total Chats + let old = performance.now() + await m.reply(wait) + let neww = performance.now() + let speed = neww - old + let maxim = `\`PING\` +\`\`\`${Math.round(neww - old)} ms\`\`\` +${readMore} +\`SERVER\` +\`\`\`Memory: ${format(totalmem() - freemem())} / ${format(totalmem())}\`\`\` +\`\`\`Os: ${os.platform()}\`\`\` -*NodeJS Memory Usage* -${'```' + - Object.keys(used) - .map( - (key, _, arr) => - `${key.padEnd(Math.max(...arr.map((v) => v.length)), ' ')}: ${format( - used[key] - )}` - ) - .join('\n') + - '```' - } - -*Total CPU Usage* -${cpu.model.trim()} (${cpu.speed} MHZ)\n*Usage*: ${cpuUsage.toFixed(2)}% -`.trim() +\`NODEJS MEMORY USAGE\` +${'```' + Object.keys(used).map((key, _, arr) => `${key.padEnd(Math.max(...arr.map(v => v.length)), ' ')}: ${format(used[key])}`).join('\n') + '```'} - m.reply(capti) +${cpus[0] ? `\`CPU INFO\` +*${cpus[0].model.trim()} (${cpu.speed} MHZ)*\n\`\`\`${Object.keys(cpu.times).map(type => `${(type).padEnd(6)}: ${(100 * cpu.times[type] / cpu.total).toFixed(2)}%\`\`\``).join('\n\`\`\`')} +\`CPU Core(s) Usage (${cpus.length} Core CPU)\` +${cpus.map((cpu, i) => `${i + 1}. ${cpu.model.trim()} (${cpu.speed} MHZ)\n${Object.keys(cpu.times).map(type => `- *${(type + '*').padEnd(6)}: ${(100 * cpu.times[type] / cpu.total).toFixed(2)}%`).join('\n')}`).join('\n\n')}` : ''} +` + m.reply(maxim) } handler.help = ['ping', 'speed'] -handler.tags = ['info', 'tools'] +handler.tags = ['info'] handler.command = /^(ping|speed|info)$/i + export default handler function clockString(ms) {