Skip to content

Commit

Permalink
fixed little bug in geocouch-filler.js
Browse files Browse the repository at this point in the history
  • Loading branch information
berb committed Jan 9, 2011
1 parent b480ede commit 65174be
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions misc/geocouch-filler-js/geocouch-filler.js
Expand Up @@ -11,32 +11,30 @@ require.paths.unshift(path.join(__dirname, 'lib'));
var Barrier = require('barrier').Barrier;

var httpClientPoolSize = 16;
var host,port,db;
var host, port, db;

if(process.argv.length < 5){
if (process.argv.length < 5) {
console.log("\n\tUsage: node geocouch-filler.js <database-URI> <[bbox]> <count>\n\n")
process.exit(1);
};
var uri = url.parse(process.argv[2] || "http://localhost:5984/gc-utils");
var bbox = JSON.parse(process.argv[3] || "[-180,-90,180,90]") ;
var bbox = JSON.parse(process.argv[3] || "[-180,-90,180,90]");
var documentCount = parseInt(process.argv[4]) || 10;


if(uri && bbox && bbox.length === 4 && documentCount){
if (uri && bbox && bbox.length === 4 && documentCount) {
host = uri.hostname;
port = uri.port || 5984;
db = uri.pathname.split("/")[1] || "gc-utils";
}

var randomArbitrary = function(min, max) {
return Math.random() * (max - min) + min;
return Math.random() * (max - min) + min;
};


//create client pool to speed
var clients = [];
for (var i = 0; i < httpClientPoolSize; i++) {
clients.push(http.createClient(port, host));
for ( var i = 0; i < httpClientPoolSize; i++) {
clients.push(http.createClient(port, host));
}

//barrier allows to exit when queries have been executed
Expand All @@ -47,30 +45,33 @@ var b = new Barrier(documentCount, function() {

var ptr = 0;

for (var i = 0; i < documentCount; i++) {
var client = clients[ptr++ % httpClientPoolSize];
var headers = {
"Content-Type" : "application/json",
"Connection" : "keep-alive",
"Host" : uri.hostname
};

for ( var i = 0; i < documentCount; i++) {
var client = clients[ptr++ % httpClientPoolSize];

var entity = {
"geometry" : {
"type" : "Point",
"coordinates": [randomArbitrary(bbox[0],bbox[2]),randomArbitrary(bbox[1],bbox[3])]
}
};
var entity = {
"geometry" : {
"type" : "Point",
"coordinates" : [ randomArbitrary(bbox[0], bbox[2]), randomArbitrary(bbox[1], bbox[3]) ]
}
};

var request = client.request('POST', "/" + db, {
"Content-Type": "application/json",
"Connection": "keep-alive"
});
request.write(JSON.stringify(entity));
request.end();
request.once('response', function (response) {
if(response.statusCode === 201){
console.log("Document "+response.headers['location']+" created");
b.submit();
}
else{
console.log("POST caused "+response.statusCode+"!");
b.submit();
}
});
var request = client.request('POST', "/" + db, headers);
request.write(JSON.stringify(entity));
request.end();
request.once('response', function(response) {
if (response.statusCode === 201) {
console.log("Document " + response.headers['location'] + " created");
b.submit();
}
else {
console.log("POST caused " + response.statusCode + "!");
b.submit();
}
});
}

0 comments on commit 65174be

Please sign in to comment.