Address comments

Add a basic parser benchmark

This CL adds a basic benchmark for the parser. There isn't a direct way to
exercise the parser in Sky, so we use imports.

This CL also adds a load event to <import> to determine when the import is done
loading.

R=eseidel@chromium.org

parser benchmark wip
This commit is contained in:
Adam Barth 2014-11-04 11:00:32 -08:00
parent d1382e4d41
commit 49e1436a68
4 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,22 @@
<import src="../resources/runner.sky" as="PerfRunner" />
<script>
var specURL = "resources/html5.html";
var cacheBust = 100;
var runner = new PerfRunner({
iterations: 10,
unit: 'ms',
});
runner.runAsync(function(done) {
var element = document.createElement("import");
element.addEventListener("load", function() {
element.remove();
done();
});
element.setAttribute("src", specURL + "?cacheBust=" + cacheBust++);
document.documentElement.appendChild(element);
});
</script>

View File

@ -0,0 +1,81 @@
<script>
function PerfRunner(options) {
this.unit_ = options.unit || "ms";
this.iterationsRemaining_ = options.iterations || 10;
this.results_ = [];
}
PerfRunner.prototype.log = function(line) {
console.log(line);
};
PerfRunner.prototype.recordResult = function(result) {
console.log(result);
this.results_.push(result);
};
PerfRunner.prototype.runAsync = function(test) {
var self = this;
window.setTimeout(function() {
var startTime = Date.now();
test(function() {
var endTime = Date.now();
self.recordResult(endTime - startTime);
if (--self.iterationsRemaining_ > 0)
self.runAsync(test);
else
self.finish();
});
});
};
PerfRunner.prototype.computeStatistics = function() {
var data = this.results_.slice();
// Add values from the smallest to the largest to avoid the loss of significance
data.sort(function(a, b) { return a - b; });
var middle = Math.floor(data.length / 2);
var stats = {
min: data[0],
max: data[data.length - 1],
median: data.length % 2 ? data[middle] : (data[middle - 1] + data[middle]) / 2,
};
// Compute the mean and variance using Knuth's online algorithm (has good numerical stability).
var squareSum = 0;
stats.values = this.results_;
stats.mean = 0;
for (var i = 0; i < data.length; ++i) {
var x = data[i];
var delta = x - stats.mean;
var sweep = i + 1.0;
stats.mean += delta / sweep;
squareSum += delta * (x - stats.mean);
}
stats.variance = data.length <= 1 ? 0 : squareSum / (data.length - 1);
stats.stdev = Math.sqrt(stats.variance);
stats.unit = this.unit_;
return stats;
};
PerfRunner.prototype.logStatistics = function(title) {
var stats = this.computeStatistics();
this.log("");
this.log(title);
if (stats.values)
this.log("values " + stats.values.join(", ") + " " + stats.unit);
this.log("avg " + stats.mean + " " + stats.unit);
this.log("median " + stats.median + " " + stats.unit);
this.log("stdev " + stats.stdev + " " + stats.unit);
this.log("min " + stats.min + " " + stats.unit);
this.log("max " + stats.max + " " + stats.unit);
};
PerfRunner.prototype.finish = function () {
this.logStatistics("Time:");
}
module.exports = PerfRunner;
</script>

View File

@ -0,0 +1,2 @@
PASS: Load event fired.
PASS: pass.sky succesfully exported this string.

View File

@ -0,0 +1,17 @@
<html>
<import src="../resources/dump-as-text.sky" />
<div id="result1">FAIL</div>
<div id="result2">FAIL</div>
<script>
var element = document.createElement("import");
element.setAttribute("src", "resources/pass.sky");
element.setAttribute("as", "hello");
element.addEventListener("load", function() {
document.getElementById("result1").textContent = "PASS: Load event fired.";
});
document.documentElement.appendChild(element);
</script>
<script>
document.getElementById("result2").textContent = hello;
</script>
</html>