简体   繁体   English

在node.js中计算Web请求

[英]Time web requests in node.js

Given Node's async nature it is difficult to time a series of web requests. 鉴于Node的异步性质,很难对一系列Web请求进行计时。 How would I fire off 100 webrequests and figure out how long each individual request takes? 如何启动100个Web请求并确定每个请求需要多长时间? Knowing the OS will only allow a few concurrent web request, how do I get the timeing for each individual webrequest, removing the time spent waiting for the other connections to complete. 知道操作系统只允许一些并发的Web请求,如何获取每个webrequest的时间,消除等待其他连接完成所花费的时间。 I was hoping the socket event was fired when the request launched but it seems that the socket event is fired after the connection has been established. 我希望在请求启动时触发套接字事件,但似乎在建立连接后触发了套接字事件。

var http = require('http');

var urls = [
    '/cameron',
    '/sara',
    '...',


// Time a url collection.
function timeUrl(url, calback) {
    var options = {  
        host: 'www.examplesite.com',   
        port: 80,   
        path: ''  
    };      
    var times = [];
    times.push({'text': 'start', 'time':Date.now()});

    http.get(options, function(res) {
        times.push({'text': 'response', 'time':Date.now()});    

        var result = '';        
        res.on('data', function(chunk) {  
            result += chunk.length ;
            // result += chunk;        
        });   
        res.on('end', function() {  
            times.push({'text': 'end', 'time': Date.now(), 'body': result, 'statusCode': res.statusCode}); // ,   
            calback(times);
        });
    }).on('error', function(e) {  
        calback();
        console.log("Got error: " + e.message);
        times.push({'error':Date.now()});
    }).on('socket', function (response) {
         times.push({'text': 'socket', 'time':Date.now()});
    });
}
for (var i = 0; i < urls.length; i++) {
    var url = urls[i];
    timeUrl(url, function(times) {
        console.log(url);
        for (var i = 0; i < times.length; i++) {
            console.log(times[i].text, times[i].time - times[1].time , 'ms');
        }
        console.log('statusCode:', times[times.length -1].statusCode, 'Response Size:', times[times.length -1].body);   
         console.log('-');
    });
}

为了便于使用,做你似乎想要做的事情,我没有看到任何节拍时间

If you're worried about OS concurrency just introduce maximum concurrency (throttling) into your requests instead of trying to guess when exactly the OS has started. 如果您担心操作系统并发性,只需在请求中引入最大并发(限制),而不是试图猜测操作系统何时启动。 I'm skipping over some minor details like error handling and using the excellent async.js library: 我正在跳过一些小细节,比如错误处理和使用优秀的async.js库:

var http  = require('http')
  , async = require('async')
  , CONCURRENCY = 5 // edit to fit your OS concurrency limit
  , results = {}
  , urls = [
    '/cameron',
    '/sara',
    '/...'
];

// Time a url collection.
function timeUrl(url, callback) {
    var options = { host: 'www.examplesite.com', port: 80 }
      , start = Date.now()
      , socket = null;
    options.path = url;

    http.get(options, function(res) {
      var response = Date.now()
        , size = 0;
        res.on('data', function(chunk) { size += chunk.length; });   
        res.on('end',  function() {
          var end = Date.now();
          results[url] = { start: start, socket: socket, response: response, end: end, size: size };
          callback();
        });
    }).on('error', function(e) {
      results[url] = { start: start, socket: socket, error: Date.now(), stack: e };
      callback();
    }).on('socket', function () {
      socket = Date.now();
    });
}

async.forEachLimit(urls, CONCURRENCY, timeUrl, function() {
  console.log(JSON.stringify(results));
});

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

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