From 06e2a441db19eece6725b6429f4a4e14bafcfda7 Mon Sep 17 00:00:00 2001 From: kpdecker Date: Sat, 31 Aug 2013 12:51:37 -0500 Subject: [PATCH] Move scaled output to benchwarmer --- bench/throughput.js | 194 ++++++++++++++-------------------- bench/util/benchwarmer.js | 91 +++++++++++----- bench/util/template-runner.js | 27 +++++ 3 files changed, 173 insertions(+), 139 deletions(-) create mode 100644 bench/util/template-runner.js diff --git a/bench/throughput.js b/bench/throughput.js index 67c2290b..277f112c 100644 --- a/bench/throughput.js +++ b/bench/throughput.js @@ -1,5 +1,5 @@ var _ = require('underscore'), - BenchWarmer = require('./util/benchwarmer'), + runner = require('./util/template-runner'), templates = require('./templates'), eco, dust, Handlebars, Mustache, eco; @@ -20,133 +20,103 @@ function error() { throw new Error("EWOT"); } -function makeSuite(warmer, name, template, handlebarsOnly) { - warmer.suite(name, function(bench) { - // Create aliases to minimize any impact from having to walk up the closure tree. - var templateName = name, +function makeSuite(bench, name, template, handlebarsOnly) { + // Create aliases to minimize any impact from having to walk up the closure tree. + var templateName = name, - context = template.context, - partials = template.partials, + context = template.context, + partials = template.partials, - handlebarsOut, - dustOut, - ecoOut, - mustacheOut; + handlebarsOut, + dustOut, + ecoOut, + mustacheOut; - var handlebar = Handlebars.compile(template.handlebars, {data: false}), - options = {helpers: template.helpers}; - _.each(template.partials && template.partials.handlebars, function(partial, name) { - Handlebars.registerPartial(name, Handlebars.compile(partial, {data: false})); - }); + var handlebar = Handlebars.compile(template.handlebars, {data: false}), + options = {helpers: template.helpers}; + _.each(template.partials && template.partials.handlebars, function(partial, name) { + Handlebars.registerPartial(name, Handlebars.compile(partial, {data: false})); + }); - handlebarsOut = handlebar(context, options); - bench("handlebars", function() { - handlebar(context, options); - }); + handlebarsOut = handlebar(context, options); + bench("handlebars", function() { + handlebar(context, options); + }); - if (handlebarsOnly) { + if (handlebarsOnly) { + return; + } + + if (dust) { + if (template.dust) { + dustOut = false; + dust.loadSource(dust.compile(template.dust, templateName)); + + dust.render(templateName, context, function(err, out) { dustOut = out; }); + + bench("dust", function() { + dust.render(templateName, context, function(err, out) { }); + }); + } else { + bench('dust', error); + } + } + + if (eco) { + if (template.eco) { + var ecoTemplate = eco.compile(template.eco); + + ecoOut = ecoTemplate(context); + + bench("eco", function() { + ecoTemplate(context); + }); + } else { + bench("eco", error); + } + } + + if (Mustache) { + var mustacheSource = template.mustache, + mustachePartials = partials && partials.mustache; + + if (mustacheSource) { + mustacheOut = Mustache.to_html(mustacheSource, context, mustachePartials); + + bench("mustache", function() { + Mustache.to_html(mustacheSource, context, mustachePartials); + }); + } else { + bench("mustache", error); + } + } + + // Hack around whitespace until we have whitespace control + handlebarsOut = handlebarsOut.replace(/\s/g, ''); + function compare(b, lang) { + if (b == null) { return; } - if (dust) { - if (template.dust) { - dustOut = false; - dust.loadSource(dust.compile(template.dust, templateName)); + b = b.replace(/\s/g, ''); - dust.render(templateName, context, function(err, out) { dustOut = out; }); - - bench("dust", function() { - dust.render(templateName, context, function(err, out) { }); - }); - } else { - bench('dust', error); - } + if (handlebarsOut !== b) { + throw new Error('Template output mismatch: ' + name + + '\n\nHandlebars: ' + handlebarsOut + + '\n\n' + lang + ': ' + b); } + } - if (eco) { - if (template.eco) { - var ecoTemplate = eco.compile(template.eco); - - ecoOut = ecoTemplate(context); - - bench("eco", function() { - ecoTemplate(context); - }); - } else { - bench("eco", error); - } - } - - if (Mustache) { - var mustacheSource = template.mustache, - mustachePartials = partials && partials.mustache; - - if (mustacheSource) { - mustacheOut = Mustache.to_html(mustacheSource, context, mustachePartials); - - bench("mustache", function() { - Mustache.to_html(mustacheSource, context, mustachePartials); - }); - } else { - bench("mustache", error); - } - } - - // Hack around whitespace until we have whitespace control - handlebarsOut = handlebarsOut.replace(/\s/g, ''); - function compare(b, lang) { - if (b == null) { - return; - } - - b = b.replace(/\s/g, ''); - - if (handlebarsOut !== b) { - throw new Error('Template output mismatch: ' + name - + '\n\nHandlebars: ' + handlebarsOut - + '\n\n' + lang + ': ' + b); - } - } - - compare(dustOut, 'dust'); - compare(ecoOut, 'eco'); - compare(mustacheOut, 'mustache'); - }); + compare(dustOut, 'dust'); + compare(ecoOut, 'eco'); + compare(mustacheOut, 'mustache'); } module.exports = function(grunt, callback) { // Deferring load incase we are being run inline with the grunt build Handlebars = require('../lib/handlebars'); - var warmer = new BenchWarmer(); - - var handlebarsOnly = grunt.option('handlebars-only'), - grep = grunt.option('grep'); - if (grep) { - grep = new RegExp(grep); - } - - _.each(templates, function(template, name) { - if (!template.handlebars || (grep && !grep.test(name))) { - return; - } - - makeSuite(warmer, name, template, handlebarsOnly); - }); - - warmer.bench(function() { - grunt.log.writeln(); // Clear out any trailing contnet - - var scaled = {}; - _.each(warmer.times, function(times, name) { - var output = scaled[name] = {}; - - _.each(times, function(time, lang) { - output[lang] = ((time - warmer.minimum) / (warmer.maximum - warmer.minimum) * 100).toFixed(2); - }); - }); - grunt.log.writeln('Scaled throughput: ' + JSON.stringify(scaled, undefined, 2)); - - callback && callback(warmer.times); + runner(grunt, makeSuite, function(times, scaled) { + callback(times); }); }; diff --git a/bench/util/benchwarmer.js b/bench/util/benchwarmer.js index a0bd4240..9b77f79a 100644 --- a/bench/util/benchwarmer.js +++ b/bench/util/benchwarmer.js @@ -1,5 +1,5 @@ - -var Benchmark = require("benchmark"); +var _ = require('underscore'), + Benchmark = require("benchmark"); var BenchWarmer = function(names) { this.benchmarks = []; @@ -51,36 +51,29 @@ BenchWarmer.prototype = { this.benchmarks.push(bench); }, + bench: function(callback) { - var benchSize = 0, names = this.names, self = this, i, l; + var self = this; - for(i=0, l=names.length; i