3.17.08 Fast Display of Coldfusion Execution Time

There is a definite penalty to using the “Report Execution Times” in the coldfusion administrator. There is a simple way to use built-in java timestamps to guage execution time.

I simply add a start time at the beginning of a request via the Application.cfc’s onRequest method and calculate the difference in time at the end of the Application.cfc’s onRequestEnd method as follows:

<cfcomponent>
    ...
    <cffunction name="onRequest" returnType="void">
        <cfargument type="String" name="targetPage" required=True>
        <!--- start execution time --->
        <cfset jDate = createObject("java", "java.util.Date")>
        <cfset __execStart = jDate.getTime()>
        ...
    </cffunction>
    <cffunction name="onRequestEnd" output="Yes"><cfsetting enablecfoutputonly="Yes">
        <cfset pageContent = getPageContext().getOut().getString()>
        <cfset getPageContext().getOut().clearBuffer()>
        <!--- end execution time --->
        <cfset jDate = createObject("java", "java.util.Date")>
        <cfset __execEnd = jDate.getTime()>
        <cfset pageContent = pageContent & "<br>" & (__execEnd - __execStart) & "ms">
        ...
    </cffunction>
    ...
</cfcomponent>

Download this code: cf-exectime-application.cfc

This is using the built-in java Date object which includes the “unix” time-stamp number of milliseconds since the unix epoc which is perfect for calculating execution time. For each run of the application 2 java Date objects are instantiate within the request scope which is only a minute amount of overhead. This method could possibly be made faster if instantiating a Date object at the Application level and only setting the actual current time at the start and end of a request, but this has proved itself as fast enough in the production environment.


2 Comments

Javier Julio on 4.4.08 at 9pm

Great to see others doing this! Using Application.cfc it makes it super easy to test how long a request is taking programmatically. I don’t use any Java methods though, just getTickCount(), and do a line of math in onRequestEnd and dump the output to the console. Have you tried using getTickCount() and noticed any penalty?

Jim Palmer on 4.11.08 at 4pm

I’d honestly like to test this. It would make the most sense to use a java sleep() function to allow the baseline and then just compare the execution times between the getTickCount and the java.util.Date. I can only imaging that the getTickCount involves at least some bit more of overhead but I would think it’s completely negligible.




0.410s