Move scaled output to benchwarmer

This commit is contained in:
kpdecker
2013-08-31 12:51:37 -05:00
parent 035449fe02
commit 06e2a441db
3 changed files with 173 additions and 139 deletions
+82 -112
View File
@@ -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);
});
};
+64 -27
View File
@@ -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<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;
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);
+27
View File
@@ -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);
});
};