读了 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 class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">cpu</span> =&gt;</span> {
<span class="hljs-keyword" 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 class="hljs-keyword" 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 class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">new</span> <span class="hljs-built_in" style="color: #0086b3; line-height: 26px;">Promise</span>(<span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">resolve</span> =&gt;</span> {
setTimeout(<span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">()</span> =&gt;</span> {
<span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">const</span> endInfo = getCPUInfo(); <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">const</span> idleDiff = startInfo.idle - endInfo.idle;
<span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">const</span> totalDiff = startInfo.total - endInfo.total;
resolve(<span class="hljs-number" style="color: #008080; line-height: 26px;">1</span> - <span class="hljs-built_in" 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()

参考链接

放在最后

  1. 觉得不错,帮忙点个推荐呗,您的支持是对我最大的激励
  2. 欢迎我的公众号:「心谭博客」,只专注于前端 + 算法的原创分享

由于个人精力有限,很多系列和历史文章没有即时同步,请前往「前端图谱」&「算法题解」,保证您有所收获。

最新文章

  1. jface databinding:部分实现POJO对象的监测
  2. shell crontab执行结果不同问题处理
  3. 提升效率(时间准确性),减少时间和资源的消耗——由89C52/89C51的定时器中断引出的一些问题
  4. 孙鑫VC学习笔记:多线程编程
  5. Android开发常见问题系列之一:eclipse中adb.exe启动失败或者无法启动
  6. 跟我学STL系列(1)——STL入门介绍
  7. maven添加远程私服
  8. Ubuntu 上安装 Freemind 并支持中文
  9. ubuntn14.04 32位安装hadoop2.7.2
  10. Javascript数据类型之Undefined和null
  11. AFNetworking速成教程
  12. input输入密码变黑点密文
  13. PCB使用技巧
  14. Android退出程序
  15. 《Intel汇编第5版》 Intel CPU小端序
  16. SQL一次查出相关类容避免长时间占用表(下)
  17. Shuffle 的 5步
  18. @Controller注解
  19. Java复习总结——继承
  20. RabbitMQ 消息确认机制以及lazy queue+ disk消息持久化

热门文章

  1. Web中的通配符
  2. 10款Web前端工具
  3. C#调用smtp邮件发送几个大坑
  4. 使用FluentEmail发送outlook邮件
  5. 由“Sysnative”引发的思考
  6. python3中map函数
  7. Perl 的内置变量$|
  8. 仿快播APP源码
  9. 数据库基础之Mysql
  10. react-native-vector-icons 安装、使用