Javascript node.js计时:process.hrtime()vs new Date().getTime()?
我正在尝试解决一个问题,即从node.js到远程http api的请求花费的时间太长。首先,我决定将计时添加到我的节点应用程序中:Javascript node.js计时:process.hrtime()vs new Date().getTime()?,javascript,node.js,Javascript,Node.js,我正在尝试解决一个问题,即从node.js到远程http api的请求花费的时间太长。首先,我决定将计时添加到我的节点应用程序中: 'use strict'; let logger = require('./logger'); let request = require('request'); module.exports = function(uri, done) { // set full url. let options = { uri: `http://example
'use strict';
let logger = require('./logger');
let request = require('request');
module.exports = function(uri, done) {
// set full url.
let options = {
uri: `http://example.org/${uri}`,
timeout: 60000,
json: true,
gzip: true,
encoding: null
};
const startDate = new Date().getTime();
const start = process.hrtime();
request(options, function(error, response, body) {
const endDate = new Date().getTime();
const end = process.hrtime(start);
const durationDate = endDate - startDate;
const duration = end[1] / 1000000;
const diff = Math.abs(Math.round(durationDate - duration));
logger.debug(`${uri} hrtime: ${duration} ms, date: ${durationDate} ms, diff: ${diff}`);
if (!error && response.statusCode === 200) {
done(error, response, body, body);
} else {
done(error, response, body);
}
});
};
这是我得到的输出:
<....>
2016-06-29T10:26:59.230Z /account hrtime: 41.567354 ms, date: 41 ms, diff: 1
2016-06-29T10:27:06.369Z /account hrtime: 42.052154 ms, date: 42 ms, diff: 0
2016-06-29T10:27:13.368Z /account hrtime: 51.582807 ms, date: 5052 ms, diff: 5000
2016-06-29T10:27:14.971Z /account hrtime: 40.705936 ms, date: 40 ms, diff: 1
2016-06-29T10:27:22.490Z /account hrtime: 45.953398 ms, date: 5046 ms, diff: 5000
2016-06-29T10:27:29.669Z /account hrtime: 42.504256 ms, date: 5043 ms, diff: 5000
2016-06-29T10:27:31.622Z /account hrtime: 39.405575 ms, date: 39 ms, diff: 0
<....>
2016-06-29T10:27:45.135Z /account hrtime: 40.594642 ms, date: 41 ms, diff: 0
2016-06-29T10:27:52.682Z /account hrtime: 40.290881 ms, date: 40 ms, diff: 0
2016-06-29T10:28:05.115Z /account hrtime: 50.81821 ms, date: 10050 ms, diff: 9999
2016-06-29T10:28:13.555Z /account hrtime: 52.061123 ms, date: 52 ms, diff: 0
<.....>
2016-06-29T10:29:45.052Z /account hrtime: 44.252486 ms, date: 44 ms, diff: 0
2016-06-29T10:29:46.829Z /account hrtime: 39.652963 ms, date: 40 ms, diff: 0
2016-06-29T10:29:49.101Z /account hrtime: 40.841915 ms, date: 41 ms, diff: 0
2016-06-29T10:29:55.097Z /account hrtime: 44.161802 ms, date: 5044 ms, diff: 5000
2016-06-29T10:30:01.784Z /account hrtime: 47.732807 ms, date: 47 ms, diff: 1
2016-06-29T10:30:04.309Z /account hrtime: 40.151299 ms, date: 40 ms, diff: 0
2016-06-29T10:30:06.926Z /account hrtime: 39.933368 ms, date: 40 ms, diff: 0
2016-06-29T10:30:14.440Z /account hrtime: 53.610396 ms, date: 5054 ms, diff: 5000
2016-06-29T10:26:59.230Z/账户hr时间:41.567354毫秒日期:41毫秒差异:1
2016-06-29T10:27:06.369Z/账户hr时间:42.052154毫秒,日期:42毫秒,差异:0
2016-06-29T10:27:13.368Z/账户hr时间:51.582807毫秒,日期:5052毫秒,差异:5000
2016-06-29T10:27:14.971Z/账户hr时间:40.705936毫秒日期:40毫秒差异:1
2016-06-29T10:27:22.490Z/账户hr时间:45.953398毫秒日期:5046毫秒差异:5000
2016-06-29T10:27:29.669Z/账户hr时间:42.504256ms日期:5043ms差异:5000
2016-06-29T10:27:31.622Z/账户hr时间:39.405575毫秒,日期:39毫秒,差异:0
2016-06-29T10:27:45.135Z/账户时间:40.594642毫秒,日期:41毫秒,差异:0
2016-06-29T10:27:52.682Z/账户hr时间:40.290881毫秒,日期:40毫秒,差异:0
2016-06-29T10:28:05.115Z/账户时间:50.81821毫秒,日期:10050毫秒,差异:9999
2016-06-29T10:28:13.555Z/账户hr时间:52.061123毫秒,日期:52毫秒,差异:0
2016-06-29T10:29:45.052Z/账户hr时间:44.252486毫秒,日期:44毫秒,差异:0
2016-06-29T10:29:46.829Z/账户hr时间:39.652963毫秒,日期:40毫秒,差异:0
2016-06-29T10:29:49.101Z/账户hr时间:40.841915毫秒,日期:41毫秒,差异:0
2016-06-29T10:29:55.097Z/账户hr时间:44.161802毫秒,日期:5044毫秒,差异:5000
2016-06-29T10:30:01.784Z/账户hr时间:47.732807毫秒日期:47毫秒差异:1
2016-06-29T10:30:04.309Z/账户hr时间:40.151299毫秒,日期:40毫秒,差异:0
2016-06-29T10:30:06.926Z/账户hr时间:39.933368毫秒,日期:40毫秒,差异:0
2016-06-29T10:30:14.440Z/账户hr时间:53.610396毫秒日期:5054毫秒差异:5000
因此,有时hrtime和getTime()之间的差异是巨大的。这不仅仅是一些误传,而是实际行为——如果看一下远程http服务器日志,我发现所有请求实际上都花费了hrtime报告的时间,getTime()报告的时间实际上是node.js应用程序响应的时间。因此,node.js应用程序本身似乎存在某种延迟。有关于为什么会发生这种情况的线索吗 节点v4.4.6更改
const duration = end[1] / 1000000;
到
你会得到要求的结果。
hr时间返回一个由两个值组成的数组-第一个值是秒,因此将其与1000相乘得到毫秒,第二个值是纳秒,因此除以1,然后再除以6个零,即1000000,将两者相加得到结果
const duration = (end[0]*1000) + (end[1] / 1000000);