@fastify/routes-stats
TypeScript icon, indicating that this package has built-in type declarations

3.4.0 • Public • Published

@fastify/routes-stats

CI NPM version js-standard-style

Provide stats for routes using require('perf_hooks'), for Fastify.

Install

npm i @fastify/routes-stats

Example

'use strict'

const Fastify = require('fastify')
const fastify = Fastify()

fastify.register(require('@fastify/routes-stats'), {
  printInterval: 4000, // milliseconds
  decoratorName: "performanceMarked", // decorator is set to true if a performace.mark was called for the request
})

fastify.get('/', function (request, reply) {
  reply.send({ hello: 'world' })
})

fastify.get(
  '/:param/dynamic-route-example',
  { config: { statsId: 'group-stats-together' } },
  function (request, reply) {
    reply.send({ hello: 'world' })
  }
)

fastify.get('/__stats__', async function () {
  // stats is added to the fastify instance
  return this.stats()
})

fastify.listen({ port: 3000 })
$ curl -s localhost:3000/__stats__ | jsonlint
{
  "GET": {
    "/": {
      "mean": 0.2406786,
      "mode": 0.755647,
      "median": 0.121999,
      "max": 0.755647,
      "min": 0.050214,
      "sd": 0.2905856386253457
    }
  },
  "POST": {
    "/": {
      "mean": 0.11260519999999999,
      "mode": 0.292262,
      "median": 0.055179,
      "max": 0.292262,
      "min": 0.044159,
      "sd": 0.10438752062722824
    }
  }
}

It will also log a stat object every 30 seconds (by default).

License

MIT

Package Sidebar

Install

npm i @fastify/routes-stats

Weekly Downloads

17

Version

3.4.0

License

MIT

Unpacked Size

22.3 kB

Total Files

14

Last publish

Collaborators

  • gurgunday
  • metcoder95
  • galvez
  • simenb
  • coopflow
  • simoneb
  • rafaelgss
  • starptech
  • delvedor
  • matteo.collina
  • allevo
  • jsumners
  • zekth
  • eomm
  • fox1t
  • airhorns
  • kibertoad
  • climba03003
  • is2ei
  • fdawgs