diff options
author | kpdecker <kpdecker@gmail.com> | 2013-08-31 12:51:37 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2013-08-31 12:51:37 -0500 |
commit | 06e2a441db19eece6725b6429f4a4e14bafcfda7 (patch) | |
tree | 32ad1ec5503d5e0552be9f472c1bd8e6eb9bbd32 /bench | |
parent | 035449fe02bb9e99fa7ffecabef95a0a3573393d (diff) | |
download | handlebars.js-06e2a441db19eece6725b6429f4a4e14bafcfda7.zip handlebars.js-06e2a441db19eece6725b6429f4a4e14bafcfda7.tar.gz handlebars.js-06e2a441db19eece6725b6429f4a4e14bafcfda7.tar.bz2 |
Move scaled output to benchwarmer
Diffstat (limited to 'bench')
-rw-r--r-- | bench/throughput.js | 172 | ||||
-rw-r--r-- | bench/util/benchwarmer.js | 91 | ||||
-rw-r--r-- | bench/util/template-runner.js | 27 |
3 files changed, 162 insertions, 128 deletions
diff --git a/bench/throughput.js b/bench/throughput.js index 67c2290..277f112 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) { - return; - } + if (handlebarsOnly) { + return; + } - if (dust) { - if (template.dust) { - dustOut = false; - dust.loadSource(dust.compile(template.dust, templateName)); + if (dust) { + if (template.dust) { + dustOut = false; + dust.loadSource(dust.compile(template.dust, templateName)); - dust.render(templateName, context, function(err, out) { dustOut = out; }); + dust.render(templateName, context, function(err, out) { dustOut = out; }); - bench("dust", function() { - dust.render(templateName, context, function(err, out) { }); - }); - } else { - bench('dust', error); - } + 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); + if (eco) { + if (template.eco) { + var ecoTemplate = eco.compile(template.eco); - ecoOut = ecoTemplate(context); + ecoOut = ecoTemplate(context); - bench("eco", function() { - ecoTemplate(context); - }); - } else { - bench("eco", error); - } + bench("eco", function() { + ecoTemplate(context); + }); + } else { + bench("eco", error); } + } - if (Mustache) { - var mustacheSource = template.mustache, - mustachePartials = partials && partials.mustache; + if (Mustache) { + var mustacheSource = template.mustache, + mustachePartials = partials && partials.mustache; - if (mustacheSource) { - mustacheOut = Mustache.to_html(mustacheSource, context, mustachePartials); + if (mustacheSource) { + mustacheOut = Mustache.to_html(mustacheSource, context, mustachePartials); - bench("mustache", function() { - Mustache.to_html(mustacheSource, context, mustachePartials); - }); - } else { - bench("mustache", error); - } + 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; - } + // 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, ''); + b = b.replace(/\s/g, ''); - if (handlebarsOut !== b) { - throw new Error('Template output mismatch: ' + name - + '\n\nHandlebars: ' + handlebarsOut - + '\n\n' + lang + ': ' + b); - } + 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 a0bd424..9b77f79 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; - - for(i=0, l=names.length; i<l; i++) { - var name = names[i]; - - if(benchSize < name.length) { benchSize = name.length; } - } - - this.nameSize = benchSize + 2; - this.benchSize = 20; - var horSize = 0; - - this.startLine("ops/msec"); - horSize = horSize + this.benchSize; - for(i=0, l=names.length; i<l; i++) { - print(names[i] + new Array(this.benchSize - names[i].length + 1).join(" ")); - horSize = horSize + this.benchSize; - } - print("WINNER(S)"); - horSize = horSize + "WINNER(S)".length; + bench: function(callback) { + var self = this; - print("\n" + new Array(horSize + 1).join("-")); + this.printHeader('ops/msec', true); Benchmark.invoke(this.benchmarks, { name: "run", onComplete: function() { + self.scaleTimes(); + self.startLine(''); + print('\n'); + self.printHeader('scaled'); + _.each(self.scaled, function(value, name) { + self.startLine(name); + + _.each(self.names, function(lang) { + self.writeValue(value[lang] || ''); + }); + }); + var errors = false, prop, bench; for(prop in self.errors) { if (self.errors.hasOwnProperty(prop) @@ -112,6 +105,46 @@ BenchWarmer.prototype = { print("\n"); }, + + scaleTimes: function() { + var scaled = this.scaled = {}; + _.each(this.times, function(times, name) { + var output = scaled[name] = {}; + + _.each(times, function(time, lang) { + output[lang] = ((time - this.minimum) / (this.maximum - this.minimum) * 100).toFixed(2); + }, this); + }, this); + }, + + printHeader: function(title, winners) { + var benchSize = 0, names = this.names, i, l; + + for(i=0, l=names.length; i<l; i++) { + var name = names[i]; + + if(benchSize < name.length) { benchSize = name.length; } + } + + this.nameSize = benchSize + 2; + this.benchSize = 20; + var horSize = 0; + + this.startLine(title); + horSize = horSize + this.benchSize; + for(i=0, l=names.length; i<l; i++) { + this.writeValue(names[i]); + horSize = horSize + this.benchSize; + } + + if (winners) { + print("WINNER(S)"); + horSize = horSize + "WINNER(S)".length; + } + + print("\n" + new Array(horSize + 1).join("-")); + }, + startLine: function(name) { var winners = Benchmark.map(this.winners(this.currentBenches), function(bench) { return bench.name.split(": ")[1]; @@ -121,9 +154,10 @@ BenchWarmer.prototype = { print(winners.join(", ")); print("\n"); - var padding = Math.max(this.benchSize - name.length + 1, 0); - name = name + new Array(padding).join(" "); - print(name); + + if (name) { + this.writeValue(name); + } }, writeBench: function(bench) { var out; @@ -151,7 +185,10 @@ BenchWarmer.prototype = { out = 'E'; } } + this.writeValue(out); + }, + writeValue: function(out) { var padding = this.benchSize - out.length + 1; out = out + new Array(padding).join(" "); print(out); diff --git a/bench/util/template-runner.js b/bench/util/template-runner.js new file mode 100644 index 0000000..8863ccd --- /dev/null +++ b/bench/util/template-runner.js @@ -0,0 +1,27 @@ +var _ = require('underscore'), + BenchWarmer = require('./benchwarmer'), + templates = require('../templates'); + +module.exports = function(grunt, makeSuite, callback) { + 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; + } + + warmer.suite(name, function(bench) { + makeSuite(bench, name, template, handlebarsOnly); + }); + }); + + warmer.bench(function() { + callback && callback(warmer.times, warmer.scaled); + }); +}; |