Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reporter which generates Github-flavoured markup tables.
- Loading branch information
1 parent
6f34181
commit 0eda5c0
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |