javascript node.js http 服务器如何获取连接数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25012185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js http server how to get connections count
提问by Manuel
I'm using node as a http server with the following code:
我使用 node 作为 http 服务器,代码如下:
http.createServer(function(req, res) {}).listen(8181);
I'm looking for a simple way to monitor a node js http server from within the same process. For me it would be enough to have a own function which just outputs the current resource usage and connection count as json. For now I don't need deep measuring or real time performance monitoring.
我正在寻找一种简单的方法来从同一进程中监视节点 js http 服务器。对我来说,拥有一个自己的函数就足够了,该函数仅将当前资源使用情况和连接计数输出为 json。目前我不需要深度测量或实时性能监控。
What are the key performance indicators for a node http server and is it possible to get them from node? If yes how? What you think of the folling kpi's:
节点 http 服务器的关键性能指标是什么,是否可以从节点获取它们?如果是如何?您对以下 kpi 的看法:
- Connection Count
- CPU Usage
- Ram Usage
- 连接数
- CPU使用率
- 内存使用
Just need to know which variables/functions I need to get the data?
只需要知道我需要哪些变量/函数来获取数据?
Thx I really appreciate your help
Thx 我真的很感谢你的帮助
回答by David Gatti
You can get the amount of connection using a built in function of NodeJS. Check getConnections. Bellow an example how to use it:
您可以使用 NodeJS 的内置函数获取连接量。检查getConnections。下面是一个如何使用它的例子:
var server = http.createServer(app);
server.getConnections(function(error, count) {
console.log(count);
});
I hope this is what you were looking for :)
我希望这就是你要找的:)
回答by micnic
You need something like this:
你需要这样的东西:
var count = 0;
http.createServer(function(req, res) {
count++;
res.on('finish', function () {
//setTimeout(function () {
count--;
//}, 60000);
}).on('close', function () {
count--;
});
}).listen(8181);
With setTimeout()
you can get the active connections in the last 1 minute.
有了setTimeout()
你可以在最后1分钟的活动连接。
see http://nodejs.org/api/os.html#os_os_cpusfor CPU usage
有关 CPU 使用情况,请参阅http://nodejs.org/api/os.html#os_os_cpus
see http://nodejs.org/api/process.html#process_process_memoryusagefor memory usage
有关内存使用情况,请参阅http://nodejs.org/api/process.html#process_process_memoryusage
回答by codemeasandwich
scribblesis a logging module I created.
scribbles是我创建的日志记录模块。
You can just drop in you project and get CPU, Mem& Netout of the box + more some other handy flags & metrics.
您可以直接加入您的项目并立即获得CPU、Mem和Net以及其他一些方便的标志和指标。
Checkout the performance-monitoringpart.
查看性能监控部分。
To use:
使用:
const scribbles = require('scribbles');
scribbles.config({
dataOut:console.log
})
setInterval(function(){
scribbles.status();
}, 5000);
// This will give you a performance snapshot every 5 seconds.
You get:
你得到:
network: Networking info
- port: listening on this Port
- connections: number of current established connections
state: the state of the services. e.g. "up", "blocking"
- cpu: CPU info
- cores: number of available cores
- model: description of the processor
- speed: MHz frequency speed
- percUsed: load on process as percentage
- percFree: available on process as percentage
- sys: System info
- startedAt: when it's system was started
- arch: platform architecture. e.g "x64"
- platform: the operating system platform
- totalMem: the total megabytes of memory being used
- freeMem: the total megabytes of memory free
- usedMem: the total megabytes of memory being used
- process:
- percUsedCpu: the percentage of processing power being used by this process
- percFreeMem: the percentage of memory being used by this process
- usedMem: the total megabytes of memory being used by this process
- startedAt: when it's process was started
- pTitle: the current process title (i.e. returns the current value of ps)
- pid: the ID of the process
- ppid: the ID of the current parent process
- user: node the name of the user who started node
- vNode: version of node
网络:网络信息
- 端口:监听这个端口
- 连接数:当前建立的连接数
状态:服务的状态。例如“向上”、“阻塞”
- cpu:CPU信息
- 核心数:可用核心数
- 型号:处理器说明
- 速度:MHz频率速度
- percUsed:进程负载百分比
- percFree:在进程中可用的百分比
- sys:系统信息
- startAt:系统启动的时间
- arch:平台架构。例如“x64”
- 平台:操作系统平台
- totalMem:正在使用的总内存兆字节
- freeMem:可用内存的总兆字节数
- usedMem:正在使用的总内存兆字节
- 过程:
- percUsedCpu:此进程使用的处理能力百分比
- percFreeMem:此进程使用的内存百分比
- usedMem:此进程使用的总内存兆字节
- startAt:进程开始的时间
- pTitle:当前进程标题(即返回ps的当前值)
- pid:进程ID
- ppid:当前父进程的ID
- user: node 启动节点的用户名
- vNode:节点的版本