[英]node.js http server how to get connections count
我使用node作为http服务器,代码如下:
http.createServer(function(req, res) {}).listen(8181);
我正在寻找一种简单的方法来监控同一进程内的节点js http服务器。 对我来说,拥有一个只能输出当前资源使用和连接数为json的函数就足够了。 目前我不需要深度测量或实时性能监控。
节点http服务器的关键性能指标是什么?是否可以从节点获取它们? 如果有,怎么样? 您如何看待愚蠢的kpi:
只需要知道我需要哪些变量/函数来获取数据?
谢谢,我非常感谢你的帮助
您可以使用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是我创建的日志记录模块。
您可以直接进入项目并获得CPU , Mem & Net开箱即用+更多其他方便的标志和指标。
检查性能监控部分。
使用:
const scribbles = require('scribbles');
scribbles.config({
dataOut:console.log
})
setInterval(function(){
scribbles.status();
}, 5000);
// This will give you a performance snapshot every 5 seconds.
你得到:
网络: 网络信息
state: 服务的状态。 例如“上升”,“阻止”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.