Skip to main content
Inspiring
August 22, 2008
Question

DateDiff doesn't support milliseconds

  • August 22, 2008
  • 22 replies
  • 3113 views
I want to simply output the time the page renders (without making debug info available to public), but amazingly, it seems CF8 lost support in DateDiff for milliseconds (which was the letter 'l' for the datepart)

Anyone know of a way to get page execution time?
    This topic has been closed for replies.

    22 replies

    Inspiring
    August 25, 2008
    Well, I moved all my onRequest into onRequestStart, and everyrthing is working fine to that end (onRequest merely returns true now, no other code in it)

    But trying those scopes again, it keeps saying they don't exist. I dumped the REQUEST scope in onRequestEnd and sure enough, everything is there (including REQUEST.datStartTime)

    Except the code that is included is referencing the REQUEST scope variable that is created in onRequestEnd, and it says it doesn't exist.
    BKBK
    Community Expert
    Community Expert
    August 25, 2008
    I wasn't aware that you were using onRequest for something else. I would leave things as they are. As they say, if it ain't broke, don't try to fix it. The main thing is that you got everything working.



    Inspiring
    August 25, 2008
    Well, right now it's not working.

    My current setup is that in onRequestStart I have the code to set the start time (tried saving it in REQUEST scope and VARIABLES scope) In the onRequest scope, I have custom code I wrote that promotes SES (Search Engine Safe) page builds, allowing me to call pages like:

    http://www.domain.com/filename/variable/variable/etc

    And in onRequestEnd, I made the calculation and stored it in both REQUEST and VARIABLES scopes. In the onRequest, after the pathing is built and the page intended is determined, that template is included. My pages are built via a template.cfc component which I invoke through CreateObject().

    The page is built as such:

    REQUEST.objTemplate.writeHeader()
    (Included Template Data here)
    REQUEST.objTemplate.writeFooter()

    The writeFooter function of my objTemplate component has code which displays the execution time, and as of all tests so far, it says the variable (intExecutionTime) does not exist in REQUEST scope (or VARIABLES)

    Should I move all my code from onRequest into the onRequestStart and leave onRequest empty?
    BKBK
    Community Expert
    Community Expert
    August 24, 2008
    Leave onRequest out of it. In fact, comment it out, unless you have use for it. It works like a cfsavecontent for the current page, enabling you to manipulate its contents. It therefore has no place here.

    Just set the start-time in onRequestStart and log or display the execution time in onRequestEnd. I would also stick to the variables scope. I don't expect the request scope defined in onRequestStart to carry over into onRequestEnd.

    Inspiring
    August 23, 2008
    Well, I'll give it a shot. Gonna set start time in onRequestStart and calculate end time in onRequestEnd and then try to output that value in the page called in the onRequest. I'll scope the calculated value and store it in the REQUEST scope and see if it flies.

    Gotta wait til Monday to do it though - my localhost at home with CF8 is broken. Will try it at work.
    BKBK
    Community Expert
    Community Expert
    August 23, 2008
    But if I determine the page render time in the onRequestEnd, I cannot output that information to the screen such as a "Page rendered in xxx" string

    You can. Try it?

    Inspiring
    August 23, 2008
    But if I determine the page render time in the onRequestEnd, I cannot output that information to the screen such as a "Page rendered in xxx" string.
    BKBK
    Community Expert
    Community Expert
    August 23, 2008
    I used GetTickCount, but it never matches the execution time when
    I have debug out (it may just be because the extra time to process the
    debug output makes this different)


    It is the extra time. You should not confuse the total execution time with the page execution time. Turn debugging on, open a CFM page and look at the section Execution Time. You will see that there are separate rows in the table for the execution time of the page and for the total execution time.

    I think the method suggested by Dan and Adam is as accurate as you can get. Something that goes like this

    <cffunction name="onRequestStart">
    <cfargument name = "targetPage" type="String" required=true/>
    <cfif listlast(arguments.targetPage, "/") is "test.cfm">
    <cfset requestBegins=gettickcount()>
    </cfif>
    </cffunction>

    <cffunction name="onRequestEnd">
    <cfargument type="String" name = "targetTemplate" required=true/>
    <cfif listlast(arguments.targetTemplate, "/") is "test.cfm">
    <cfset requestEnds = gettickcount()>
    <!--- log or display the time in millisecs, namely, (requestEnds-requestBegins) --->
    </cfif>
    </cffunction>

    Inspiring
    August 23, 2008
    > I used GetTickCount, but it never matches the execution time when I have debug
    > out (it may just be because the extra time to process the debug output makes
    > this different)

    Correct. GetTickCount() will be closer to the mark than what debugging
    reports.


    > There are limitations of where I can use GetTickCount(), because I have to set
    > it at the start of the request (easy enough, stored in a REQUEST-scope
    > variable) but then I have to calculate the difference before the page ends (so
    > the time isn't right) so the variable for the difference exists when I want to
    > output it (in the footer of the page)

    Well you'd be needing to do the same thing with dateDiff(), so if there's
    an issue, it's not to do with getTickCount().

    Can't you just bung the final calculation - and logging thereof - into
    OnRequestEnd.cfm?

    --
    Adam
    Inspiring
    August 22, 2008
    There are a couple of suitable methods available in application.cfc files.