问题
So every 0.5 seconds, I'm interested in getting the price of Bitcoin as fast as possible.
curl https://www.okcoin.com/api/ticker.do?ok=1
*** 1st I do this in 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();
I get:
took 1033.241079ms
buy: 356.83
sell: 357.02
1033ms is way too slow for me...
*** 2ndly, I do (check for new price every 2 seconds):
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);
Gives:
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
Still looks slow to me...
*** 3rdly, I use agentkeepalive module (again polling every 2 seconds):
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);
Gives:
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
Is this the best I can do? I'm running all this from a VPS in the UK. Apart from moving my VPS closer to Hong Kong to get lower latency; is there anything else I can do? How do I know that the keepalive is working? Can I bypass any of the SSL or network handshaking?
回答1:
I seem to get very slightly better results using the request package, but overall similar to your step3 maybe try that one, I'm running it from US :
Code :
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);
Output :
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
Now consider this thought experiment :
There's 9623km between London and HK.
At the speed of light, and in a straight line, your packet would take 2*(9623/300000) = 64ms to travel back and forth.
Now add to that the fact that it's not a straight optic fiber, the multiple servers it has to pass through... and the time the server takes to process your request.
I guess 250ms isn't that bad after all ;)
来源:https://stackoverflow.com/questions/26302539/nodejs-https-requests-taking-too-long