profiler.html revision e9ee71cc3e50c936d1f244bb111ef0524037b397
<html>
<head>
<title>profiler tests</title>
</head>
<body class="yui-skin-sam">
<h1>Profiler Tests</h1>
<div id="c"></div>
<script type="text/javascript">
//global function
function getMessage(){
return "hi";
}
//global object
var myObject = {
getName : function(){
return "myObject";
}
};
YUI({
base: '/build/',
filter: "debug",
logInclude: { TestRunner: true }
}).use('test', 'console', 'profiler', function (Y) {
Y.namespace("Tests");
Y.Tests.Profiler = (function(){
var Assert = Y.Assert;
var Profiler = Y.Profiler;
//-------------------------------------------------------------------------
// Information needed to run tests
//-------------------------------------------------------------------------
var testObject = {
factorial : function (num){
if (num > 1) {
return num * testObject.factorial(num-1);
} else {
return 1;
}
},
add : function (iterations){
var sum = 0;
for (var i=0; i < iterations; i++){
sum++;
}
}
};
var root = {};
this.name = "SuperType";
}
root.SuperType.prototype.getName = function(){
return this.name;
};
this.age = 29;
}
getAge : function (){
return this.age;
}
});
//-------------------------------------------------------------------------
// Base Test Suite
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Test Case for basic function profiling
//-------------------------------------------------------------------------
name : "Profiler Registration Tests",
tearDown: function(){
},
//---------------------------------------------------------------------
// Special assertions for profiler
//---------------------------------------------------------------------
assertFunctionIsRegistered : function (fullFuncName, shortFuncName, owner, originalFunction, newFunction){
Assert.areEqual(originalFunction, containerFunc, "Original " + fullFuncName + " function was not stored.");
Assert.areEqual(originalFunction.prototype, containerFunc.prototype, fullFuncName + " prototype was not copied.");
Assert.areEqual(shortFuncName, containerFunc.__yuiFuncName, fullFuncName + " function name was not stored.");
Assert.isObject(Profiler.getReport(fullFuncName), "Report for " + fullFuncName + " function was not created.");
},
assertFunctionIsNotRegistered : function(fullFuncName, shortFuncName, owner, originalFunction, newFunction){
Assert.areEqual(originalFunction, newFunction, "Original " + fullFuncName + " function was not placed back on owner.");
Assert.isUndefined(Profiler.getOriginal(fullFuncName), "Container for original " + fullFuncName + " function was not destroyed.");
},
//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------
testInstrument: function(){
var original = function() { return "Hello"; };
},
testRegisterFunction : function (){
var originalFunction = testObject.factorial;
var fullFuncName = "testObject.factorial";
this.assertFunctionIsRegistered(fullFuncName, "factorial", testObject, originalFunction, testObject.factorial);
Profiler.unregisterFunction(fullFuncName);
this.assertFunctionIsNotRegistered(fullFuncName, "factorial", testObject, originalFunction, testObject.factorial);
},
testRegisterGlobalFunction : function (){
var originalFunction = getMessage;
var fullFuncName = "getMessage";
Profiler.unregisterFunction(fullFuncName);
this.assertFunctionIsNotRegistered(fullFuncName, "getMessage", window, originalFunction, getMessage);
},
testRegisterGlobalFunctionOnGlobalObject : function (){
var originalFunction = myObject.getName;
var fullFuncName = "myObject.getName";
Profiler.registerFunction(fullFuncName);
this.assertFunctionIsRegistered(fullFuncName, "getName", myObject, originalFunction, myObject.getName);
Profiler.unregisterFunction(fullFuncName);
this.assertFunctionIsNotRegistered(fullFuncName, "getName", myObject, originalFunction, myObject.getName);
},
testRegisterObject : function (){
var funcNames = [];
var originalFuncs = {};
//get all methods
for (var propName in testObject){
if (typeof propName == "function"){
funcNames.push(propName);
originalFuncs[propName] = testObject[propName];
}
}
//check each method
var fullFuncName = "testobject." + funcNames[i];
var originalFunction = originalFuncs[funcNames[i]];
this.assertFunctionIsRegistered(fullFuncName, funcNames[i], testObject, originalFunction, testObject[funcNames[i]]);
}
Profiler.unregisterObject("testObject");
//check each method
for (var i=0; i < funcNames.length; i++){
var fullFuncName = "testobject." + funcNames[i];
var originalFunction = originalFuncs[funcNames[i]];
this.assertFunctionIsNotRegistered(fullFuncName, funcNames[i], testObject, originalFunction, testObject[funcNames[i]]);
}
},
testRegisterConstructor : function (){
//gather stuff on the prototype
var funcNames = [];
var originalFuncs = {};
funcNames.push(prop);
}
}
this.assertFunctionIsRegistered("root.SuperType", "SuperType", root, originalConstructor, root.SuperType);
//check each method
var fullFuncName = "root.SuperType.prototype." + funcNames[i];
var originalFunction = originalFuncs[funcNames[i]];
this.assertFunctionIsRegistered(fullFuncName, funcNames[i], root.SuperType.prototype, originalFunction, root.SuperType.prototype[funcNames[i]]);
}
//check each method
for (var i=0; i < funcNames.length; i++){
var originalFunction = originalFuncs[funcNames[i]];
this.assertFunctionIsNotRegistered(fullFuncName, funcNames[i], root.SuperType.prototype, originalFunction, root.SuperType.prototype[funcNames[i]]);
}
this.assertFunctionIsNotRegistered("root.SuperType", "SuperType", root, originalConstructor, root.SuperType);
},
testRegisterConstructorWithInheritance : function (){
//gather stuff on the prototype
var funcNames = [];
var originalFuncs = {};
funcNames.push(prop);
}
}
this.assertFunctionIsRegistered("root.SubType", "SubType", root, originalConstructor, root.SubType);
//check the superclass property
Assert.areEqual(root.SuperType, root.SubType.superclass.constructor, "SubType superclass constructor should be SuperType.");
//check each method
var fullFuncName = "root.SubType.prototype." + funcNames[i];
var originalFunction = originalFuncs[funcNames[i]];
this.assertFunctionIsRegistered(fullFuncName, funcNames[i], root.SubType.prototype, originalFunction, root.SubType.prototype[funcNames[i]]);
}
//check each method
for (var i=0; i < funcNames.length; i++){
var originalFunction = originalFuncs[funcNames[i]];
this.assertFunctionIsNotRegistered(fullFuncName, funcNames[i], root.SubType.prototype, originalFunction, root.SubType.prototype[funcNames[i]]);
}
this.assertFunctionIsNotRegistered("root.SubType", "SubType", root, originalConstructor, root.SubType);
},
testFunctionAccuracy1 : function (){
},
testFunctionAccuracy2 : function (){
var age = o.getAge();
var name = o.getName();
age = o2.getAge();
name = o2.getName();
}
}));
//-------------------------------------------------------------------------
// Test Case for stopwatch functions
//-------------------------------------------------------------------------
name : "Profiler Stopwatch Tests",
tearDown: function(){
},
testStartStop: function (){
for (var i=0; i < 10000; i++){}
var report = Profiler.getReport("random.entry");
Assert.isObject(report, "Report should be an object.");
Assert.areEqual(1, report.calls, "Call count should be 1.");
Assert.isNumber(report.max, "Max should be a number.");
Assert.isNumber(report.min, "Min should be a number.");
Assert.isNumber(report.avg, "Average should be a number.");
},
testStartStopPause : function (){
for (var i=0; i < 10000; i++){}
for (var i=0; i < 10000; i++){}
for (var i=0; i < 10000; i++){}
},
testStartTwice : function () {
for (var i=0; i < 10000; i++){}
for (var i=0; i < 10000; i++){}
}
}));
//-------------------------------------------------------------------------
// Test Case for report data
//-------------------------------------------------------------------------
name : "Profiler Report Data Tests",
testGetReport : function (){
testObject.factorial(10);
},
testGetCallCount : function (){
testObject.factorial(10);
},
testGetReport : function () {
o.getAge();
o.getName();
o2.getAge();
o2.getName();
o2.getAge();
o2.getName();
o2.getName();
var report = Profiler.getFullReport();
//Assert.isObject(report["root.SubType.prototype"], "There should be an entry for root.SubType.prototype.");
Assert.areEqual(3, report["root.SubType.prototype.getAge"].calls, "getAge() should have a call count of 3.");
Assert.isObject(report["root.SubType.prototype.getName"], "There should be an entry for getName()");
Assert.areEqual(4, report["root.SubType.prototype.getName"].calls, "getName() should have a call count of 4.");
}
}));
//return it
return suite;
})();
verbose : true,
//consoleLimit : 10,
newestOnTop : false
});
r.render('#c');
//add to the testrunner and run
/*if (parent && parent != window) {
} else {
}*/
});
</script>
</body>
</html>