YUI 3.x Home -

YUI Library Examples: Profiler: Simple Profiling Example

Profiler: Simple Profiling Example

This example shows basic usage of the Profiler for profiling a single function. A single function is profiled and all of the information displayed.

Simple Profiling Example

This example begins by creating a namespace:

  1. Y.namespace("example.profiler");
Y.namespace("example.profiler");

This namespace serves as the core object upon which others will be added (to prevent creating global objects).

Next, an object is created with a method:

  1. //object with method to profile
  2. Y.example.profiler.MathHelper = {
  3. factorial : function (num){
  4. if (num > 1) {
  5. return num * MathHelper.factorial(num-1);
  6. } else {
  7. return 1;
  8. }
  9. }
  10. };
//object with method to profile
Y.example.profiler.MathHelper = {
    factorial : function (num){
        if (num > 1) {
            return num * MathHelper.factorial(num-1);
        } else {
            return 1;
        }
    }
};

This object, MathHelper contains a single method called factorial() that computes the factorial of a given number. Any time factorial() is called, the argument indicates how many times the function will be recursively called. For example, factorial(10) results in the funtion being called 10 times. This makes it an ideal test case for profiling because the results are so predictable.

Registering the function

The most important step to profile this function is to call registerFunction() with the fully-qualified function name, which is Y.example.profiler.MathHelper, and the object:

  1. Y.Profiler.registerFunction("Y.example.profiler.MathHelper.factorial", Y.example.profiler.MathHelper);
Y.Profiler.registerFunction("Y.example.profiler.MathHelper.factorial", Y.example.profiler.MathHelper);

Since this function is not fully accessible in the global scope, the owner object must be passed in as the second argument.

Running the example

With everything setup, the last step is to run the code. This initialization is assigned to take place when the DOM has been loaded by using the "domready" custom event:

  1. Y.on("domready", function (){
  2. Y.example.profiler.MathHelper.factorial(10);
  3.  
  4. var calls = Y.Profiler.getCallCount("Y.example.profiler.MathHelper.factorial");
  5. var max = Y.Profiler.getMax("Y.example.profiler.MathHelper.factorial");
  6. var min = Y.Profiler.getMin("Y.example.profiler.MathHelper.factorial");
  7. var avg = Y.Profiler.getAverage("Y.example.profiler.MathHelper.factorial");
  8.  
  9. Y.Profiler.unregisterFunction("Y.example.profiler.MathHelper.factorial");
  10.  
  11. var msg = "Method Y.example.profiler.MathHelper was run " + calls + "times.
  12. " +
  13. "The average time was " + avg + "ms.
  14. " +
  15. "The max time was " + max + " ms.
  16. " +
  17. "The min time was " + min + " ms.";
  18. alert(msg);
  19. });
Y.on("domready", function (){
    Y.example.profiler.MathHelper.factorial(10);
 
    var calls = Y.Profiler.getCallCount("Y.example.profiler.MathHelper.factorial");
    var max = Y.Profiler.getMax("Y.example.profiler.MathHelper.factorial");
    var min = Y.Profiler.getMin("Y.example.profiler.MathHelper.factorial");
    var avg = Y.Profiler.getAverage("Y.example.profiler.MathHelper.factorial");
 
    Y.Profiler.unregisterFunction("Y.example.profiler.MathHelper.factorial");
 
    var msg = "Method Y.example.profiler.MathHelper was run " + calls + "times.
" +
            "The average time was " + avg + "ms.
" +
            "The max time was " + max + " ms.
" +
            "The min time was " + min + " ms.";
    alert(msg);
});

The code block begins by calling factorial() once, which gets profiled. Then, the information about the function can be retrieved from the Profiler. This information is output in an alert, displaying the number of times that the function was called along with the minimum, maximum, and average running times. Since this is a very simple function, the run times will most likely be 0ms on most machines.

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings