Using Custom Debugging Template to Track What Templates Execute

So here's my solution to the problem I described last time. In summary, you're the new CF guy at a company with a large application. All in all, the application is great, but like all "mature" applications, it could probably use some pruning. But how do you know what can be safely cut out, and what is a vital piece of the application. In other words: So What Templates are executing anyway?

My solution... custom debugging templates. Thanks to Ray Camden and Adam Podolnick and their awesome work on ColdFire, custom debugging templates were on my mind lately. So how does the ColdFusion debugger help solve my problem?

Well as we all know, the underlying debugging service keeps track of all templates executed during a request. So I wrote a simple debugging page that looks at this and stores the templates executed in the server scope of the machine. My custom page has no output so the users see nothing. There is obviously some overhead running the debugger on a production server. So you'll have to decide for yourself if you can afford adding the extra time to the request.

I've been running my templateTracker.cfm custom debugging template for about a week now. Out of 1486 *.cfm and *.cfc's on the file system, users have executed 837 of them. When that number stops going up, I know I'll have my list of orphaned files. Let the pruning begin!!!

Oh yeah, here is my code for templateTracker.cfm custom debugging template. Nothing fancy, but gets the job done.

<cfsilent>

<cfset qEvents = "" />

<!--- Lets get the debugging data --->
<cftry>
   <cfobject action="CREATE" type="JAVA" class="coldfusion.server.ServiceFactory" name="factory">
   <cfset cfdebugger = factory.getDebuggingService()>
   <cfset qEvents = cfdebugger.getDebugger().getData()>
   <cfcatch type="Any"></cfcatch>
</cftry>

<!--- If we got the data, lets store it in an array in the server scope --->
<cfif isQuery(qEvents)>
   
   <!--- lets create the array if it doesn't exist yet --->
   <cfif not structKeyExists(server,"templateTracker")>
   <cfset server.templateTracker = arrayNew(1) />
   </cfif>
   
   
   <!--- QofQ to get ride of duplicates --->
   <cfquery name="qEvents" dbtype="query">
   SELECT DISTINCT template
   FROM qEvents
   </cfquery>
   
   
   <cfloop query="qEvents">
      
      <cfif findNoCase("CFC[",template)>
      <cfset tamplate = trim(listLast(listFirst(template,"|"),"[")) /><!--- I'm a cfc, and this is my path --->
      <cfelse>
      <cfset tamplate = template /><!--- I'm a cfm, and this is my path --->
      </cfif>
      
      
      <cfif server.templateTracker.indexof(tamplate) EQ -1><!--- Use Java's indexof to see if this template already exists in our array --->
         
         <cflock name="templateTracker" type="readonly" timeout="5">
         
            <cfif server.templateTracker.indexof(tamplate) EQ -1>
               <cfset arrayAppend(server.templateTracker,tamplate)><!--- I'm a new template. Add me to the array --->
            </cfif>
         
         </cflock>
         
      </cfif>
      
   </cfloop>

</cfif>

</cfsilent>

Related Blog Entries

Comments
Copyright © 2006, Tim Elsner. This is a personal weblog. The opinions expressed here represent my own and not those of my employer.
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.