YUI Library Home

Site Search (YDN & YUIBlog):

# 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.

View example in new window.

## 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:

<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"> <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/profilerviewer/assets/skins/sam/profilerviewer.css"> <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/utilities/utilities.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/element/element-beta-min.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/calendar/calendar.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yuiloader/yuiloader-min.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/profiler/profiler-min.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/profilerviewer/profilerviewer-beta-min.js"></script>

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

<div id="profiler"> <!--ProfilerViewer will be inserted here.--> </div> <div id="cal1container"> <!--Calendar instance will be inserted here.--> </div>

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

//instantiate Calendar: YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1","cal1container"); //profile the instance, labeling it "cal1": YAHOO.tool.Profiler.registerObject("cal1", YAHOO.example.calendar.cal1 ); //render the Calendar; after this line, the Calendar is on the page, //ready to be used; all the while, Profiler will be tracking the time //spent in each function: YAHOO.example.calendar.cal1.render();

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

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

The full JavaScript block for this example is as follows:

YAHOO.namespace("example.calendar"); YAHOO.example.calendar.init = function() { //instantiate Calendar: YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1","cal1container"); //profile the instance, labeling it "cal1": YAHOO.tool.Profiler.registerObject("cal1", YAHOO.example.calendar.cal1 ); //render the Calendar; after this line, the Calendar is on the page, //ready to be used; all the while, Profiler will be tracking the time //spent in each function: YAHOO.example.calendar.cal1.render(); YAHOO.example.pv = new YAHOO.widget.ProfilerViewer("profiler", { showChart: true, //we want to see the pretty charts! base:"../../build/", //path to YUI assets; if you leave this //blank, files will be drawn from //yui.yahooapis.com. //path to Charts Control swf file; if left blank, we'll //use the one from yui.yahooapis.com; this path is relative //to your current HTML page: swfUrl: "../../build/charts/assets/charts.swf", //In this case we only want to see functions that have //been called at least once and that have the "cal1" //label associated with them in Profiler: filter: function(o) { //Only show functions that have been called at least //once and that are part of the cal1 object: return ((o.calls > 0) && (o.fn.indexOf("cal1") > -1)); } }); } //Run the calendar.innit function only when the Dom is fully loaded: YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init);

### ProfilerViewer Control (beta) Examples:

- Simple Profiling
- Using the ProfilerViewer API while Profiling the YUI Menu Control
- Using Profiler and ProfilerViewer to Profile non-YUI Code
- Skinning the ProfilerViewer Control
- ProfilerViewer Internationalization: Spanish
- ProfilerViewer Internationalization: German

### More ProfilerViewer Control (beta) Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

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