Determine How Long Your ColdFusion Page Has Been Running Using GetPageContext()

Posted April 26, 2007 at 8:13 AM by Ben Nadel

Tags: ColdFusion

The other day, I discovered how to get the date/time at which your ColdFusion page started running. Before I had found this, I had contemplated using a GetTickCount() at the beginning of my page run to mark this time. But, this method is ganky and requires updates to more than one file. By reaching into the Page Context, I am able to grab the start date/time value of the page without having to modify any other templates:

  • <!---
  • Use the page and fusion context objects to get the
  • date/time stamp at which this page started processing.
  • Then, get the number of seconds that that start date is
  • smaller than the current date/time stamp value.
  • --->
  • <cfset intRunTimeInSeconds = DateDiff(
  • "s",
  • GetPageContext().GetFusionContext().GetStartTime(),
  • Now()
  • ) />
  •  
  •  
  • <!---
  • Output the number of seconds in which the page has
  • been processing.
  • --->
  • <p>
  • Page has been processing for:
  • #intRunTimeInSeconds# Seconds
  • </p>

Running the above, I get the output:

Page has been processing for: 0 Seconds

Zero seconds; not surprising considering the page has what amounts to like three lines of code on it. While this might seem completely insignificant, this is a very cool thing to know. I will go into that a bit more next.




Reader Comments

Apr 26, 2007 at 4:50 PM // reply »
319 Comments

You should consider filing a request for this to be added to the language (I mean a shortcuz function). I can see it being useful.


Apr 26, 2007 at 5:41 PM // reply »
11,238 Comments

Yeah, if we have GetTickCount(), maybe something like GetElapsedTickCount() or GetPageTickCount() would be very nice.


Dec 6, 2007 at 4:14 AM // reply »
3 Comments

Hi Ben,

Any idea why i get this error?

"The selected method getFusionContext was not found. Either there are no methods with the specified method name and argument types, or the method getFusionContext is overloaded with arguments types that ColdFusion can't decipher reliably. If this is a Java object and you verified that the method exists, you may need to use the javacast function to reduce ambiguity. "

I tried javacast and still same error occurs.


Dec 6, 2007 at 7:13 AM // reply »
11,238 Comments

@truthseeker,

That is odd because the function GetFusionContext() doesn't take any methods. What version of ColdFusion are you running?


Dec 6, 2007 at 8:50 PM // reply »
3 Comments

@Ben,

I'm using 6.1. Hmm... Could this be a version issue? Haven't tested it in later versions, though. 'Coz the development is still running in 6.1.


Dec 7, 2007 at 7:15 AM // reply »
11,238 Comments

@truthseeker,

To be honest, I wouldn't even know what to tell you. It is possible that the underlying mechanisms behind GetPageContext() changed going into MX7 (what we use in production).


Dec 7, 2007 at 8:42 AM // reply »
3 Comments

guess i'll just have to stick with GetTickCount() for now :)


Apr 13, 2010 at 3:30 PM // reply »
54 Comments

I noticed this does not work within onRequestEnd(). I always get 0 despite loading rather bulky pages. Appears not to work inside onRequest either, surely there's a way to use this within Application.cfc.


Apr 15, 2010 at 10:32 PM // reply »
11,238 Comments

@Drew,

That is odd. I know I have used this approach inside of Application.cfc.


Apr 15, 2010 at 10:33 PM // reply »
11,238 Comments

@Ben,

I'll have to double-check when I am in front of my work computer.


Aug 19, 2011 at 11:35 AM // reply »
2 Comments

Place the code at the bottom of your page. I had it on top and it didn't work. It kept give me 0 (zero) return. Once I placed it on the bottom it came up with the correct processing time.

Side note
Ben thanks for all your hard work. I pray the day I have your brain because my job would be 100% easier.


Aug 19, 2011 at 11:36 AM // reply »
2 Comments

@Fermin,

PS I'm running CF9


Sep 22, 2011 at 6:46 PM // reply »
6 Comments

I'm sure this has been considered but why not just write the now() to a session then a flush and another one at the end of the page and then do a date/time compare? Then you can even do an isdefined to see the original load time of the page even if someone reloads.

What I'm trying to do is write when someone leaves a page. I'm experimenting with some javascript that will execute a cf page when leaving to try to figure out how long a visitor stays without doing it through the server logs. Any ideas for that?



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools