Skip to content

Commit

Permalink
bench: add simple benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 15, 2017
1 parent a88bfd5 commit 069e1e6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ dist: precise
before_install:
# Skip updating shrinkwrap / lock
- "npm config set shrinkwrap false"
# Remove all non-test dependencies
- "npm rm --save-dev benchmark beautify-benchmark"
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev istanbul"
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
Expand Down
59 changes: 59 additions & 0 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

/**
* Module dependencies.
*/

var benchmark = require('benchmark')
var benchmarks = require('beautify-benchmark')

/**
* Globals for benchmark.js
*/

global.fakerequest = fakerequest
global.forwarded = require('..')

var suite = new benchmark.Suite()

suite.add({
name: 'no header',
minSamples: 100,
fn: 'var addrs = forwarded(fakerequest({}))'
})

suite.add({
name: '1 address',
minSamples: 100,
fn: 'var addrs = forwarded(fakerequest({ "x-forwarded-for": "192.168.0.10" }))'
})

suite.add({
name: '2 addresses',
minSamples: 100,
fn: 'var addrs = forwarded(fakerequest({ "x-forwarded-for": "192.168.0.10, 192.168.1.20" }))'
})

suite.add({
name: '5 addresses',
minSamples: 100,
fn: 'var addrs = forwarded(fakerequest({ "x-forwarded-for": "192.168.0.10, 192.168.1.20, 192.168.1.21, 192.168.1.22, 192.168.1.23" }))'
})

suite.on('cycle', function onCycle (event) {
benchmarks.add(event.target)
})

suite.on('complete', function onComplete () {
benchmarks.log()
})

suite.run({async: false})

function fakerequest (headers) {
return {
headers: headers,
connection: {
remoteAddress: '10.0.0.1'
}
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
],
"repository": "jshttp/forwarded",
"devDependencies": {
"beautify-benchmark": "0.2.4",
"benchmark": "2.1.4",
"eslint": "3.19.0",
"eslint-config-standard": "10.2.1",
"eslint-plugin-import": "2.7.0",
Expand All @@ -32,6 +34,7 @@
"node": ">= 0.6"
},
"scripts": {
"bench": "node benchmark/index.js",
"lint": "eslint .",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
Expand Down

0 comments on commit 069e1e6

Please sign in to comment.