Skip to content

Commit

Permalink
Correctly count non-00 statusCodes
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jul 14, 2016
1 parent af0e693 commit 02d160d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function run (opts, cb) {

function onResponse (statusCode, resBytes, responseTime) {
tracker.emit('response', this, statusCode, resBytes, responseTime)
const codeIndex = (parseInt(statusCode) / 100) - 1
const codeIndex = Math.floor(parseInt(statusCode) / 100) - 1

This comment has been minimized.

Copy link
@GlenTiki

GlenTiki Jul 14, 2016

Collaborator

Nice catch 👍

This comment has been minimized.

Copy link
@mcollina

mcollina Jul 14, 2016

Author Owner

really @kamil-mech found the bug!

statusCodes[codeIndex] += 1
// only record 2xx latencies
if (codeIndex === 1) latencies.record(responseTime)
Expand Down
23 changes: 5 additions & 18 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ const https = require('https')
const fs = require('fs')
const path = require('path')

function startServer () {
function startServer (opts) {
opts = opts || {}

const statusCode = opts.statusCode || 200
const server = http.createServer(handle)

server.listen(0)

function handle (req, res) {
res.statusCode = statusCode
res.end('hello world')
}

Expand Down Expand Up @@ -67,23 +71,6 @@ function startHttpsServer () {
return server
}

for (let i = 1; i <= 5; i++) {
module.exports[`start${i}xxServer`] = function () {
const server = http.createServer(handle)

server.listen(0)

function handle (req, res) {
res.writeHead(Number(`${i}00`))
res.end()
}

server.unref()

return server
}
}

module.exports.startServer = startServer
module.exports.startTimeoutServer = startTimeoutServer
module.exports.startHttpsServer = startHttpsServer
Expand Down
3 changes: 2 additions & 1 deletion test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ test('run should allow users to enter timestrings to be used for duration', (t)
for (let i = 1; i <= 5; i++) {
test(`run should count all ${i}xx status codes`, (t) => {
t.plan(2)
const server = helper[`start${i}xxServer`]()

const server = helper.startServer({ statusCode: i * 100 + 2 })

run({
url: `http://localhost:${server.address().port}`,
Expand Down

0 comments on commit 02d160d

Please sign in to comment.