Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Oct 13, 2014
1 parent 4a776e2 commit 8163c78
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 198 deletions.
98 changes: 20 additions & 78 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@
return nextBindingText(false, oneTime, expression);
}

function MDVBenchmark(testDiv, width, depth, decoration, instanceCount,
oneTimeBindings, compoundBindings, expressions) {
function MDVBenchmark(testDiv, density, width, depth, decoration,
instanceCount, oneTimeBindings, compoundBindings,
expressions) {
Benchmark.call(this);
this.testDiv = testDiv;
this.density = density;
this.width = width;
this.depth = depth;
this.decoration = decoration;
Expand Down Expand Up @@ -178,32 +180,26 @@
}
},

setupTest: function(density) {
setup: function() {
if (this.fragment) {
return;
}

// |decoration| attributes on each element in each depth
var bindingCount = this.decoration *
(Math.pow(this.width, this.depth) - 1) * this.width;
// if |decoration| >= 1, one binding for each text node at each depth.
if (this.decoration > 0)
bindingCount += Math.pow(this.width, this.depth) - 1;

this.bindingCount = Math.round(bindingCount * density);
this.bindingCount = Math.round(bindingCount * this.density);
this.bindingCounter = 0;
this.propNameCounter = 0;
this.fragment = document.createDocumentFragment();
this.buildFragment(this.fragment, this.width, this.depth, this.decoration,
density);
},

teardownTest: function(density) {
this.fragment = undefined;
},

setupMDVVariant: function() {
if (testDiv.childNodes.length > 1)
alert('Failed to cleanup last test');
this.density);

testDiv.innerHTML = '';
this.template = testDiv.appendChild(document.createElement('template'));
this.template = this.testDiv.appendChild(document.createElement('template'));
HTMLTemplateElement.decorate(this.template);
if (this.expressions)
this.template.bindingDelegate = new PolymerExpressions;
Expand All @@ -212,75 +208,21 @@
this.template.setAttribute('repeat', '');
},

runMDV: function() {
test: function() {
this.template.model = this.flip ? this.ping : this.pong;
this.flip = !this.flip;
},

teardownMDVVariant: function() {
if (this.template)
dispose: function() {
if (this.template) {
this.template.clear();
this.template = undefined;
testDiv.innerHTML = '';
},

setupHandlebarsVariant: function() {
testDiv.innerHTML = '';
var div = document.createElement('div');
div.appendChild(this.fragment.cloneNode(true));
var stringTemplate = '{{#each this}}' + div.innerHTML + '{{/each}}';
stringTemplate = stringTemplate.replace(/\[\[/g, '{{');
stringTemplate = stringTemplate.replace(/\]\]/g, '}}');
this.compiledTemplate = Handlebars.compile(stringTemplate);
},

runHandlebars: function() {
testDiv.innerHTML = '';
testDiv.innerHTML = this.compiledTemplate(this.flip ?
this.ping : this.pong);
if (!testDiv.querySelectorAll('div').length)
console.error('Foo');
this.flip = !this.flip;
},

teardownHandlebarsVariant: function() {
testDiv.innerHTML = '';
},

setupVariant: function(testType) {
switch (testType) {
case 'MDV':
this.setupMDVVariant();
break;
case 'Handlebars':
this.setupHandlebarsVariant();
break;
if (this.testDiv.childNodes.length > 1)
alert('Failed to cleanup last test');
}
},

run: function(testType) {
switch (testType) {
case 'MDV':
this.runMDV();
break;
case 'Handlebars':
this.runHandlebars();
break;
}
},

teardownVariant: function(testType) {
switch (testType) {
case 'MDV':
this.teardownMDVVariant();
break;
case 'Handlebars':
this.teardownHandlebarsVariant();
break;
}
},

destroy: function() {}
this.template = undefined;
this.testDiv.innerHTML = '';
}
});

global.MDVBenchmark = MDVBenchmark;
Expand Down
38 changes: 19 additions & 19 deletions benchmark/d8_benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
// Code distributed by Google as part of the polymer project is also
// subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt

// Flags: --allow-natives-syntax
var console = {
log: print,
error: print
};

var requestAnimationFrame = function(callback) {
callback();
}

var testDiv = document.createElement('div');
var width = 2;
Expand All @@ -16,28 +23,21 @@ var oneTime = false;
var compoundBindings = false;
var expressionCheckbox = false;
var bindingDensities = [0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1];
var testTypes = ['MDV'];

function benchmarkComplete(results) {
print('benchmarkComplete');
print(JSON.stringify(results));
console.log(JSON.stringify(results));
}

function updateStatus(density, testType, runCount) {
print('updateStatus');
print(testType + ' ' + (100 * density) +
'% binding density, ' + runCount + ' runs');
function updateStatus(b, variation, runCount) {
console.log((100 * b.density) + '% binding density, ' + runCount + ' runs');
}

var test = new MDVBenchmark(testDiv, width, depth, decoration, instanceCount,
oneTime,
compoundBindings,
expressionCheckbox);
var benchmarks = bindingDensities.map(function(density) {
return new MDVBenchmark(testDiv, density, width, depth, decoration,
instanceCount,
oneTime,
compoundBindings,
expressionCheckbox);
});

var runner = new BenchmarkRunner(test,
bindingDensities,
testTypes,
benchmarkComplete,
updateStatus);
runner.go();
runTimeouts();
Benchmark.all(benchmarks, 0, updateStatus).then(benchmarkComplete);
18 changes: 0 additions & 18 deletions benchmark/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@
// Code distributed by Google as part of the polymer project is also
// subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt

var console = {
log: print,
error: print
};

var timeouts = []
function setTimeout(fn) {
timeouts.push(fn);
}

function runTimeouts() {
while (timeouts.length) {
if (Object.deliverAllChangeRecords)
Object.deliverAllChangeRecords();
timeouts.shift()();
}
}

function Window() {}

var window = new Window();
Expand Down
106 changes: 23 additions & 83 deletions benchmark/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<title>Template instantiation benchmarks</title>
<meta charset="utf-8">
<script src="../load.js"></script>
<script src="../node_modules/handlebars/dist/handlebars.js"></script>
<script src="../../observe-js/benchmark/chartjs/Chart.js"></script>
<script src="../../observe-js/benchmark/benchmark.js"></script>
<script src="../../polymer-expressions/third_party/esprima/esprima.js"></script>
Expand Down Expand Up @@ -58,13 +57,9 @@ <h1>Template instantiation benchmarks</h1>
</select>
<br>
<span>Binding Density (%): </span>
<input id="bindingDensityInput" style="width: 200px" value="0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100"><br>

MDV: <input id="mdvCheckbox" type="checkbox" checked>
Handlebars: <input id="handlebarsCheckbox" type="checkbox" checked>

<input id="bindingDensityInput" style="width: 200px" value="0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100">
<button id="go">Run Benchmarks</button><br>
One-time Bindings: <input id="oneTimeBindings" type="checkbox" checked>
One-time Bindings: <input id="oneTimeBindings" type="checkbox">
Compound Bindings: <input id="compoundBindings" type="checkbox">
Expressions: <input id="expressions" type="checkbox">
<span id="statusSpan"></span>
Expand Down Expand Up @@ -97,9 +92,6 @@ <h3 style="margin-left: 440px">Binding Density</h3>
var decorationSelect = document.getElementById('decorationSelect');
var instanceCountSelect = document.getElementById('instanceCountSelect');

var mdvCheckbox = document.getElementById('mdvCheckbox');
var handlebarsCheckbox = document.getElementById('handlebarsCheckbox');

var compoundCheckbox = document.getElementById('compoundBindings');
var oneTimeBindings = document.getElementById('oneTimeBindings');
var expressionCheckbox = document.getElementById('expressions');
Expand All @@ -113,41 +105,8 @@ <h3 style="margin-left: 440px">Binding Density</h3>
var configSelect = document.getElementById('configSelect');

var testDiv = document.getElementById('testDiv');

function changeBenchmark() {
benchmark = window[benchmarkSelect.value];
configSelect.textContent = '';
benchmark.configs.forEach(function(config) {
var option = document.createElement('option');
option.textContent = config;
configSelect.appendChild(option);
});
}

var ul = document.getElementById('legendList');
var colors = [
[100,149,237],
[220,20,60],
].map(function(rgb) {
return 'rgba(' + rgb.join(',') + ',.7)';
});

expressionCheckbox.addEventListener('click', function() {
if (expressionCheckbox.checked)
handlebarsCheckbox.checked = false;
})

oneTimeBindings.addEventListener('click', function() {
if (!oneTimeBindings.checked)
handlebarsCheckbox.checked = false;
})

handlebarsCheckbox.addEventListener('click', function() {
if (handlebarsCheckbox.checked) {
expressionCheckbox.checked = false;
oneTimeBindings.checked = true;
}
})
var rgbColor = 'rgba(100, 149, 237, .7)';

goButton.addEventListener('click', function() {
goButton.disabled = true;
Expand All @@ -158,18 +117,11 @@ <h3 style="margin-left: 440px">Binding Density</h3>
return Number(val) / 100;
});

var testTypes = [];
if (mdvCheckbox.checked)
testTypes.push('MDV');
if (handlebarsCheckbox.checked)
testTypes.push('Handlebars');

testTypes.forEach(function(testType, i) {
var li = document.createElement('li');
li.textContent = testType;
li.style.color = colors[i];
ul.appendChild(li);
});

var li = document.createElement('li');
li.textContent = 'mdv';
li.style.color = rgbColor;
ul.appendChild(li);

function benchmarkComplete(results) {
if (Observer._allObserversCount > 4)
Expand All @@ -185,21 +137,16 @@ <h3 style="margin-left: 440px">Binding Density</h3>
return density * 100 + '%';
});

var timesArray = [];
for (var i = 0; i < results[0].length; i++) {
timesArray.push(results.map(function(result) {
return result[i];
}));
}
var timesArray = [results];

var ctx = timesCanvas.getContext("2d");
new Chart(ctx).Line({
labels: labels,
datasets: timesArray.map(function(times, i) {
return {
fillColor: 'rgba(255, 255, 255, 0)',
strokeColor: colors[i],
pointColor: colors[i],
strokeColor: rgbColor,
pointColor: rgbColor,
pointStrokeColor: "#fff",
data: times
};
Expand All @@ -210,34 +157,27 @@ <h3 style="margin-left: 440px">Binding Density</h3>

goButton.disabled = false;
goButton.textContent = 'Run Benchmarks';
updateStatus();
statusSpan.textContent = 'finished';
}

function updateStatus(density, testType, runCount) {
if (!testType) {
statusSpan.textContent = '';
return;
}

statusSpan.textContent = testType + ' ' + (100 * density) +
'% binding density, ' + runCount + ' runs';
function updateStatus(b, variation, runCount) {
statusSpan.textContent = (100 * b.density) + '% binding density, ' + runCount + ' runs';
}

var width = Number(widthSelect.value);
var depth = Number(depthSelect.value);
var decoration = Number(decorationSelect.value);
var instanceCount = Number(instanceCountSelect.value);

var test = new MDVBenchmark(testDiv, width, depth, decoration, instanceCount,
oneTimeBindings.checked,
compoundBindings.checked,
expressionCheckbox.checked);
var runner = new BenchmarkRunner(test,
bindingDensities,
testTypes,
benchmarkComplete,
updateStatus);
runner.go();
var benchmarks = bindingDensities.map(function(density) {
return new MDVBenchmark(testDiv, density, width, depth, decoration,
instanceCount,
oneTimeBindings.checked,
compoundBindings.checked,
expressionCheckbox.checked);
});

Benchmark.all(benchmarks, 0, updateStatus).then(benchmarkComplete);
});
</script>
</body>
Expand Down

0 comments on commit 8163c78

Please sign in to comment.