Node.js: Get Response Time

2019-02-22 03:56发布

How can I know the response time of a URL?

I'm using http.get() to make an HTTP GET request.

2条回答
祖国的老花朵
2楼-- · 2019-02-22 04:27

There's no builtin function or value to get the response time.

But you can easily get the value yourself.

var http = require('http');
var start = new Date();
http.get({host: 'google.com', port: 80}, function(res) {
    console.log('Request took:', new Date() - start, 'ms');
});

EDIT

Since V8 also supports the new ES5 Date.now(), using that instead of new Date() would be a little bit cleaner.

查看更多
看我几分像从前
3楼-- · 2019-02-22 04:30

If you are not looking for a programmatic solution, load up your friend and mine, Firefox with Firebug installed. When you bring up Firebug, select the "Net" tab and you will see the response times of all requests on the page. Hover over a time and you get a popup breakdown of where the time went -- DNS Lookup, Wait Time, etc.

查看更多
登录 后发表回答