Skip to content

Commit

Permalink
Fixed #16: can now pass customiseRequest to build custom requests per
Browse files Browse the repository at this point in the history
connection
  • Loading branch information
GlenTiki committed Jun 24, 2016
1 parent 085838b commit 10dddba
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Start autocannon against the given target.
* `method`: The http method to use. _OPTIONAL_ `default: 'GET'`.
* `body`: A `String` or a `Buffer` containing the body of the request. Leave undefined for an empty body. _OPTIONAL_ default: `undefined`.
* `headers`: An `Object` containing the headers of the request. _OPTIONAL_ default: `{}`.
* `customiseRequest`: A `Function` which will be passed the `Client` object for each connection to be made. This can be used to customise each individual connection headers and body using the API shown below. The changes you make to the client in this function will take precedence over the default `body` and `headers` you pass in here. There is an example of this in the samples folder. _OPTIONAL_ default: `function noop () {}`.
* `cb`: The callback which is called on completion of the benchmark. Takes the following params. _OPTIONAL_.
* `err`: If there was an error encountered with the run.
* `results`: The results of the run.
Expand Down
5 changes: 5 additions & 0 deletions lib/myhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function Client (opts) {
return new Client(opts)
}

opts.customiseRequest = opts.customiseRequest || noop
opts.pipelining = opts.pipelining || 1
opts.port = opts.port || 80
opts.method = opts.method || 'GET'
Expand Down Expand Up @@ -65,6 +66,8 @@ function Client (opts) {

this.setHeaders(opts.headers)

opts.customiseRequest(this)

this._connect()
}

Expand Down Expand Up @@ -148,4 +151,6 @@ Client.prototype._rebuild = function () {
}
}

function noop () {}

module.exports = Client
1 change: 1 addition & 0 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function run (opts, cb) {
url.method = opts.method
url.body = opts.body
url.headers = opts.headers
url.customiseRequest = opts.customiseRequest

let clients = []
for (let i = 0; i < opts.connections; i++) {
Expand Down
33 changes: 33 additions & 0 deletions samples/customise-individual-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const http = require('http')
const autocannon = require('autocannon')

const server = http.createServer(handle)

server.listen(0, startBench)

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

function startBench () {
const url = 'http://localhost:' + server.address().port

autocannon({
url: url,
connections: 1000,
duration: 10,
customiseRequest: customiseRequest
}, finishedBench)

let connection = 0

function customiseRequest (client) {
client.setBody('connection number', connection++)
}

function finishedBench (err, res) {
console.log('finished bench', err, res)
}
}

0 comments on commit 10dddba

Please sign in to comment.