Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark runner #126

Merged
merged 1 commit into from
Nov 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
107 changes: 107 additions & 0 deletions benchmarks/runbench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict'

var fs = require('fs')
var path = require('path')
var spawn = require('child_process').spawn
var pump = require('pump')
var split = require('split2')
var through = require('through2')
var steed = require('steed')

function usage () {
return fs.createReadStream(path.join(__dirname, 'usage.txt'))
}

if (!process.argv[2]) {
usage().pipe(process.stdout)
process.exit()
}

var selectedBenchmark = process.argv[2].toLowerCase()
var benchmarkDir = path.resolve(__dirname)
var benchmarks = {
basic: 'basic.bench.js',
object: 'object.bench.js',
deepobject: 'deep-object.bench.js',
multiarg: 'multiArg.bench.js',
child: 'child.bench.js',
grandchild: 'childChild.bench.js',
conception: 'childCreation.bench.js'
}

function runBenchmark (name, done) {
var benchmarkResults = {}
benchmarkResults[name] = {}

var processor = through(function (line, enc, cb) {
var parts = ('' + line).split(': ')
var parts2 = parts[0].split('*')
var logger = parts2[0].replace('bench', '')

if (!benchmarkResults[name][logger]) benchmarkResults[name][logger] = []

benchmarkResults[name][logger].push({
time: parts[1].replace('ms', ''),
iterations: parts2[1].replace(':', '')
})

cb()
})

console.log('Running ' + name.toUpperCase() + ' benchmark\n')
var benchmark = spawn(
process.argv[0],
[path.join(benchmarkDir, benchmarks[name])]
)

benchmark.stdout.pipe(process.stdout)
pump(benchmark.stdout, split(), processor)

benchmark.on('exit', function () {
console.log('')
if (done && typeof done === 'function') done(null, benchmarkResults)
})
}

function sum (ar) {
var result = 0
for (var i = 0; i < ar.length; i += 1) {
result += Number.parseFloat(ar[i].time)
}
return result
}

function displayResults (results) {
console.log('==========')
var benchNames = Object.keys(results)
for (var i = 0; i < benchNames.length; i += 1) {
console.log(benchNames[i] + ' averages')
var benchmark = results[benchNames[i]]
var loggers = Object.keys(benchmark)
for (var j = 0; j < loggers.length; j += 1) {
var logger = benchmark[loggers[j]]
var average = sum(logger) / logger.length
console.log(loggers[j] + ' average: ' + average)
}
}
console.log('==========')
}

function toBench (done) {
runBenchmark(this.name, done)
}

var benchQueue = []
if (selectedBenchmark !== 'all') {
benchQueue.push(toBench.bind({name: selectedBenchmark}))
} else {
var keys = Object.keys(benchmarks)
for (var i = 0; i < keys.length; i += 1) {
selectedBenchmark = keys[i]
benchQueue.push(toBench.bind({name: selectedBenchmark}))
}
}
steed.series(benchQueue, function (err, results) {
if (err) return console.error(err.message)
results.forEach(displayResults)
})
16 changes: 16 additions & 0 deletions benchmarks/usage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Pino Benchmarks

To run a benchmark, specify which to run:

・all ⁃ run all benchmarks (takes a while)
・basic ⁃ log a simple string
・object ⁃ logging a basic object
・deepobject ⁃ logging a large object
・multiarg ⁃ multiple log method arguments
・child ⁃ child from a parent
・grandchild ⁃ child from a child
・conception ⁃ child constructor

Example:

node runbench basic
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
"scripts": {
"browser-test": "zuul tape test/browser.test.js --local",
"test": "standard && tap --no-cov test/*test.js",
"ci": "standard && tap --cov test/*test.js"
"ci": "standard && tap --cov test/*test.js",
"bench-all": "node benchmarks/runbench all",
"bench-basic": "node benchmarks/runbench basic",
"bench-object": "node benchmarks/runbench object",
"bench-deepobject": "node benchmarks/runbench deepobject",
"bench-multiarg": "node benchmarks/runbench multiarg",
"bench-child": "node benchmarks/runbench child",
"bench-grandchild": "node benchmarks/runbench grandchild",
"bench-conception": "node benchmarks/runbench conception"
},
"precommit": "test",
"repository": {
Expand Down Expand Up @@ -40,9 +48,12 @@
"log": "^1.4.0",
"loglevel": "^1.4.0",
"pre-commit": "^1.1.2",
"pump": "^1.0.1",
"standard": "^8.0.0",
"steed": "^1.1.3",
"tap": "^7.0.0",
"tape": "^4.6.2",
"through2": "^2.0.1",
"winston": "^2.1.1",
"zuul": "^3.11.1"
},
Expand Down