Using Profiler/ProfilerViewer to Inspect Non-YUI Code

The ProfilerViewer Control (and the Profiler that drives it) is code-agnostic: It can be used as easily to profile your own code or third-party code as it can to profile YUI modules. In this example, we use Profiler and ProfilerViewer to profile the DP Syntax Highlighter script that the YUI Project uses for code highlighting on our website.

To lighten the initial YUI footprint on the page (and minimize the impact of YUI on the script being profiled), this example takes the following steps:

  1. YUI Loader and Profiler are loaded first, along with the script being profiled. By putting only a tiny amount of YUI code on the page, we should be able to run DP Syntax Highlighter without its performance being impacted much by YUI.
  2. Once DP Syntax Highlighter has run, highlighting the codeblocks in the tutorial below, click on the link below reading "Show Code Profile." This will trigger YUI Loader to load ProfilerViewer and its dependencies, rendering the display and allowing you to explore the performance profile of the Syntax Highlighter.

Show Code Profile


Here's how we accomplish the steps outlined above. (Note: It's the script-based code-highlighting used below that's being profiled in this example.)

First, we load the minimal dependencies for profiling code with YUI Profiler. To that, we add the DP Syntax Highlighter code (ie, the code that we'll be profiling).

1<script type="text/javascript" src="http://yui.yahooapis.com//build/yuiloader/yuiloader-min.js"></script> 
2<script type="text/javascript" src="http://yui.yahooapis.com//build/profiler/profiler-min.js"></script> 
3<script type="text/javascript" src="http://developer.yahoo.com/yui/examples/profilerviewer/assets/dpSyntaxHighlighter.js"></script> 
view plain | print | ?

Next, we set up a simple initialization function that (a) sets up the code we want to profile then (b) executes the code:

1YAHOO.example.pv.init = function() { 
2 
3    //profile the Syntax Highlighter; note: we want to register the 
4    //object for profiling before we execute the highlighting code: 
5    YAHOO.tool.Profiler.registerConstructor("dp.sh.Highlighter"); 
6    YAHOO.tool.Profiler.registerObject("dpSyntaxHighlighter", dp, true); 
7 
8    //Highlight code: 
9    dp.SyntaxHighlighter.HighlightAll('code'); 
10 
11}
12 
13YAHOO.example.pv.init(); 
view plain | print | ?

At this point, we've told Profiler what parts of Syntax Highlighter we want profiled and we've then run Syntax Highlighter, capturing data about its performance. And we've done all this with the smallest possible amount of YUI code on the page. Note that Profiler is loaded now, but ProfilerViewer and all of the YUI infrastructure it leverages — like Element, DataTable and Charts — is still absent.

Now it's time to set up a button that brings in the YUI ProfilerViewer and all of its dependencies. That button executes the following code:

1//When the showProfile button is clicked, use YUI Loader to get all required 
2//dependencies and then show the profile: 
3YAHOO.example.pv.showProfilerViewer = function() { 
4 
5    //disable the button once it's clicked: 
6    document.getElementById("showProfile").onclick = ""
7    document.getElementById("showProfile").className += " disabled"
8     
9    //private function renders the viewer once dependencies are loaded: 
10    var showViewer = function() { 
11 
12        //instantiate ProfilerViewer with desired options: 
13        var pv = new YAHOO.widget.ProfilerViewer("", { 
14            visible: true//expand the viewer mmediately after instantiation 
15            showChart: true
16            base:"../../build/"
17            swfUrl: "../../build/charts/assets/charts.swf" 
18        }); 
19    }; 
20 
21    //private function gets dependencies for ProfilerViewer: 
22    var getProfilerViewer = function() { 
23        var loader = new YAHOO.util.YUILoader({ 
24            base: "../../build/"
25            require: ["profilerviewer"], //YUI Loader will handle all dependencies 
26                                      //for us. Nice! 
27            onSuccess: showViewer 
28        }); 
29        loader.insert(); 
30    }; 
31     
32    //fire getProfilerViewer to trigger the loading and display of the viewer 
33    //console: 
34    getProfilerViewer(); 
35 
36}; 
view plain | print | ?

You can use similar strategies to leverage YUI's Profiler and ProfilerViewer on your projects — even those that aren't based on YUI.

The full script souce for this example follows:

1YAHOO.namespace("example.pv"); 
2 
3YAHOO.example.pv.init = function() { 
4 
5    //profile the Syntax Highlighter; note: we want to register the 
6    //object for profiling before we execute the highlighting code: 
7    YAHOO.tool.Profiler.registerConstructor("dp.sh.Highlighter"); 
8    YAHOO.tool.Profiler.registerObject("dpSyntaxHighlighter", dp, true); 
9 
10    //Highlight code: 
11    dp.SyntaxHighlighter.HighlightAll('code'); 
12 
13}; 
14 
15//When the showProfile button is clicked, use YUI Loader to get all required 
16//dependencies and then show the profile: 
17YAHOO.example.pv.showProfilerViewer = function() { 
18 
19    //disable button: 
20    document.getElementById("showProfile").onclick = ""
21    document.getElementById("showProfile").className += " disabled"
22     
23    //private function renders the viewer once dependencies are loaded: 
24    var showViewer = function() { 
25 
26        //instantiate ProfilerViewer with desired options: 
27        var pv = new YAHOO.widget.ProfilerViewer("", { 
28            visible: true//expand the viewer mmediately after instantiation 
29            showChart: true
30            base:"../../build/"
31            swfUrl: "../../build/charts/assets/charts.swf" 
32        }); 
33    }; 
34 
35    //private function gets dependencies for ProfilerViewer: 
36    var getProfilerViewer = function() { 
37        var loader = new YAHOO.util.YUILoader({ 
38            base: "../../build/"
39            require: ["profilerviewer"], //YUI Loader will handle all dependencies 
40                                      //for us. Nice! 
41            onSuccess: showViewer 
42        }); 
43        loader.insert(); 
44    }; 
45     
46    //fire getProfilerViewer to trigger the loading and display of the viewer 
47    //console: 
48    getProfilerViewer(); 
49 
50}; 
51 
52YAHOO.example.pv.init(); 
view plain | print | ?