nodejs - https请求耗时太长

时间:2014-10-10 15:09:13

标签: node.js https network-protocols low-latency

所以每0.5秒,我有兴趣尽快获得比特币的价格。

curl https://www.okcoin.com/api/ticker.do?ok=1

***我在nodejs中执行此操作:

var https = require('https');

var options = {
    host: 'www.okcoin.com',
    port: 443,
    path: '/api/ticker.do?ok=1',
    method: 'GET'
};

var time;
time=process.hrtime();
var req = https.request(options, function (res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        var diff=process.hrtime(time);
    console.log("took %dms", (diff[0] * 1e9 + diff[1])/1e6);

    var obj=JSON.parse(chunk);
    console.log("buy: "+obj.ticker.buy);
    console.log("sell: "+obj.ticker.sell);
    });
});
req.end();

我明白了:

took 1033.241079ms
buy: 356.83
sell: 357.02
对我来说,1033毫秒太慢了......

*** 2,我这样做(每2秒检查一次新价格):

var https = require('https');

var options = {
    host: 'www.okcoin.com',
    port: 443,
    path: '/api/ticker.do?ok=1',
    method: 'GET'
};


var time;
setInterval(function (){
    time=process.hrtime();
    https.get(options,function(res) {
        res.on('data', function (body) {
            var diff=process.hrtime(time);
            console.log("took %dms", (diff[0] * 1e9 + diff[1])/1e6);
        });
    })
},2000);

给出:

took 983.929691ms
took 944.539056ms
took 650.801495ms
took 729.976666ms
took 474.631864ms
took 1577.007647ms
took 1263.056615ms
took 929.032185ms
took 248.543903ms
took 702.258904ms
took 250.711814ms
took 832.824128ms
took 252.986642ms
took 255.080667ms
took 301.444557ms
took 297.921488ms

我看起来仍然很慢......

***第三,我使用agentkeepalive模块(每2秒再次轮询):

var https = require('https');
var kaa = require('agentkeepalive').HttpsAgent;

var keepaliveAgent = new kaa({
    maxSockets: 100,
    maxFreeSockets: 10,
    timeout: 60000,
    keepAliveTimeout: 30000   //free socket keepalive for 30 seconds
});
var options = {
    host: 'www.okcoin.com',
    port: 443,
    path: '/api/ticker.do?ok=1',
    method: 'GET',
    agent: keepaliveAgent
};


var time;
setInterval(function (){
    time=process.hrtime();
    https.get(options,function(res) {
    res.on('data', function (body) {
        var diff=process.hrtime(time);
        console.log("took %dms", (diff[0] * 1e9 + diff[1])/1e6);
    });
    })
},2000);

给出:

took 1673.525828ms
took 286.205758ms
took 276.441161ms
took 276.11333ms
took 770.785999ms
took 470.329748ms
took 235.586808ms
took 284.85831ms
took 238.250933ms
took 239.285309ms
took 237.260822ms
took 460.541798ms
took 853.24585ms
took 234.314756ms
took 285.784553ms
took 286.228781ms
took 235.298578ms
took 569.50514ms
took 235.890092ms
took 241.18033ms
took 234.773913ms

这是我能做的最好的吗?我在英国的VPS中运行这一切。除了让我的VPS更接近香港以获得更低的延迟;还有什么我可以做的吗?我怎么知道keepalive工作正常?我可以绕过任何SSL或网络握手吗?

1 个答案:

答案 0 :(得分:3)

使用请求包我似乎得到了更好的结果,但总体上类似于你的步骤3可能会尝试那个,我是从美国运行的:

代码:

var request = require('request');
var options = {
    url:'https://www.okcoin.com/api/ticker.do?ok=1',
    headers: {
            'connection': 'Keep-Alive'
        }
}

setInterval(function(){
var time;
time=Date.now();
request(options,function(err,res,body){
    var diff=Date.now();
    console.log("took %dms", (diff-time));
});
},1000);

输出:

took 484ms
took 253ms
took 248ms
took 254ms
took 251ms
took 259ms
took 255ms
took 252ms
took 259ms
took 257ms
took 263ms
took 255ms
took 257ms
took 253ms
took 264ms
took 254ms

现在考虑一下这个思想实验:

伦敦和香港之间有9623公里。

光速在一条直线中,您的数据包需要2 *(9623/300000)= 64ms来回移动。

现在补充一点,它不是直的光纤,它必须经过的多个服务器......以及服务器处理您的请求所需的时间。

我估计250毫秒并不是那么糟糕;)