connect-composer-stats

0.1.0 • Public • Published

connect-composer-stats

Statistics for connect-composer middlewares

NPM version Build Status

Enable runtime measurements for composed middlewares using connect-composer to track down latency problems within middlewares.

Inject either globally or locally into your composed middlewares and dump results into a CVS file.

Table of Contents

Description

Set globally for all composed middlewares

var composestats = require('connect-composer-stats')
var compose = require('connect-composer')
// get new stats object
var stats = composestats()
// globally inject stats use:
compose.options = { stats: stats.from }
 
// a middleware function
var mw = function (req, res, next) { next() }
// composing the single middlewares
var mws = compose (mw, mw, mw, mw)
var req = {}, res = {}
 
mws(req, res, function () {
  console.log(stats.data)
})

Set locally for a single composed middlewares

var composestats = require('connect-composer-stats')
var compose = require('connect-composer')
// get new stats object
var stats = composestats()
 
// a middleware function
var mw = function (req, res, next) { next() }
// composing the single middlewares
var mws = compose (mw, mw, mw, mw)
// locally inject stats use:
mws.options = { stats: stats.from }
 
var req = {}, res = {}
 
mws(req, res, function () {
  console.log(stats.data)
})

Wrap arround a single middleware function

var composestats = require('connect-composer-stats')
var compose = require('connect-composer')
// get new stats object
var stats = composestats()
 
// a middleware function
var mw = function (req, res, next) { next() }
// wrapping the stats middleware
var wrap = stats.from(mw)
 
var req = {}, res = {}
 
wrap(req, res, function () {
  wrap(req, res, function () {
    console.log(stats.data)
  })
})

Methods

stats()

create statistics for middleware(s)

Returns: Object, object with functions

from(fn)

take statistics from middleware function fn

Parameters

fn: function, middleware function from which to generate the statistics

Returns: function, wrapped middleware function

dump(options)

Dump the collected statistics into a csv file or to stdout

Parameters

options: Object, options

  • ns: false, // {Boolean} if false output is in milliseconds; if true then nanoseconds
  • dir: process.cwd(), // {String} dir where stats files are written; default current working dir
  • csv: true, // {Boolean} of true output csv to filesystem; default true
  • sortkey: 'total' // {String} key to reverse sort (total, average, max, min)

Returns: String, filename of csv file

data

The data object where the statistics for each function are stored

Example

{ './example.js:16:19:mwG':
   { name: './example.js:16:19:mwG', // file, line, column and name of function called
     count: 3,                       // number of function calls
     total: 6389,                    // time in nanoseconds
     min: 826,
     max: 2923 },
  './example.js:14:18:mw':
   { name: './example.js:14:18:mw',
     count: 2,
     total: 1662,
     min: 742,
     max: 920 } }

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work or correctly attributed with the source of its origin and licence.

License

Copyright (c) 2015 commenthol (MIT License)

See LICENSE for more info.

References

Readme

Keywords

Package Sidebar

Install

npm i connect-composer-stats

Weekly Downloads

1

Version

0.1.0

License

MIT

Last publish

Collaborators

  • commenthol