koa-server-timing

0.2.2 • Public • Published

koa-server-timing

NPM version Build status Test coverage Dependency Status License Downloads

Koa 2 Server-Timing header middleware.

Installation

$ npm install koa-server-timing

API

const Koa = require('koa');
const app = new Koa();
app.use(require('koa-server-timing')({ total: true /* default to NODE_ENV !== 'production' */ }));
 
ctx.state.timings.startSpan('A Task description', 'taskSlug' /* optional, will be created a-task-description, if missed */)
 
/* ... do some long task to measure here */
ctx.state.timings.stopSpan('A Task description' /* or 'taskSlug' or return from startSpan */);
 

Options

  • total where do you want to see total processing time in Server-Timings

Example

const timings = require('koa-server-timing');
const koa = require('koa');
const db = require('./mongoose');
const app = koa();
 
app.use(timings());
 
app.listen(3000);
 
console.log('listening on port 3000');
 
app.use(async (ctx, next) => {
    ctx.state.timings.startSpan('Query DB for User object');
    const user = await db.User.findOne({ email: 'test@test.com' }).exec();
    ctx.state.timings.stopSpan('Query DB for User object'); // or just pass return of startSpan (it will be a slug)
})
 

Why

  • Small, no dependencies, 100% test coverage
  • Uses process.hrtime, so, no extra timers and very precise

See also

License

MIT

Package Sidebar

Install

npm i koa-server-timing

Weekly Downloads

1

Version

0.2.2

License

MIT

Last publish

Collaborators

  • tinovyatkin