Faster Debugging with CFTIMER and CFTRACE
Abstract Two of the most common debugging tasks are displaying variables and identifying bottlenecks. For years I relied on CFDUMP and CFABORT to display variables and getTickCount() to calculate processing time and identify slow-running code.
Indeed these wonderful tags and functions have long been the quintessential toolkit of any ColdFusion developer.
In the approach above I used <CFDUMP> and <CFABORT> tags to display the value of a variable. Want to display a few variables? The screen quickly becomes a rainbow of purple, green, and gray as you look for your variables. While the wonders of CFDUMP probably deserve their own article, Adobe recently introduced a powerful new tag called CFTRACE.
Introducing CFTrace
With CFTRACE, you can easily display changing variable values either inline or in the debugging section, categorize variables, and even abort execution, all in one tag. So the good ole CFDUMP becomes:
*Note that with CFTRACE the var attribute accepts a variable name without pound signs.
The Advantages of CFTrace
"So what" you say, "I've been using CFDUMP and CFABORT for years, why would I switch?" Well, there are many reasons. Have you ever had to display a variable more than once? The task of identifying those values on the screen can become very cumbersome, especially if manipulated and changed multiple times.
Showing variable values at different points in a template is easy with CFTRACE. Simply add CFTRACE tags after every change and you'll see an organized list in the debugging info section at the bottom of the screen.
Another clear advantage of CFTRACE is that it can easily be turned on and off via the ColdFusion administrator. I find it extremely useful not to constantly remove and add code during the development process just to display variables.
Simply browse the ColdFusion Administrator and go to Debugging & Logging -> Debugging Settings to turn CFTRACE on and off.
Not convinced yet? Consider the fact that CFTRACE also lets you categorize and type information as well as display the time differences between traces. CFTRACE calls are also displayed in Dreamweaver's server debug tab results window and ColdFusion logs
All of the above should be enough to convince any developer to give CFTRACE a test drive. It's a hidden gem that has long been overlooked.
Measuring Processing Time with CFTIMER
Occasionally, I'll have to debug a very slow running template. In the past I had to rely on getTickCount() to find out how long it took a section of code to run.
<cfloop from="1" to="100" index="i">
<cfoutput>#i#</cfoutput>
</cfloop>
<cfset end = GetTickCount()>
<cfoutput>Loop took #end-start# ms</cfoutput>
But recently I've been using a new tag called CFTIMER. Simply wrap a section of your code with CFTIMER and, voilà, you instantly know how long it took to run that section.
<cfloop from="1" to="1000" index="i">
<cfoutput>#i#</cfoutput>
</cfloop>
</CFTIMER>
CFTIMER lets you put the information inline (as seen in the example above) in an HTML comment, in the debugging info, or even in a nice outline as seen in the example below.
<cfloop from="1" to="100" index="i">
<cfoutput>#i#</cfoutput>
</cfloop>
</CFTIMER>
Using "outline" can become very useful when you have several nested sections you want to test.
Summary CFTRACE and CFTIMER introduce new powerful debugging capabilities. These tags offer significant advantages over the previous methods and make it both easier and faster to debug code.
Source : © 2006 SYS-CON Media Inc.

There are no comments for this entry.
[Add Comment]