fix benchmark pass percentage
This commit is contained in:
parent
d979d26bb4
commit
7b450bdeb9
46
test/bench.js
vendored
46
test/bench.js
vendored
@ -23,7 +23,7 @@ function load() {
|
||||
/**
|
||||
* Run all benchmarks
|
||||
*/
|
||||
function runBench(options) {
|
||||
async function runBench(options) {
|
||||
options = options || {};
|
||||
const specs = load();
|
||||
|
||||
@ -38,7 +38,7 @@ function runBench(options) {
|
||||
if (options.marked) {
|
||||
marked.setOptions(options.marked);
|
||||
}
|
||||
bench('es5 marked', specs, marked);
|
||||
await bench('es5 marked', specs, marked);
|
||||
|
||||
es6marked.setOptions({
|
||||
gfm: false,
|
||||
@ -50,7 +50,7 @@ function runBench(options) {
|
||||
if (options.marked) {
|
||||
es6marked.setOptions(options.marked);
|
||||
}
|
||||
bench('es6 marked', specs, es6marked);
|
||||
await bench('es6 marked', specs, es6marked);
|
||||
|
||||
// GFM
|
||||
marked.setOptions({
|
||||
@ -63,7 +63,7 @@ function runBench(options) {
|
||||
if (options.marked) {
|
||||
marked.setOptions(options.marked);
|
||||
}
|
||||
bench('es5 marked (gfm)', specs, marked);
|
||||
await bench('es5 marked (gfm)', specs, marked);
|
||||
|
||||
es6marked.setOptions({
|
||||
gfm: true,
|
||||
@ -75,7 +75,7 @@ function runBench(options) {
|
||||
if (options.marked) {
|
||||
es6marked.setOptions(options.marked);
|
||||
}
|
||||
bench('es6 marked (gfm)', specs, es6marked);
|
||||
await bench('es6 marked (gfm)', specs, es6marked);
|
||||
|
||||
// Pedantic
|
||||
marked.setOptions({
|
||||
@ -88,7 +88,7 @@ function runBench(options) {
|
||||
if (options.marked) {
|
||||
marked.setOptions(options.marked);
|
||||
}
|
||||
bench('es5 marked (pedantic)', specs, marked);
|
||||
await bench('es5 marked (pedantic)', specs, marked);
|
||||
|
||||
es6marked.setOptions({
|
||||
gfm: false,
|
||||
@ -100,10 +100,10 @@ function runBench(options) {
|
||||
if (options.marked) {
|
||||
es6marked.setOptions(options.marked);
|
||||
}
|
||||
bench('es6 marked (pedantic)', specs, es6marked);
|
||||
await bench('es6 marked (pedantic)', specs, es6marked);
|
||||
|
||||
try {
|
||||
bench('commonmark', specs, (() => {
|
||||
await bench('commonmark', specs, (() => {
|
||||
const commonmark = require('commonmark');
|
||||
const parser = new commonmark.Parser();
|
||||
const writer = new commonmark.HtmlRenderer();
|
||||
@ -116,7 +116,7 @@ function runBench(options) {
|
||||
}
|
||||
|
||||
try {
|
||||
bench('markdown-it', specs, (() => {
|
||||
await bench('markdown-it', specs, (() => {
|
||||
const MarkdownIt = require('markdown-it');
|
||||
const md = new MarkdownIt();
|
||||
return md.render.bind(md);
|
||||
@ -126,7 +126,7 @@ function runBench(options) {
|
||||
}
|
||||
|
||||
try {
|
||||
bench('markdown.js', specs, (() => {
|
||||
await bench('markdown.js', specs, (() => {
|
||||
const md = require('markdown').markdown;
|
||||
return md.toHTML.bind(md);
|
||||
})());
|
||||
@ -135,25 +135,23 @@ function runBench(options) {
|
||||
}
|
||||
}
|
||||
|
||||
function bench(name, specs, engine) {
|
||||
async function bench(name, specs, engine) {
|
||||
const before = process.hrtime();
|
||||
for (let i = 0; i < 1e3; i++) {
|
||||
for (const spec of specs) {
|
||||
engine(spec.markdown);
|
||||
await engine(spec.markdown);
|
||||
}
|
||||
}
|
||||
const elapsed = process.hrtime(before);
|
||||
const ms = prettyElapsedTime(elapsed).toFixed();
|
||||
|
||||
const results = [];
|
||||
let correct = 0;
|
||||
for (const spec of specs) {
|
||||
results.push({
|
||||
expected: spec.html,
|
||||
actual: engine(spec.markdown)
|
||||
});
|
||||
if (await htmlDiffer.isEqual(spec.html, engine(spec.markdown))) {
|
||||
correct++;
|
||||
}
|
||||
}
|
||||
const correct = results.reduce((num, result) => num + (htmlDiffer.isEqual(result.expected, result.actual) ? 1 : 0), 0);
|
||||
const percent = (correct / results.length * 100).toFixed(2);
|
||||
const percent = (correct / specs.length * 100).toFixed(2);
|
||||
|
||||
console.log('%s completed in %sms and passed %s%', name, ms, percent);
|
||||
}
|
||||
@ -161,13 +159,13 @@ function bench(name, specs, engine) {
|
||||
/**
|
||||
* A simple one-time benchmark
|
||||
*/
|
||||
function time(options) {
|
||||
async function time(options) {
|
||||
options = options || {};
|
||||
const specs = load();
|
||||
if (options.marked) {
|
||||
marked.setOptions(options.marked);
|
||||
}
|
||||
bench('marked', specs, marked);
|
||||
await bench('marked', specs, marked);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -263,7 +261,7 @@ function camelize(text) {
|
||||
/**
|
||||
* Main
|
||||
*/
|
||||
function main(argv) {
|
||||
async function main(argv) {
|
||||
const opt = parseArg(argv);
|
||||
|
||||
if (opt.minified) {
|
||||
@ -271,9 +269,9 @@ function main(argv) {
|
||||
}
|
||||
|
||||
if (opt.time) {
|
||||
time(opt);
|
||||
await time(opt);
|
||||
} else {
|
||||
runBench(opt);
|
||||
await runBench(opt);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user