A simple way to profile the damage of running a task in a Node.JS instance.
When you request a new damage calculation it follow these steps:
With Node.JS and NPM installed, type:
$ npm install damage
Use this to get the Damage constructor.
var Damage = require('damage');
Then you can use prepare() to define a preparation task to run before each collection of tasks.
Damage.prepare(function () {
var fs = require('fs');
});
Then finally you can just get your damageOf() function, that you need to create damage tests.
var damageOf = Damage();
Now that you have the function, just use it with a description, a damage test function and the number of damage repeats, just like this:
damageOf('reading a file',function () {
fs.readFileSync(__filename);
done();
},1000);
Remember: Inside the test function you need to call done() after you finish the task.

You can change the global configuration of Damage with this:
Damage.config({
colors: false
});
To see all Damage's configuration, read this configuration file.
You can change the Damage environment object with this:
Damage.env({
shit: true
});
Then when you can access env variables like this:
var damageOf = Damage();
damageOf('testing some stuff', function () {
if (env.shit)
// do stuff
else
// do stuff
},1000);
You can change the Damage environment object with this:
Damage.prepare(function () {
var fs = require('fs');
var _ = require('underscore');
var mongoose = require('mongoose');
});
Some cases you'll need to do specific task that can not be placed inside prepare() becuse is not used in other tasks.
For those cases you can use start() to define where the test really begins.
Just like this:
var damageOf = Damage();
Damage.prepare(function () { var fs = require('fs'); });
damageOf('testing some specific shit', function () {
var requiredFile = fs.readFileSync('randomfile');
var obj = JSON.parse(requiredFile);
...
Add the start() to define that test will start now.
...
start();
...
Now the real test begins.
...
for (o in obj)
obj[o]=Number(obj[o]).valueOf();
done(); // And stops here.
},1000);
You can listen for the final processed and full result of each damage test using this:
// Add this after the damageOf() call
.is(function (processed,result) {
fs.writeFile('output.log',JSON.parse(processed));
})
Now you can get a heap snapshot file for each task. Just define the following configurations:
Damage.config({
heapdump: true,
dumpPath: __dirname+'/tmp/' // Where you want to save all heap snapshots.
});
There are some examples here. =)