time-tree

0.3.0 • Public • Published

time-tree

Timer utility for Node.js that gives you a contextual tree of where time is spent.

If process.hrtime() is available, it will use it, otherwise it will use Date.now().

Build Status NPM version

Install

To install the most recent release from npm, run:

npm install time-tree

Quick Example

var timetree = require('time-tree');
 
var timer = timetree('example');
timer.setContext({ actions: 2 });
return doSomething(timer, function() {
    return doSomething(timer, function() {
        timer.end();
        console.log(timer.getResult());
    });
});
 
function doSomething(timer, callback) {
    var subTimer = timer.split('subtask');
    setTimeout(function() {
        subTimer.end();
        return callback();
    }, 100);
}

Outputs,

{ name: 'example',
  duration: 205.275,
  context: { actions: 2 },
  timers:
   [ { name: 'subtask', duration: 103.307 },
     { name: 'subtask', duration: 101.597 } ] }

See examples/ for more.

Documentation

### timetree(name[, context])

Creates a new Timer object with the given name, and starts the timer.

Arguments

Example

var timetree = require('time-tree');
var timer = timetree('myTimer');
### getName()

Gets the timer name.

### getDuraton()

Gets the timer duration.

### getContext()

Gets the context data for the timer.

### setContext(context)

Sets the context data for the timer. This is returned with the timer data in getResult().

For example, if you're timing how long it takes to perform N actions, you may want to set N to the context.

Arguments

  • context - Context data to set on the timer.

Example

timer.setContext({ actions: 2 });
### split(name[, context])

Creates a new sub timer with the given name, starts the sub timer, stores it in the parent timer, and returns it.

Arguments

Example

var subTimer = timer.split('subTimer');
### end()

Stops the timer, calculating the duration of the timer.

Note: You must stop each individual timer. Calling end() on a parent timer will not call end() on its sub timers.

Example

timer.end();
### wrap(callback)

Wraps a callback to include a call to end().

Arguments

  • callback - Function to wrap

Example

// Instead of this
fs.readFile(filename, function (err, file) {
  timer.end();
  callback(err, file);
})
 
// You can do this
fs.readFile(filename, timer.wrap(callback));
### getSubTimer(name[, recursive])

Returns the first sub timer object that matches the given name.

Arguments

  • name - Name of timer to search for.
  • recursive - Whether to search recursively. Optional. Defaults to FALSE.

Example

timer.getSubTimer('subEvent');
### getSubTimers(name[, recursive])

Returns the sub timer objects that match the given name.

Arguments

  • name - Name of timer to search for.
  • recursive - Whether to search recursively. Optional. Defaults to FALSE.

Example

timer.getSubTimers('subEvent');
### getResult()

Returns the timer and all sub timers as a plain data object, i.e. for logging.

Example

timer.getResult();

Returns,

{ name: 'myTimer',
  duration: 100,
  context: { actions: 2 },
  timers:
   [ { name: 'subTimer', duration: 50 } ] }

Readme

Keywords

none

Package Sidebar

Install

npm i time-tree

Weekly Downloads

0

Version

0.3.0

License

MIT

Last publish

Collaborators

  • stephenmelrose