Skip to content

Instantly share code, notes, and snippets.

@markdaws
Created August 17, 2011 22:36
Show Gist options
  • Save markdaws/1152822 to your computer and use it in GitHub Desktop.
Save markdaws/1152822 to your computer and use it in GitHub Desktop.
var http = require('http');
var util = require('util');
var count = (new Date()).getTime();
var apiPath = 'services/rest/?method=flickr.photos.getInfo&api_key=52102ae42eabc584b4f587b5f7aa5797&photo_id=6053145786+-+013&format=rest';
var host = 'api.flickr.com';
var numberOfRequests = 1000;
var completedCount = 0;
var startTime = (new Date()).getTime();
function callAPI(id) {
doGet(id + ': GET1',
http,
host,
80,
apiPath + '&rand=' + (count++),
function(err, response) {
++completedCount;
console.log(id + ' completed (' + completedCount + ' of ' + numberOfRequests + ')');
if(numberOfRequests === completedCount) {
var endTime = (new Date()).getTime();
console.log('num req:' + numberOfRequests +
', time:' + (endTime-startTime)/1000 + '(s)');
return;
}
setTimeout(function() {
callAPI(id + 1);
}, 1000);
});
}
function doGet(id, protocol, host, port, path, callback) {
var options = {
host: host,
port: port,
path: path,
method: 'GET'
};
var req = protocol.request(options, function(res) {
var response = '';
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function () {
callback(null, response);
});
res.on('error', function (e) {
callback(e, null);
});
});
req.on('error', function(e) {
callback(e, null);
});
req.end();
}
callAPI(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment