Skip to content

Commit

Permalink
Merge pull request #22 from thekemkid/issue-19
Browse files Browse the repository at this point in the history
Fixed #16: can now pass customiseRequest to build custom requests per connection
  • Loading branch information
mcollina committed Jun 25, 2016
2 parents a1aaf18 + 759e648 commit 1567b53
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 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)
}
}
37 changes: 35 additions & 2 deletions test/myhttp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ test('client supports changing the headers and body', (t) => {

t.same(client._req,
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'header changes updated request')
'changes updated request')
client.destroy()
})

Expand All @@ -237,6 +237,39 @@ test('client supports changing the headers and body together', (t) => {

t.same(client._req,
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'header changes updated request')
'changes updated request')
client.destroy()
})

test('client customiseRequest function overwrites the headers and body', (t) => {
t.plan(9)

const opts = server.address()
opts.body = 'hello world'
opts.method = 'POST'
opts.customiseRequest = (client) => {
t.ok(client.setHeadersAndBody, 'client had setHeadersAndBody method')
t.ok(client.setHeaders, 'client had setHeaders method')
t.ok(client.setBody, 'client had setBody method')

client.setHeadersAndBody({header: 'modifiedHeader'}, 'modified')
}

const client = new Client(opts)

t.same(client.opts.body, 'modified', 'body was changed')
t.notSame(client.opts.body, 'hello world', 'body was changed')

t.same(client.opts.headers, {'Content-Length': 8, header: 'modifiedHeader'}, 'header was changed')
t.notSame(client.opts.body, {'Content-Length': 11}, 'header was changed')

t.same(client._req,
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'changes updated request')

t.notSame(client._req,
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
'changes updated request')

client.destroy()
})

0 comments on commit 1567b53

Please sign in to comment.