From 069e1e6261bc32fb0aafb738e3abb111f73bf118 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Thu, 14 Sep 2017 20:07:37 -0400 Subject: [PATCH] bench: add simple benchmark --- .travis.yml | 2 ++ benchmark/index.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +++ 3 files changed, 64 insertions(+) create mode 100644 benchmark/index.js diff --git a/.travis.yml b/.travis.yml index 113b38b..6b5b301 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/benchmark/index.js b/benchmark/index.js new file mode 100644 index 0000000..bffcd02 --- /dev/null +++ b/benchmark/index.js @@ -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' + } + } +} diff --git a/package.json b/package.json index 5bf4e82..7cab5e2 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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/",