
讀了 os 模塊的檔案,研究了幾個有意思的問題:
?? 識別作業系統平臺 ?? 理解和計算“平均負載” ?? 理解和計算“cpu 使用率” ?? 理解和計算“記憶體使用率” ?? 查看運行時間
識別作業系統平臺
nodejs 提供了os.platform()和os.type(),可以用來識別作業系統平臺,推薦使用: os.platform()
理解和計算“平均負載”
平均負載是指:單位時間內,系統處于可運行狀態和不可中斷狀態的平均行程數,它和 cpu 使用率沒有直接關系,
其中,這里的可運行狀態指的是:正在使用 cpu 或正在等待 cpu 的行程,不可中斷狀態指的是:內核態關鍵流程中的行程,
在 nodejs 中,直接呼叫os.loadavg()可以獲得 1、5 和 15 分鐘的平均負載,它和 unix 命令uptime回傳值一樣,
為什么需要關心平均負載這個問題呢?因為行程分為 2 種,第一種就是“CPU 密集型”,它的 cpu 使用率和平均負載都是高的;第二種是“IO 密集型”,它的 cpu 使用率不一定高,但是等待 IO 會造成平均負載高,所以,cpu 使用率和平均負載共同反應系統性能,
平均活躍行程數最理想的狀態是 cpu 數量=平均負載,如果 cpu 數量 < 平均負載,那么平均負載過高,
// 判斷是否平均負載過高
function isHighLoad() {
const cpuNum = os.cpus().length;
return os.loadavg().map(item => item > cpuNum);
}
理解和計算“cpu 使用率”
很多監控軟體都提供針對 cpu 使用率的“實時”監控,當然這個實時不是真的實時,有個時間差,這個功能,nodejs 如何實作呢?
第一步:封裝getCPUInfo(),計算獲取 cpu 花費的總時間與空閑模式花費的時間,
/** * 獲取cpu花費的總時間與空閑模式的時間 */ function getCPUInfo() { const cpus = os.cpus(); let user = 0, nice = 0, sys = 0, idle = 0, irq = 0, total = 0;cpus.forEach(<span style="line-height: 26px;"><span style="line-height: 26px;">cpu</span> =></span> { <span style="color: #333; font-weight: bold; line-height: 26px;">const</span> { times } = cpu; user += times.user; nice += times.nice; sys += times.sys; idle += times.idle; irq += times.irq; }); total = user + nice + sys + idle + irq; <span style="color: #333; font-weight: bold; line-height: 26px;">return</span> { total, idle };
}
第二步:當前時間點 t1,選定一個時間差 intervel,計算 t1 和 t1 + interval 這兩個時間點的 cpu 時間差與空閑模式時間差,回傳 1 - 空閑時間差 / cpu時間差,回傳的結果就是時間差 intervel 內的平均 cpu 使用率,
function getCPUUsage(interval = 1000) { const startInfo = getCPUInfo();<span style="color: #333; font-weight: bold; line-height: 26px;">return</span> <span style="color: #333; font-weight: bold; line-height: 26px;">new</span> <span style="color: #0086b3; line-height: 26px;">Promise</span>(<span style="line-height: 26px;"><span style="line-height: 26px;">resolve</span> =></span> { setTimeout(<span style="line-height: 26px;"><span style="line-height: 26px;">()</span> =></span> { <span style="color: #333; font-weight: bold; line-height: 26px;">const</span> endInfo = getCPUInfo(); <span style="color: #333; font-weight: bold; line-height: 26px;">const</span> idleDiff = startInfo.idle - endInfo.idle; <span style="color: #333; font-weight: bold; line-height: 26px;">const</span> totalDiff = startInfo.total - endInfo.total; resolve(<span style="color: #008080; line-height: 26px;">1</span> - <span style="color: #0086b3; line-height: 26px;">Math</span>.abs(idleDiff / totalDiff)); }, interval); });
}
使用方式如下:
getCPUUsage().then(usage => console.log("cpu使用率:", usage));
理解和計算“記憶體使用率”
cpu 的指標有平均負載、cpu 使用率,記憶體的指標有記憶體使用率,
借助 nodejs 介面,實作非常簡單:
function getMemUsage() {
return 1 - os.freemem() / os.totalmem();
}
查看運行時間
nodejs 運行時間: process.uptime()系統運行時間: os.uptime()
參考鏈接
Node.js os doc 第三方擴展庫:os-utils 怎么理解平均負載(一) 位元組序(大小端)理解
放在最后
覺得不錯,幫忙點個推薦唄,您的支持是對我最大的激勵 歡迎我的公眾號:「心譚博客」,只專注于前端 + 演算法的原創分享
由于個人精力有限,很多系列和歷史文章沒有即時同步,請前往「前端圖譜」&「演算法題解」,保證您有所識訓,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/151741.html
標籤:JavaScript
上一篇:JS陣列冒泡排序
下一篇:vue(四)--屬性系結
