Skip to content

Commit

Permalink
Add reporter which generates Github-flavoured markup tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
davedoesdev committed Aug 11, 2013
1 parent 6f34181 commit 0eda5c0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/reporters/gfm.js
@@ -0,0 +1,45 @@

/**
* prints results in a Github-flavored markdown table. Only works
* with batch benchmark data
*/

function Reporter(){
this.out = process.stdout
}

Reporter.prototype.report = function(name, results, iterations){
var out = this.out

out.write('\n' + name + ' x' + commaGroups(iterations))
out.write('|total (ms)|average (ns)| diff (%)\n')
out.write(':--|--:|--:|--:\n')

results.sort(function (a,b) {
return a.total - b.total
}).forEach(function(r) {
out.write(r.name);
out.write('|' + formatNumber(r.total / 1e6, 0))
out.write('|' + formatNumber(r.total / iterations, 0))
// diff to fastest
out.write('|' + formatNumber(difference(results[0].total, r.total), 0))
out.write('\n')
})
}

function difference(r1, r2) {
// can't devide by zero
return ((r2 - r1) / (r1 || 1)) * 100
}

function formatNumber(x, n) {
return x === 0 ? '-' : commaGroups(x.toFixed(n))
}

function commaGroups(value) {
var parts = value.toString().split('.')
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.')
}

module.exports = Reporter

0 comments on commit 0eda5c0

Please sign in to comment.