Skip to content

Commit

Permalink
bench: add simple benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 7, 2017
1 parent 74793d9 commit 1599530
Show file tree
Hide file tree
Showing 5 changed files with 127 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 beautify-benchmark 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
47 changes: 47 additions & 0 deletions benchmark/etag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

/**
* Module dependencies.
*/

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

/**
* Globals for benchmark.js
*/

global.fresh = require('..')

var suite = new benchmark.Suite()

suite.add({
name: 'star',
minSamples: 100,
fn: 'var val = fresh({ \'if-none-match\': \'*\' }, { etag: \'"foo"\' })'
})

suite.add({
name: 'single etag',
minSamples: 100,
fn: 'var val = fresh({ \'if-none-match\': \'"foo"\' }, { etag: \'"foo"\' })'
})

suite.add({
name: 'several etags',
minSamples: 100,
fn: 'var val = fresh({ \'if-none-match\': \'"foo", "bar", "fizz", "buzz"\' }, { etag: \'"buzz"\' })'
})

suite.on('start', function onCycle (event) {
process.stdout.write(' etag\n\n')
})

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

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

suite.run({async: false})
34 changes: 34 additions & 0 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var fs = require('fs')
var path = require('path')
var spawn = require('child_process').spawn

var exe = process.argv[0]
var cwd = process.cwd()

for (var dep in process.versions) {
console.log(' %s@%s', dep, process.versions[dep])
}

console.log('')

runScripts(fs.readdirSync(__dirname))

function runScripts (fileNames) {
var fileName = fileNames.shift()

if (!fileName) return
if (!/\.js$/i.test(fileName)) return runScripts(fileNames)
if (fileName.toLowerCase() === 'index.js') return runScripts(fileNames)

var fullPath = path.join(__dirname, fileName)

console.log('> %s %s', exe, path.relative(cwd, fullPath))

var proc = spawn(exe, [fullPath], {
'stdio': 'inherit'
})

proc.on('exit', function () {
runScripts(fileNames)
})
}
41 changes: 41 additions & 0 deletions benchmark/modified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

/**
* Module dependencies.
*/

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

/**
* Globals for benchmark.js
*/

global.fresh = require('..')

var suite = new benchmark.Suite()

suite.add({
name: 'not modified',
minSamples: 100,
fn: 'var val = fresh({ \'if-modified-since\': \'Fri, 01 Jan 2010 00:00:00 GMT\' }, { \'last-modified\': \'Sat, 01 Jan 2000 00:00:00 GMT\' })'
})

suite.add({
name: 'modified',
minSamples: 100,
fn: 'var val = fresh({ \'if-modified-since\': \'Mon, 01 Jan 1990 00:00:00 GMT\' }, { \'last-modified\': \'Sat, 01 Jan 2000 00:00:00 GMT\' })'
})

suite.on('start', function onCycle (event) {
process.stdout.write(' modified\n\n')
})

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

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

suite.run({async: false})
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
],
"repository": "jshttp/fresh",
"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 @@ -35,6 +37,7 @@
"node": ">= 0.6"
},
"scripts": {
"bench": "node benchmark/index.js",
"lint": "eslint --plugin markdown --ext js,md .",
"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 1599530

Please sign in to comment.