Skip to content

Commit

Permalink
add ability to get response time on reply (#1697)
Browse files Browse the repository at this point in the history
* add ability to get response time on reply

* feedback

* Update docs/Reply.md as per suggestion

Co-Authored-By: Manuel Spigolon <behemoth89@gmail.com>

* update type of FastifyReply

* add type test
  • Loading branch information
GlenTiki authored and delvedor committed Jun 11, 2019
1 parent fda8448 commit 1ad7566
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
9 changes: 9 additions & 0 deletions docs/Reply.md
Expand Up @@ -10,6 +10,7 @@
- [.hasHeader(key)](#hasheaderkey)
- [.redirect(dest)](#redirectdest)
- [.callNotFound()](#callnotfound)
- [.getResponseTime()](#getresponsetime)
- [.type(contentType)](#typecontenttype)
- [.serializer(func)](#serializerfunc)
- [.sent](#sent)
Expand Down Expand Up @@ -109,6 +110,14 @@ Invokes the custom not found handler.
reply.callNotFound()
```

<a name="getResponseTime"></a>
### .getResponseTime()
Invokes the custom response time getter to calculate the amount of time passed since the request was started.

```js
const milliseconds = reply.getResponseTime()
```

<a name="type"></a>
### .type(contentType)
Sets the content type for the response.
Expand Down
1 change: 1 addition & 0 deletions fastify.d.ts
Expand Up @@ -166,6 +166,7 @@ declare namespace fastify {
getHeader(name: string): string | undefined
hasHeader(name: string): boolean
callNotFound(): void
getResponseTime(): number
type(contentType: string): FastifyReply<HttpResponse>
redirect(url: string): FastifyReply<HttpResponse>
redirect(statusCode: number, url: string): FastifyReply<HttpResponse>
Expand Down
15 changes: 11 additions & 4 deletions lib/reply.js
Expand Up @@ -222,6 +222,16 @@ Reply.prototype.callNotFound = function () {
notFound(this)
}

Reply.prototype.getResponseTime = function () {
var responseTime = 0

if (this[kReplyStartTime] !== undefined) {
responseTime = now() - this[kReplyStartTime]
}

return responseTime
}

function preserializeHook (reply, payload) {
if (reply.context.preSerialization !== null) {
onSendHookRunner(
Expand Down Expand Up @@ -460,11 +470,8 @@ function onResponseCallback (err, request, reply) {
if (reply.log[kDisableRequestLogging]) {
return
}
var responseTime = 0

if (reply[kReplyStartTime] !== undefined) {
responseTime = now() - reply[kReplyStartTime]
}
var responseTime = reply.getResponseTime()

if (err != null) {
reply.log.error({
Expand Down
30 changes: 29 additions & 1 deletion test/internals/reply.test.js
Expand Up @@ -14,7 +14,7 @@ const {
} = require('../../lib/symbols')

test('Once called, Reply should return an object with methods', t => {
t.plan(12)
t.plan(13)
const response = { res: 'res' }
function context () {}
function request () {}
Expand All @@ -27,6 +27,7 @@ test('Once called, Reply should return an object with methods', t => {
t.is(typeof reply.status, 'function')
t.is(typeof reply.header, 'function')
t.is(typeof reply.serialize, 'function')
t.is(typeof reply.getResponseTime, 'function')
t.is(typeof reply[kReplyHeaders], 'object')
t.strictEqual(reply.res, response)
t.strictEqual(reply.context, context)
Expand Down Expand Up @@ -977,3 +978,30 @@ test('should throw error when attempting to set reply.sent more than once', t =>
t.pass()
})
})

test('reply.getResponseTime() should return 0 before the timer is initialised on the reply by setting up response listeners', t => {
t.plan(1)
const response = { statusCode: 200 }
const context = {}
const reply = new Reply(response, context, null)
t.equal(reply.getResponseTime(), 0)
})

test('reply.getResponseTime() should return a number greater than 0 after the timer is initialised on the reply by setting up response listeners', t => {
t.plan(1)
const fastify = require('../..')()
fastify.route({
method: 'GET',
url: '/',
handler: (req, reply) => {
reply.send('hello world')
}
})

fastify.addHook('onResponse', (req, reply) => {
t.true(reply.getResponseTime() > 0)
t.end()
})

fastify.inject({ method: 'GET', url: '/' })
})
4 changes: 4 additions & 0 deletions test/types/index.ts
Expand Up @@ -346,6 +346,10 @@ server
.get('/deprecatedpath/*', (req, reply) => {
reply.callNotFound()
})
.get('/getResponseTime', function (req, reply) {
const milliseconds : number = reply.getResponseTime()
reply.send({ milliseconds })
})

// Generics example
interface Query {
Expand Down

0 comments on commit 1ad7566

Please sign in to comment.