Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1729559
  • 博文数量: 150
  • 博客积分: 660
  • 博客等级: 上士
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 11:39
文章分类

全部博文(150)

文章存档

2019年(4)

2018年(36)

2017年(53)

2016年(7)

2015年(3)

2014年(3)

2013年(27)

2012年(2)

2011年(1)

2006年(1)

2005年(13)

分类: 敏捷开发

2014-06-27 14:50:43

从3月份开始接手公司的一个nodejs项目,并开始学习,在此记录学习中遇到的问题及解决思路或者方法。

nodejs作为web服务的情况相当普遍,当前如何获取nodejs的当前信息,介绍下自己的一点想法和实践。

1、nodejs 的cpu、内存、版本和操作系统等信息

    这部分相对简单,可以直接通过nodejs内置的模块获取

    os模块:可以获取cpu个数、内存大小,操作系统等信息,通过require('os')即可。
    process模块:可以获取nodejs当前使用的内存信息,process是全局变量,在任意地方可以直接使用,例如

       console.log('worker: ' + worker.process.pid + ' memory usage: ' + util.inspect(process.memoryUsage(worker.process.pid))); //获取当前node进程的pid,内存信息。
       console.log( 'nodejs version: ' + process.version); //获取nodejs版本号

2、nodejs的网络连接信息

    如果是httpserver 的话,连接信息有两个,一个客户端连入连接,和server连接后端服务的连接(作为proxy的情况)

2、1 连入连接数

    模块:net
    函数:server.getConnections(callback)
     用法:testConn.js


var http = require("http");
var express = require('express');                              
var app  = express();
server = http.createServer(app);      
// 中间省略express路由等代码 
function nodeStatus(req,res,next) {
  server.getConnections(function(err,num) {
    //console.log('all : ' + num);
    res.send('connections:' + num + '\n' );
  });
}

   2、2 连出连接数

    模块:http
    函数或类: http.agent   、 http.globalAgent
    属性:agent.maxSockets  //最大连接数
             agent.sockets        //连接数数组
             agent.requests      //等待队列
   用法:testAgent.js

var http = require("http");
var express = require('express');
var app  = express();
server = http.createServer(app);
server.listen(9000);
var agent = http.globalAgent;
agent.maxSockets = 10 ;        // 超过10后,queue开始出现


// 每3秒访问一遍
setInterval(function() {




http.get("", function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});


http.get("", function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});
}, 3000);




//每两秒打印一次连接数和队列,可以看到连接数达到10以后,队列开始排队
setInterval(function() {


  for(i in agent.sockets) {
    console.log('Connections: ' + i + ' : ' + agent.sockets[i].length);
  }
  for(i in agent.requests) {
    console.log('Queue: ' + i + ' : ' + agent.requests[i].length);
  }
}, 2000);

在linux下 可以运行 node testAgent.js ,屏幕输出如下,可以看到当前的发起连接数和等待队列。

onnections: : 5
Connections: : 5
Connections: : 5
Connections: : 5
Got response: 200
Got response: 200
Connections: : 6
Connections: : 6
Got response: 200
Got response: 200
Connections: : 7
Connections: : 7
Connections: : 7
Connections: : 7
Got response: 200
Got response: 200
Connections: : 8
Connections: : 8
Got response: 200
Got response: 200
Connections: : 9
Connections: : 9
Connections: : 9
Connections: : 9
Got response: 200
Got response: 200
Connections: : 10
Connections: : 10
Connections: : 10
Connections: : 10
Queue: : 1
Queue: : 1
Connections: : 10
Connections: : 10
Queue: : 1
Queue: : 1
Connections: : 10
Connections: : 10
Queue: : 2
Queue: : 2



   
             
    




阅读(5114) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~