Skip to content

Commit

Permalink
Merge pull request #72 from thekemkid/totalReqs
Browse files Browse the repository at this point in the history
Total reqs sent counted
  • Loading branch information
GlenTiki authored Jul 21, 2016
2 parents d765e37 + 645eed1 commit f726c52
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 44 deletions.
1 change: 1 addition & 0 deletions lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Client.prototype._doRequest = function (rpi) {
this.destroy()
return
}
this.emit('request')

this.resData[rpi].startTime = process.hrtime()
this.conn.write(this.requestIterator.move())
Expand Down
13 changes: 0 additions & 13 deletions lib/percentiles.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/progressTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const chalk = require('chalk')
const prettyBytes = require('pretty-bytes')
const xtend = require('xtend')
const format = require('./format')
const percentiles = require('./percentiles')
const percentiles = require('hdr-histogram-percentiles-obj').percentiles
const defaults = {
// use stderr as its progressBar's default
outputStream: process.stderr,
Expand Down
38 changes: 11 additions & 27 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const URL = require('url')
const Histogram = require('native-hdr-histogram')
const timestring = require('timestring')
const Client = require('./httpClient')
const percentiles = require('./percentiles')
const xtend = require('xtend')
const histUtil = require('hdr-histogram-percentiles-obj')
const histAsObj = histUtil.histAsObj
const addPercentiles = histUtil.addPercentiles

const defaultOptions = {
headers: {},
Expand Down Expand Up @@ -58,6 +60,7 @@ function run (opts, cb) {
let timeouts = 0
let totalBytes = 0
let totalRequests = 0
let totalCompletedRequests = 0
let amount = opts.amount
let stop = false
let numRunning = opts.connections
Expand Down Expand Up @@ -91,8 +94,12 @@ function run (opts, cb) {
client.on('response', onResponse)
client.on('connError', onError)
client.on('timeout', onTimeout)
client.on('request', () => { totalRequests++ })
client.on('done', onDone)
clients.push(client)

// we will miss the initial request emits because the client emits request on construction
totalRequests += url.pipelining < url.rate ? url.rate : url.pipelining
}

function distributeNums (x, i) {
Expand Down Expand Up @@ -142,7 +149,7 @@ function run (opts, cb) {

const interval = setInterval(() => {
totalBytes += bytes
totalRequests += counter
totalCompletedRequests += counter
requests.record(counter)
throughput.record(bytes)
counter = 0
Expand All @@ -155,7 +162,7 @@ function run (opts, cb) {
let result = {
title: opts.title,
url: opts.url,
requests: histAsObj(requests, totalRequests),
requests: histAsObj(requests, totalCompletedRequests),
latency: addPercentiles(latencies, histAsObj(latencies)),
throughput: histAsObj(throughput, totalBytes),
errors: errors,
Expand All @@ -167,6 +174,7 @@ function run (opts, cb) {
pipelining: opts.pipelining,
'non2xx': statusCodes[0] + statusCodes[2] + statusCodes[3] + statusCodes[4]
}
result.requests.sent = totalRequests
statusCodes.forEach((code, index) => { result[(index + 1) + 'xx'] = code })
tracker.emit('done', result)

Expand Down Expand Up @@ -233,30 +241,6 @@ function run (opts, cb) {
return tracker
} // run

function histAsObj (hist, total) {
const result = {
average: Math.ceil(hist.mean() * 100) / 100,
stddev: Math.ceil(hist.stddev() * 100) / 100,
min: hist.min(),
max: hist.max()
}

if (typeof total === 'number') {
result.total = total
}

return result
}

function addPercentiles (hist, result) {
percentiles.forEach((perc) => {
const key = ('p' + perc).replace('.', '')
result[key] = hist.percentile(perc)
})

return result
}

function noop () {}

module.exports = run
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"chalk": "^1.1.3",
"deep-extend": "^0.4.1",
"hdr-histogram-percentiles-obj": "^1.1.0",
"http-parser-js": "^0.4.2",
"minimist": "^1.2.0",
"native-hdr-histogram": "^0.3.0",
Expand Down
4 changes: 4 additions & 0 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ test('run', (t) => {
t.ok(result.requests.min, 'requests.min exists')
t.ok(result.requests.max, 'requests.max exists')
t.ok(result.requests.total >= result.requests.average * 2 / 100 * 95, 'requests.total exists')
t.ok(result.requests.sent, 'sent exists')
t.ok(result.requests.sent >= result.requests.total, 'total requests made should be more than or equal to completed requests total')

t.ok(result.throughput, 'throughput exists')
t.ok(result.throughput.average, 'throughput.average exists')
Expand Down Expand Up @@ -80,6 +82,8 @@ test('tracker.stop()', (t) => {
t.ok(result.requests.min, 'requests.min exists')
t.ok(result.requests.max, 'requests.max exists')
t.ok(result.requests.total >= result.requests.average * 2 / 100 * 95, 'requests.total exists')
t.ok(result.requests.sent, 'sent exists')
t.ok(result.requests.sent >= result.requests.total, 'total requests made should be more than or equal to completed requests total')

t.ok(result.throughput, 'throughput exists')
t.ok(result.throughput.average, 'throughput.average exists')
Expand Down
12 changes: 9 additions & 3 deletions test/runAmount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const timeoutServer = helper.startTimeoutServer()
const server = helper.startServer()

test('run should only send the expected number of requests', (t) => {
t.plan(7)
t.plan(10)

let done = false

Expand All @@ -18,7 +18,8 @@ test('run should only send the expected number of requests', (t) => {
amount: 50146
}, (err, res) => {
t.error(err)
t.equal(res.requests.total, 50146, 'results should match the amount')
t.equal(res.requests.total + res.timeouts, 50146, 'results should match the amount')
t.equal(res.requests.sent, 50146, 'totalRequests should match the amount')
done = true
})

Expand All @@ -33,6 +34,7 @@ test('run should only send the expected number of requests', (t) => {
}, (err, res) => {
t.error(err)
t.equal(res.requests.total, 20, 'results should match max connection requests * connections')
t.equal(res.requests.sent, 20, 'totalRequests should match the expected amount')
})

run({
Expand All @@ -42,11 +44,12 @@ test('run should only send the expected number of requests', (t) => {
}, (err, res) => {
t.error(err)
t.equal(res.requests.total, 10, 'results should match max overall requests')
t.equal(res.requests.sent, 10, 'totalRequests should match the expected amount')
})
})

test('should shutdown after all amounts timeout', (t) => {
t.plan(2)
t.plan(5)

run({
url: `http://localhost:${timeoutServer.address().port}`,
Expand All @@ -56,5 +59,8 @@ test('should shutdown after all amounts timeout', (t) => {
}, (err, res) => {
t.error(err)
t.equal(res.errors, 10)
t.equal(res.timeouts, 10)
t.equal(res.requests.sent, 10, 'totalRequests should match the expected amount')
t.equal(res.requests.total, 0, 'total completed requests should be 0')
})
})

0 comments on commit f726c52

Please sign in to comment.