Created
October 31, 2014 18:02
-
-
Save cstivers78/3c4c90aa4dd53351e99c to your computer and use it in GitHub Desktop.
Throttled Operations in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* usage: node . <ITERS> <OPS> | |
* | |
* ITERS - Number of iterations to perform. Each iteration executes OPS operations. | |
* OPS - Number of operations to perform per iteration. | |
*/ | |
var aerospike = require('aerospike'); | |
var util = require('util'); | |
// default values | |
var ITERS_DEFAULT = 4 * 1000; | |
var OPS_DEFAULT = 4 * 1000; | |
// upper bounds | |
var iters = process.argv.length > 2 ? parseInt(process.argv[2],10) : ITER_DEFAULT; | |
var ops = process.argv.length > 3 ? parseInt(process.argv[3],10) : OPS_DEFAULT; | |
// counters | |
var iter_count = 0; | |
var op_count = 0; | |
var config = { | |
hosts: [{addr: 's4', port: 3000}] | |
}; | |
// called for each operation. | |
// will report on memory usage | |
// will spawn a new iteration if any remaining | |
function done(client) { | |
op_count++; | |
if ( op_count % 1000 === 0 ) { | |
console.log("heap", op_count, util.inspect(process.memoryUsage())); | |
} | |
if ( op_count === ops ) { | |
console.log("--------------------------------"); | |
console.log("iteration", iter_count); | |
console.log("operations", op_count); | |
console.log("heap", util.inspect(process.memoryUsage())); | |
iter_count++; | |
if ( iter_count < iters ) { | |
run(client) | |
} | |
} | |
} | |
// an iteration | |
function run(client) { | |
op_count = 0; | |
console.log("================================"); | |
console.log("iteration", iter_count) | |
var i, key, rec; | |
for (i = 0; i < ops; i++) { | |
key = aerospike.key('test','demo2', i); | |
rec = {i: i}; | |
client.put(key, rec, function(err) { | |
done(client); | |
}); | |
} | |
} | |
// run it! | |
aerospike.client(config).connect(function(err, client) { | |
run(client); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment