Skip to content

Commit

Permalink
Use iframes to isolate each library so they don't clash with each other.
Browse files Browse the repository at this point in the history
Note: Rather than completely refactor the code this version takes
the data from the iframe and then recreates the data that used to be
created by all the calls to testSet inside matrix_benchmark.html

It's probably not the cleanest way to do it but it does mean the
least amount of changes.

Other changes will be needed to make it work in browsers
that don't support TypedArrays.
  • Loading branch information
greggman committed Jun 3, 2011
1 parent 926da86 commit 96d6857
Show file tree
Hide file tree
Showing 9 changed files with 1,395 additions and 961 deletions.
125 changes: 125 additions & 0 deletions js/test-helper.js
@@ -0,0 +1,125 @@
TestHelper = function() {

var isInIFrame = (window.location != window.parent.location) ? true : false;

function log(html) {
var div = document.createElement('div');
div.innerHTML = html;
document.body.appendChild(div);
}

function debuggerLog(obj) {
if (window.console && window.console.log) {
window.console.log(obj);
}
}

function resetPseudoRandom() {
if (window.resetPseudoRandom) {
window.resetPseudoRandom();
}
}

function test(label, f) {
// Repeats each benchmark multiple times to smooth out anomalies
// Also tracks min and max times

if(!f) {
return { avg: null, min: null, max: null };
}

var runCount = 10;
var internalRunCount = 100;
var totalIterations = 0;
var minIterations = 0;
var maxIterations = 0;
var totalTime = 0;
var minTime = 0;
var maxTime = 0;
var timePerRun = 30;
resetPseudoRandom();

for(var i = 0; i < runCount; ++i) {
var data = f(internalRunCount, -1, timePerRun);
var iterations = internalRunCount * data.loopCount;
var time = data.time;
if(i == 0) {
minIterations = iterations;
maxIterations = iterations;
minTime = time;
maxTime = time;
} else {
if(minIterations > iterations) {
minIterations = iterations;
minTime = time;
}
if(maxIterations < iterations) {
maxIterations = iterations;
maxTime = time;
}
}
totalIterations += iterations;
totalTime += time;
}

var iterationsPerSecond = Math.floor(totalIterations / (totalTime * 0.001));
var minIterationsPerSecond = Math.floor(minIterations / (minTime * 0.001));
var maxIterationsPerSecond = Math.floor(maxIterations / (maxTime * 0.001));

return { avg: iterationsPerSecond, min: minIterationsPerSecond, max: maxIterationsPerSecond, result: data.result };
}

function runTests(tests) {
// Gather the test names
var testNames = []
for (var testName in tests) {
testNames.push(testName);
}

// Run each test
function runNextTest() {
if (testNames.length) {
var testName = testNames.shift();
var testInfo = tests[testName];
var data = test(testName, testInfo.test);
if (data.avg === null) {
log('<i>' + testName + ' Unsupported</i>');
} else {
log('<i>' + label + '</i> - Avg: <b>' + data.avg + ' iterations per second</b>, Min: ' + data.min + ' iterations per second, Max: ' + data.max + ' iterations per second');
}
if (testNames.length) {
setTimeout(runNextTest, 100);
}
}
}
runNextTest();
}

function main() {
var href = window.location.href;
var slash = href.lastIndexOf('/');
var name = href.substr(slash + 1).replace('.html', '');
log('Test for: ' + name);
log('');

if (!isInIFrame) {
runTests(tests);
} else {
if (console && console.log) {
debuggerLog("loaded: " + name);
parent.childLoaded();
}
}
}

return {
main: main,
test: test,
resetPseudoRandom: resetPseudoRandom
};

}();




173 changes: 173 additions & 0 deletions libs/CanvasMatrix/CanvasMatrix.html
@@ -0,0 +1,173 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>Matrix Benchmark</title>

<!-- Common Utilities -->
<script type="text/javascript" src="../../js/rand.js"></script>

<!-- Matrix Libraries -->
<script type="text/javascript" src="CanvasMatrix.js"></script>

<!-- Benchmarking utilities -->
<script type="text/javascript" src="../../js/test-helper.js"></script>
<script type="text/javascript">

window.onload = TestHelper.main;

function radToDeg(angleInRadians) {
return angleInRadians * 180 / Math.PI;
}

function normalize(a) {
var r = [];
var n = 0.0;
var aLength = a.length;
for (var i = 0; i < aLength; ++i)
n += a[i] * a[i];
n = Math.sqrt(n);
if (n > 0.00001) {
for (var i = 0; i < aLength; ++i)
r[i] = a[i] / n;
} else {
r = [0,0,0];
}
return r;
};



function canvasMatrixRandom() {
var m = new CanvasMatrix4()
var axis = normalize([pseudoRandom(), pseudoRandom(), pseudoRandom()]);
var angle = radToDeg(pseudoRandom());
var trans = [pseudoRandom(), pseudoRandom(), pseudoRandom()];
m.translate(trans[0], trans[1], trans[2]);
m.rotate(angle, axis[0], axis[1], axis[2]);
return m;
}

var tests = {
'Multiplication':
{ test: function(count, maxCount, milliSeconds) {
var m1 = canvasMatrixRandom();
var m2 = canvasMatrixRandom();
var start = Date.now();
var loopCount = 0;
while (Date.now() - start < milliSeconds && loopCount != maxCount) {
++loopCount;
for (var i = 0; i < count; ++i) {
m1.multLeft(m2);
}
}
return {time: Date.now()-start, loopCount: loopCount, result: m1};
}},

'Translation':
{ test: function(count, maxCount, milliSeconds) {
var m1 = new CanvasMatrix4();
var m2 = canvasMatrixRandom();
var start = Date.now();
var loopCount = 0;
while (Date.now() - start < milliSeconds && loopCount != maxCount) {
++loopCount;
for (var i = 0; i < count; ++i) {
m1.translate(1, 2, 3);
}
}
m1.multRight(m2);
return {time:Date.now()-start, loopCount: loopCount, result: m1};
}},

'Scaling':
{ test: function(count, maxCount, milliSeconds) {
var m1 = new CanvasMatrix4();
var m2 = canvasMatrixRandom();
var start = Date.now();
var loopCount = 0;
while (Date.now() - start < milliSeconds && loopCount != maxCount) {
++loopCount;
for (var i = 0; i < count; ++i) {
m1.scale(1, 2, 3);
}
}
m1.multRight(m2);
return {time:Date.now()-start, loopCount: loopCount, result: m1};
}},

'Rotation (Arbitrary axis)':
{ test: function(count, maxCount, milliSeconds) {
var m1 = new CanvasMatrix4();
var m2 = canvasMatrixRandom();
var start = Date.now();
var loopCount = 0;
while (Date.now() - start < milliSeconds && loopCount != maxCount) {
++loopCount;
for (var i = 0; i < count; ++i) {
m1.rotate(90, 1, 2, 3);
}
}
m1.multRight(m2);
return {time:Date.now()-start, loopCount: loopCount, result: m1};
}},

'Rotation (X axis)':
{ test: function(count, maxCount, milliSeconds) {
var m1 = new CanvasMatrix4();
var m2 = canvasMatrixRandom();
var start = Date.now();
var loopCount = 0;
while (Date.now() - start < milliSeconds && loopCount != maxCount) {
++loopCount;
for (var i = 0; i < count; ++i) {
m1.rotate(90, 1, 0, 0);
}
}
m1.multRight(m2);
return {time:Date.now()-start, loopCount: loopCount, result: m1};
}},

'Transpose':
{ test: function(count, maxCount, milliSeconds) {
var m1 = canvasMatrixRandom();
var start = Date.now();
var loopCount = 0;
while (Date.now() - start < milliSeconds && loopCount != maxCount) {
++loopCount;
for (var i = 0; i < count; ++i) {
m1.transpose();
}
}
return {time:Date.now()-start, loopCount: loopCount, result: m1};
}},

'Inverse':
{ test: function(count, maxCount, milliSeconds) {
var m1 = new CanvasMatrix4();
m1.perspective(90, 0.5, 1, 1000);
var start = Date.now();
var loopCount = 0;
while (Date.now() - start < milliSeconds && loopCount != maxCount) {
++loopCount;
for (var i = 0; i < count; ++i) {
m1.invert();
}
}
return {time:Date.now()-start, loopCount: loopCount, result: m1};
}},

'Inverse 3x3':
{ test: null },

'Vector Transformation':
{ test: null },

};

</script>
</head>
<body>
</body>
</html>

0 comments on commit 96d6857

Please sign in to comment.