YUI Library Home

YUI Library Examples: ProfilerViewer Control (beta): Simple Profiling

ProfilerViewer Control (beta): Simple Profiling

In this simple example, the ProfilerViewer Control is used to profile an instance of the YUI Calendar Control. Interact with the calendar interface, then activate the ProfilerViewer to see the profile data. You can then continue to interact with the calendar, refreshing the data in the ProfilerViewer to update the profiling display.

A Simple Use Case: Profiling Calendar

One of the nice things about the YUI Profiler is that it makes it simple to profile any JavaScript function or object structure. ProfilerViewer's job is to make it just as easy to explore that data.

In this example, we create an instance of YUI's Calendar Control, tell Profiler to profile our Calendar instance, and then use ProfilerViewer to experience that data.

This example has the following dependencies:

1<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/http://yui.yahooapis.com/2.6.0//build/calendar/assets/skins/sam/calendar.css">  
2<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/profilerviewer/assets/skins/sam/profilerviewer.css"
3 
4<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/utilities/utilities.js"></script> 
5<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/element/element-beta-min.js"></script> 
6<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/calendar/calendar.js"></script> 
7<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yuiloader/yuiloader-min.js"></script> 
8<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/profiler/profiler-min.js"></script> 
9<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/profilerviewer/profilerviewer-beta-min.js"></script> 
view plain | print | ?

We begin with empty div elements for Calendar and Profiler to use:

1<div id="profiler"
2<!--ProfilerViewer will be inserted here.--> 
3</div> 
4 
5<div id="cal1container"
6<!--Calendar instance will be inserted here.--> 
7</div> 
view plain | print | ?

In JavaScript, we instantiate our Calendar and then pass the instance to Profiler for profiling:

1//instantiate Calendar: 
2YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1","cal1container"); 
3 
4//profile the instance, labeling it "cal1": 
5YAHOO.tool.Profiler.registerObject("cal1", YAHOO.example.calendar.cal1 ); 
6 
7//render the Calendar; after this line, the Calendar is on the page, 
8//ready to be used; all the while, Profiler will be tracking the time 
9//spent in each function: 
10YAHOO.example.calendar.cal1.render(); 
view plain | print | ?

Finally, we set up ProfilerViewer to consume the profiling information and make it visible in the interface:

1//instantiate ProfilerViewer: 
2var pv = new YAHOO.widget.ProfilerViewer("profiler", { 
3    showChart: true//we want to see the pretty charts! 
4    base:"../../build/"//path to YUI assets; if you leave this 
5                         //blank, files will be drawn from  
6                         //yui.yahooapis.com. 
7    //path to Charts Control swf file; if left blank, we'll 
8    //use the one from yui.yahooapis.com; this path is relative 
9    //to your current HTML page: 
10    swfUrl: "../../build/charts/assets/charts.swf"
11     
12    //In this case we only want to see functions that have 
13    //been called at least once and that have the "cal1" 
14    //label associated with them in Profiler: 
15    filter: function(o) { 
16        //Only show functions that have been called at least 
17        //once and that are part of the cal1 object: 
18        return ((o.calls > 0) && (o.fn.indexOf("cal1") > -1)); 
19    } 
20}); 
view plain | print | ?

The full JavaScript block for this example is as follows:

1YAHOO.namespace("example.calendar"); 
2 
3YAHOO.example.calendar.init = function() { 
4    //instantiate Calendar: 
5    YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1","cal1container"); 
6     
7    //profile the instance, labeling it "cal1": 
8    YAHOO.tool.Profiler.registerObject("cal1", YAHOO.example.calendar.cal1 ); 
9     
10    //render the Calendar; after this line, the Calendar is on the page, 
11    //ready to be used; all the while, Profiler will be tracking the time 
12    //spent in each function: 
13    YAHOO.example.calendar.cal1.render(); 
14     
15    YAHOO.example.pv = new YAHOO.widget.ProfilerViewer("profiler", { 
16        showChart: true//we want to see the pretty charts! 
17        base:"../../build/"//path to YUI assets; if you leave this 
18                             //blank, files will be drawn from  
19                             //yui.yahooapis.com. 
20        //path to Charts Control swf file; if left blank, we'll 
21        //use the one from yui.yahooapis.com; this path is relative 
22        //to your current HTML page: 
23        swfUrl: "../../build/charts/assets/charts.swf"
24         
25        //In this case we only want to see functions that have 
26        //been called at least once and that have the "cal1" 
27        //label associated with them in Profiler: 
28        filter: function(o) { 
29            //Only show functions that have been called at least 
30            //once and that are part of the cal1 object: 
31            return ((o.calls > 0) && (o.fn.indexOf("cal1") > -1)); 
32        } 
33    }); 
34 
35
36 
37//Run the calendar.innit function only when the Dom is fully loaded: 
38YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init); 
view plain | print | ?

Copyright © 2008 Yahoo! Inc. All rights reserved.

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