Wednesday, November 18, 2009

CF: Controlling Time Based Runtime Exceptions

Ultimately time controls everything. This is true in larger scheme of things but as well in writing and running ColdFusion code. Fortunately, many CF tags have built in time controls in forms of exceptions that can be thrown when a pre-specified time has been exceeded. Most tags implement this through a timeout attribute.

You, then, use a CFTRY/CFCATCH combination to handle timeout occurrences, e.g. a web page failed to respond, or FTP didn't connect, etc.

However, this system breaks down if your intent is control multiple tags in combination, e.g. a combination of CFHTTP and CFFTP may take up to 30 seconds though individually CFHTTP or CFFTP may consume a variable amount of time.

Similarly, you are running a multitude of small steps in a loop, e.g. loading 1000 records, or displaying 500 lines. Though each line will only take a fraction of a second to process the number of lines in aggregate will blow you out of the water.

Normally in these cases we would wait on an overall page time out if we had one, then produce a generic catch all message saying your page has timed out and good luck trying this again.

This may suffice generically speaking, but may prove to be frustrating for users and visitors of your apps and sites.

What if you could instead be more pro-active. What if you could predict how long your processing is going to take, even on shared servers with varying loads?
What if you could give users options to either proceed, adjust your number of iterations, or simply stop early because you will never finish the task at hand in the time given.

Ideally your code would be very compact:


<!--- check prediction --->
<cfif objTC.WillTimeout()>
<!--- we will timeout lets do something about it --->
...



Conceptually speaking this could have been done for a long time in CF, but practically I have seen few implementations.

Let's look at what it would take to make this prediction using a scenario in which we have a repetitive core process that we need to time. Thus, it would be all about the ability to measure time for each loop. Thus we would
a) need to start a timer, e.g. GetTickCount()
b) end the timer, e.g. another GetTickCount()
c) measure the difference between start and end time
d) use the remaining iterations to come up with how much more time we need

Simple, right? Indeed it is. But the downfall is that it doesn't work.
The system breaks down because of the high variability in practice between each iteration of the loop. Using the built in CF function GetTickCount() we could establish a start and end time. But, when we measure the execution time between the two tick counts the results in this realm change quite a bit.

So, will we have to implement something complex using Java JETM instead to get this working? You probably could, but it isn't necessary if we add a dose of statistics to the whole puzzle. Here is a modified approach:
a) establish statistical validity threshold in number of iterations (e.g. how many time do I need to have gone through the loop to have a valid sample)
b) still need to start a timer, e.g. GetTickCount()
c) end the timer, e.g. another GetTickCount()
d) measure the difference between start and end time
e) update your statistics
f) if validity threshold has been reached make prediction


This does seem to work a lot better but requires many more lines of code. Rather than put all of these lines of code into each template, I have abstracted the needed calls into a component.

I would initialize the component (objTC) with the number of iterations (number of times I would need to go through the loop) and how many times I will need to have completed the loop to get a valid time prediction. In my example this would be 1000000 iterations with 150 completions. Or, something like this:


<cfset objTC = CreateObject("component","TimeControl").init(1000000,150)>



Then within the loop I would start and end the main timer like so:


<!--- start measuring --->
<cfset objTC.start()>


End Measuring:


<!--- end measuring --->
<cfset objTC.end()>


Now that the component takes care of all the nasty statistics stuff I can keep my main code simple and have some functions to use to either provide feedback to user, or just check on the status of things.
E.g. I could provide feedback like so:

<cfif objTC.HasValidAverage()>
<!--- send the expected time to user
when this process will complete --->

<cfoutput>
Expected completion at #objTC.getFinishTime()#
or (#objTC.getFinishSeconds()#seconds)
<br/>
</cfoutput>

<cfflush>


This is just a sample on how to handle it. I have put several more functions in the examlpe to play with. It may work well for some of your scenarios or not all. In any case, I do believe an improved feedback to user and more predictive code behavior based on execution time will reflect well on you as programmer. So feel free to play with samples and see whether this can get you started.

The full sample code can be downloaded here.


Cheers,
-B

Saturday, November 14, 2009

CF: Meetup presentation about debugging materials (Nov 12, 2009)

Thanks again to everyone attending the presentation. The goal was to get an idea about debug space and think about attacking debug problems by first identifying what area they originate from. Hope this gave everyone to use.

Also we discussed with special focus on time based debugging from the performance perspective as well as from the perspective of handling time based exceptions predictevly.

Dowload presentation and code now.

Several questions regarding the use of CFDUMP came up and here are couple of links to deeper treatment of advanced CDFDUMP tag in CF9. Additinal attributes can be specified to tweak its behavior:

CFDUMP in CF9

Cheers
-Bilal

Wednesday, October 21, 2009

CF: CFinNC presentations

Hallo everyone. First thanks for attending my sessions. If you did not attend and wanted to take a peak at what we covered please feel free to review the slides. Of course more fun was had during the sessions ;o)

Download Beginning Debugging Session

Download Advanced Debugging Session

I have inlcluded more slides and examples than we covered hoping that they can be usefull as well. All my material are distributed under creative commons license. Feel free to contact me with questions or comments.

Generally, this conference showed that the spirit of CF is alive and well. It was an all volunteer event from the organization to the speakers and all the things in between. It introduced old and new people to CF and furthered networking in this Region. I hope we can make this an annual event.

Cheers,
-B.

Tuesday, October 13, 2009

CF: CFinNC is all about debugging for me

My presentations for CFinNC will be all about debugging. There is a lot to talk about crunched into two 50 minutes segments.
Debugging is the often hated but yet nonetheless very important part of the developer's life fraught with lots of frustration and brief moments of glorious joy when the culprits are found. I am trying to give both the beginner and advanced audiences multiple techniques and tools to make the shining moments more frequent and frustrating times less so.

Here is my official description about the overall presentations:

Debugging can be a very frustrating exercise. Many times we find ourselves spending three times as long as it took us to develop the code to debug it. Especially code written by other developers seems time consuming to debug ;o)
Understanding the debugging infrastructure will shortcut much of the confusion and help to quickly focus on the source of the problem. What can you do when has reached is limits? We will talk about techniques, tools, and tips available to the ColdFusion Developer to make this a "less" frustrating exercise.

Hope to see you there,
B.

Thursday, September 24, 2009

General: Ordering from Adobe and how not to sell.

So during the CFUnited conference Adobe and organizers offered a special deal on ColdFusion to the attendees. I thought that this was a highly cool deal. Adobe supporting the very people that promote them every day.
I quickly chatted up the Adobe people at the conference on how to accomplish this as I did have a project requiring deployment of 23 CF servers. They all were consistent, which is good; I need to talk to the Eastern Sales Rep (Dominick Conte) whom I could not find in person or in attendance anywhere, which is bad, but whose business cards where everywhere ;o)
So, upon my return, I happily attempted to call Dominick. Different times, different dates; only voice mail answering. I thought the guy must be selling this stuff like hot-cakes as he never is available via phone; so I gave up and send an email to ask how one orders these hot deals offered by Adobe on the show.
His initial response was that I was not really at the show. Interesting, approach; made me doubt my very existence for about a little less than 0.1 ms... ahhh... deep thoughts. Of course, I advised him how he can find out who attended and then was waiting for some sort of contact.
Alas, again, no-go.
I am now paraphrasing the emails that went back and forth thereafter:

Me: "hey wassup", "why you not talk to me?", and "how can I order your good stuff?" and "why is it so difficult to talk to you guys?".
Dom: "Yeah, I tried to talk to you" (I checked and no such phone logs), and " what the h..k do you want anyways?"

This is not going well I think, as I am very consitently asking this man who is in sales on how to place an order (partners, direct or what?). So, he finaly caves and shoots back an email that he is not really the dude to talk to and the only way I can get this offer is to talk to a company in Canada.

WTH?


Cheers,
B.


Addendum:
We finally did end up buying some licenses for different projects from Adobe through the reseller in Canada. The credit for this goes entirely to Adam Lehman who could get me a contact name for ordering.

Cheers,
B.

Wednesday, September 2, 2009

CF: CfinNC North Carolina ColdFusion / Flex / Air Conference - Oct 17-18, 2009

I will be presenting at the CFinNC conference this October. I will share some insight into working with SOLR, the search engine based on Apache Lucene as well as talk about debugging tips and tricks. If you have some insight you would like to share feel free to leave a comment.

CFinNC - Carolina ColdFusion / Flex / Air Conference - Oct 17-18, 2009

Here are the details on the conference:

Registration for CFinNC is now open! CFinNC is a free web development conference held in Raleigh, North Carolina during the weekend of October 17th and 18th with an International line-up of speakers presenting on timely and relevant topics on web development. Thanks to the generosity of our sponsors and the creativity of the planning committee, registration for the event will be free and includes entry to the weekend event and to all presentations.

In order to keep the event free we have removed some hard costs and some benefits you normally get from paid-registration events.

Please be aware of the following:

1) You are responsible for paying for lunch for both days. We have enlisted the services of a local caterer and will be providing lunch each day for $10/day. This includes a sandwich, chips and drink. Please bring cash the day of the event! We will not be able to process credit cards!

2) Limited Edition, Collectible CFinNC conference t-shirts will be available for purchase for $15 with any proceeds going to offset costs and possibly sponsor a planning committee dinner (if we sell them all).

Lunch and shirt purchase is completely optional. You may indicate if you would like to purchase lunch and/or a t-shirt on the registration form.

For more information, please check the CFinNC website at: http://www.cfinnc.com


-Cheers,
Bilal

Wednesday, August 19, 2009

CF: Adobe publishes hot fixes for ColdFusion

On August 17, 2009 Adobe published a number of security updates to numerous versions of ColdFusion. This has led to some scrambling to test these issues, get them patched etc.
So far the hotfixes did not appear to break anything but the packaging is lacking as many manual steps will have to be completed.
I could not quite understand why these hotfixes could not have been bundled together as one fix that can be applied.
To make our lives easier I have built an installer for ColdFusion 8.0.1 running on Windows systems to do just that. You are free to use it at your own risk.

The installer will update standalone installation of CF not J2EE/JEE installations.

Thus the following hot fixes will be applied:
CVE-2009-1872, CVE-2009-1875, CVE-2009-1876, CVE-2009-1877, CVE-2009-1878

These JRUN only updates will not:
CVE-2009-1873, CVE-2009-1874

Cheers,