ColdFusion Performance Tuning Keen Haynes Certified ColdFusion Developer
Overview Application Environments Testing Techniques Tuning Techniques Coding Tips Variable Locking Databases and Queries Thread Processing Debugging
Application Environment Goals Provide separate, autonomous areas for Development, Testing/Staging and Production Provide the Developer Community with an environment that maximizes Productivity Effectiveness
Application Environment, cont. Provide a Testing/Staging environment that facilitates Automated application staging Testing Load/Scalability testing Production readiness (data migration, etc) Provide a Production environment that is Scalable Manageable Makes efficient use of resources Designed for High-Availability and end-user responsiveness
Testing Techniques Perform “Critical Path” or “Strategic Strike” Analysis Stress User vs. Real User Build a “Real-World” load generator test script Load Test & Perform Bottleneck Analysis Do Manual Timings in a browser (no load vs under load) Perform Endurance Testing Examine log files during testing
Testing Techniques, cont. Principle elements involved in template response time: Total Execution Time (i.e. 203ms) Query Time (i.e. 156ms) Processing/Response Time: Total - Query (i.e. 46ms)
Tuning Techniques Establish your max user base line Manually walk through test and capture explode bench marking – identify bottlenecks Test with new general CF admin settings Test with changes to simultaneous request settings changes – normally the biggest bang for your buck.
Managing Performance, cont. Query Time: Can be significantly reduced by proper DB Design, Configuration, Tuning and Administration Use a professional Database Administrator (DBA) Ensure/verify updated/proper version of ODBC (i.e. MDAC v 2.6) and/or DB client libraries on CF Server In CF Server, try “Maintain DB Connections” (Very) generally, queries taking longer than > 150ms execution time w/no load MUST be examined closely
Coding Tips Use CF built-in functions wherever possible Avoid complex logic inside CFQUERY (build outside) Fully scope variables Lock all Session, Application, and Server variable access MDAC update ComCheck Overuse of #
Coding Tips Carefully load test and analyze CFX calls response times C++, Java Be careful using CFHTTP, CFFTP Scalability of CFFILE CFMAIL in mass mailings Avoid redundant queries and looping queries Use Stored Procedures where appropriate Avoid redundant code Use of CFINCLUDE versus Custom Tags Blank space
Coding Tips Caching strategies Queries Administrator CACHEDWITHIN Selection of content Templates Administrator CFCACHE SUPERCACHE
Variable Locking Server, Application, and Session vars must be locked using ! Or random memory corruption occurs. Consider Client and Request scopes Symptoms: PCode errors, Unknown Exception errors Use “Full Checking” in Dev and Staging environments
Variable Locking Levels: Exclusive, ReadOnly Exclusive: does not allow any operations on a given variable scope until the current process within the lock is complete. ReadOnly: allows multiple reads within the scope but no write access. If an Exclusive lock is processed at the same time as a ReadOnly lock, the ReadOnly lock will wait for the Exclusive lock to finish before proceeding.
Variable Locking Locking methods As a general rule of practice locking should be done in the code using CFLOCK. ReadOnly locking can be applied globally to SERVER, APPLICATION, and SESSION variables using the "Automatic Read Locking" option on the Locking page of the ColdFusion Administrator. There is no method to write-lock variables globally in the CF Administrator when using multi-threaded sessions. One could single-thread sessions using the "Single Threaded Sessions" option, thereby eliminating the need for locking SESSION variables, but this results in severe performance degradation and is not recommended for scalable applications.
Variable Locking CFLOCK Syntax Typical usage [process]
Variable Locking NAME Locks May be used in place of locking by scope. The NAME argument is used in place of the SCOPE argument. Nesting Locks As a general rule of thumb, nested locks should be avoided as they can result in deadlocks. Please refer to the CFML Language Reference Documentation if nested locks are considered absolutely necessary. Avoid use of locks in queries
Database Issues Use latest stable release Look for PCode errors including “SQL” or “Query” Queries that never return; apparent CF Server hang Troubleshoot by setting max DB connections to 1
SQL Ensure appropriate indexes are available for all common queries Clustered Indexes in SQL Server Avoid calling queries in loops Use joins instead of looping around queries if possible
Query Caching Appropriate for queries with few permutations Limits to number and size of queries No easy way to “flush” cached queries Caching for only one minute at 10 queries per second reduces load on the database (for that query) by 600x
Query Caching Example: Cache statecodes query for 10 minutes: SELECT * FROM States ORDER BY StateCode
A.cfm page is requested by a web browser The.cfm request passed to the CF Web server stub The stub sends the request to the ColdFusion Application Server for processing. Thread Processing
Thread Processing (cont) A listener thread receives the request, sends it to one of the waiting active simultaneous threads to process the request. If all of simultaneous threads are busy, the listener thread places the request on a waiting list. When one of the simultaneous request slots becomes available, listener thread sends the waiting request to the active slot.
Thread Processing (cont) The ColdFusion server Processes all of the CFML tags in the page Converts the results to HTML and sends them back to the web server to be sent to the browser.
Thread Processing (cont) The thread is put back into the active pool to be reused. Much less resource intensive than the thread create/destroy scenario.
3rd Party Calls If 3rd party call never returns a result set or error message, thread will continue to wait and will NOT timeout If many.cfm page requests with blocking third party requests Simultaneous Request limit will be reached ColdFusion server will appear to hang BUT: ColdFusion is not failing to process, it simply is waiting for a response from a third party resource call.
ColdFusion Debugging Performance or stability problems more often than not are caused by processing of a third party request Pinpointing these third party problems can be difficult The following steps should be taken to help determine the source of the issue
ColdFusion Debugging STEP 1 Examine logs
ColdFusion Debugging STEP 2 Verify the source of the unresponsiveness of the ColdFusion server Try opening up ColdFusion Administrator or other simple.CFM page If this fails, try to open a.html or.html page If web server returns the.html page, the web server is functioning properly, go to Step 2 If.html page does not return, web server is causing the problem, stop and restart the web server If after stopping and starting the web server ColdFusion pages still do not return, go to step 2.
ColdFusion Debugging STEP 3 Open up the NT Performance Monitor or run cfstat and add the CF "Queued Requests", and "Running Requests" counters, along with %CPU usage for the CFSERVER instance. If third party request is suspected, at time of the hang: %CPU usage will be approx. zero, Running Requests will be approx. at Simultaneous Request limit and Queued Requests should be rising
ColdFusion Debugging STEP 4 - At the time of the unresponsive period, try running a query through MSQuery using SQLPlus or ISQL. These do not connect to the database through ODBC and will verify the ODBC connection to the database Run the same SQL statement that you believe is causing the problem SQL Profiler Deadlocking
ColdFusion Debugging STEP 5 - Turn on "logging of slow pages" in the debugger section. At the very least it will list the templates that are active for n seconds. This will give your users a place to start looking for long running queries
ColdFusion Debugging STEP 6 - Non-functioning, or slow functioning CFX tags, or CFML code called in your ColdFusion templates can also be the cause of ColdFusion performance/stability issues. Determining that a CFX tag or piece of CFML code is causing the problem is not an easy task Use the CF function GetTickCount() around CFX tag call (or any suspected piece of slow performing code)
ColdFusion Debugging Example: (code to be timed here...) Time to Complete: #totalTime#
ColdFusion Debugging Versions: Be certain all patch revisions are current OS Kernal C++ DB WWW CFAS
ColdFusion Debugging Poorly written SQL statements Full table scans (not, like) No database indexing No use of stored procedures Deadlocking
ColdFusion Debugging CFMAIL as mass-mailer CFX tags that lose thread control CFHTTP - incorrect URLs No CFLOCK around client, application variables Poorly written logic Liberal use of CFLOCATION Disk I/O
ColdFusion Debugging Logs Overuse (CFAS) Fill disk (DB, WWW, CFAS) Client variable storage in registry Debug settings on IP specific Third party application servers sharing box Extraneous services ScreenSaver, etc.