繁体   English   中英

node.js http服务器如何获取连接数

[英]node.js http server how to get connections count

我使用node作为http服务器,代码如下:

http.createServer(function(req, res) {}).listen(8181);

我正在寻找一种简单的方法来监控同一进程内的节点js http服务器。 对我来说,拥有一个只能输出当前资源使用和连接数为json的函数就足够了。 目前我不需要深度测量或实时性能监控。

节点http服务器的关键性能指标是什么?是否可以从节点获取它们? 如果有,怎么样? 您如何看待愚蠢的kpi:

  1. 连接数
  2. CPU使用率
  3. 拉姆用法

只需要知道我需要哪些变量/函数来获取数据?

谢谢,我非常感谢你的帮助

您可以使用NodeJS的内置函数获取连接量。 检查getConnections Bellow示例如何使用它:

var server = http.createServer(app);    

server.getConnections(function(error, count) {

    console.log(count);

});

我希望这就是你要找的:)

你需要这样的东西:

var count = 0;

http.createServer(function(req, res) {
    count++;
    res.on('finish', function () {
        //setTimeout(function () {
        count--;
        //}, 60000);
    }).on('close', function () {
        count--;
    });
}).listen(8181);

使用setTimeout()您可以在最后1分钟内获得活动连接。

有关CPU使用情况,请参阅http://nodejs.org/api/os.html#os_os_cpus

有关内存使用情况,请参阅http://nodejs.org/api/process.html#process_process_memoryusage

scribbles是我创建的日志记录模块。

您可以直接进入项目并获得CPUMemNet开箱即用+更多其他方便的标志和指标。

检查性能监控部分。

使用:

const scribbles = require('scribbles');

scribbles.config({
   dataOut:console.log
})

setInterval(function(){
  scribbles.status();
}, 5000);

// This will give you a performance snapshot every 5 seconds.

你得到:

  • 网络: 网络信息

    • port: 收听此端口
    • connections: 当前建立的连接数
  • state: 服务的状态。 例如“上升”,“阻止”

  • cpu: CPU信息
    • 核心: 可用核心数量
    • model: 处理器的描述
    • 速度: MHz频率
    • percUsed: 以百分比形式加载进程
    • percFree: 按流程可用百分比
  • sys: 系统信息
    • startedAt: 当它的系统启动时
    • arch: 平台架构。 例如“x64”
    • 平台: 操作系统平台
    • totalMem: 正在使用的内存总兆字节数
    • freeMem: 可用内存的总兆字节数
    • usedMem: 正在使用的内存总兆字节数
  • 处理:
    • percUsedCpu: 此进程使用的处理能力百分比
    • percFreeMem: 此进程使用的内存百分比
    • usedMem: 此进程使用的内存总兆字节数
    • startedAt: 当它的进程开始时
    • pTitle: 当前进程标题(即返回ps的当前值)
    • pid: 进程的ID
    • ppid: 当前父进程的ID
    • user: node启动节点的用户的名称
    • vNode: 节点的版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM