Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: Aurélien Deleusière

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

By Ben Nadel on
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.



Looking For A New Job?

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader 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.

Reply to this Comment

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

Reply to this Comment

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.

Reply to this Comment

@truthseeker,

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

Reply to this Comment

@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.

Reply to this Comment

@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).

Reply to this Comment

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.

Reply to this Comment

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.

Reply to this Comment

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?

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.