performance-node

0.2.0 • Public • Published

Performance for Node.js

CircleCI styled with prettier

A superset of the User Timing API (window.performance) for Node.js

Uses process.hrtime to capture marks + measures to gain deeper visibility into the timings that you define in your application.

0 Dependencies!

Requirements

  • Node >= 4.3.2
  • NPM >= 2.14.12

Install

With yarn (recommended) in project directory:

yarn add performance-node

With npm in project directory:

npm install performance-node

Then, run your application:

const Perf = require('performance-node');
 
const timeline = new Perf();
 
timeline.mark('foo-start');
// delay(10)
timeline.mark('foo-end');
timeline.measure('foo-measure', 'foo-start', 'foo-end');
 
const myMeasure = timeline.getEntriesByName('foo-measure')[0];
// {name: 'foo-measure', startTime: 1.2, duration: 10.5, entryType: 'measure'}

Methods

const Perf = require('performance-node');
 
const timeline = new Perf();
 
// set marks
timeline.mark('foo-start');
timeline.mark('foo-end');
 
// create a measurement
timeline.measure('foo-measure', 'foo-start', 'foo-end');
 
// get all timeline entries (marks + measures)
timeline.getEntries();
 
// get all entries with the same name
timeline.getEntriesByName('foo-start');
 
// get all entries with the same type
timeline.getEntriesByType('measure');
 
// clear marks
timeline.clearMarks();
 
// clear measures
timeline.clearMeasures();
 
// clear all entries
timeline.clear();
 
// get a point in time in milliseconds
timeline.now();

Config

offset (number: optional)

By default, the startTime of each mark is calculated by subracting the construction time from process.hrtime. If you would rather the startTime map as closely as possible to process.hrtime values, set offset: 0. You can also supply any number as a custom offset.

const Perf = require('performance-node');
 
const timeline = new Perf({ offset: 0 });
 
timeline.mark('foo-start');
// delay(10)
timeline.mark('foo-end');
timeline.measure('foo-measure', 'foo-start', 'foo-end');
 
const myMeasure = timeline.getEntriesByName('foo-measure')[0];
// {name: 'foo-measure', startTime: 227851.91, duration: 10.5, entryType: 'measure'}

timestamp (bool: optional = false)

Add a timestamp (unix epoch) value for each mark based on Date.now().

const Perf = require('performance-node');
 
const timeline = new Perf({ timestamp: true });
 
timeline.mark('foo-start');
 
const myMeasure = timeline.getEntriesByName('foo-start')[0];
// {name: 'foo-start', startTime: 1.2, duration: 0, entryType: 'mark', timestamp: 1501189303951}

Contributing

  • This project uses Prettier. Please execute npm run eslint -- --fix to auto-format the code before submitting pull requests.

Package Sidebar

Install

npm i performance-node

Weekly Downloads

69

Version

0.2.0

License

Apache-2.0

Last publish

Collaborators

  • teamiopipe