ASP.NET Stability St. Louis .NET Users Group April 25, 2005 Preston Page What’s Great About .NET Advanced Language Options Extensive Framework for Windows, Web and Device Programming Greatly Improved Productivity Enhanced Performance Simplified Deployment Best Stability Yet When Stability Fails… End User Experience Suffers Poor First Impression Lost Credibility Lost Opportunity Lost Revenue Increased Support Costs Increased Operations Costs Panic Mode, Escalations Costly Rewrite Six Stability Threats 1. 2. 3. 4. 5. 6. Improperly Structured Error Handling “Hidden” Memory Leaks Non-Compliance with Coding Standards Poor Performance Database Issues Inadequate Testing Improper Error Handling Cause of 80 – 90 % of Stability Problems Missing Altogether? Empty Catch Statement Too Much Faith in Garbage Collection No Finally Statement Objects Out of Scope for Finally Inadequate Object Cleanup Missing Logging/Alerting Recursion from Catch Statement Style Problems Anatomy of Try…Catch Try ' Starts a structured exception handler. ' Place executable statements that may generate ' an exception in this block. Catch [optional filters] ' This code runs if the statements listed in ' the Try block fail and the filter on the Catch ' statement is true. [Additional Catch blocks] Finally ' This code always runs immediately before ' the Try statement exits. End Try ' Ends a structured exception handler. MSDN Help Sample Function GetStrings (ByVal FileName As String) As Collection Dim Strings As New Collection Dim Stream As System.IO.StreamReader Stream = System.IO.File.OpenText(FileName) Try While True Strings.Add(Stream.ReadLine()) End While Catch eos As System.IO.EndOfStreamException ' No action is necessary; end of stream Catch IOExcep As System.IO.IOException ‘ Unexpected IO Error Strings = Nothing ' Caller must test Null Finally Stream.Close End Try Return Strings End Function Subtle Problems… Function GetStrings (ByVal FileName As String) As Collection Dim Strings As New Collection Dim Stream As System.IO.StreamReader Stream = System.IO.File.OpenText(FileName) (1) ‘ Scope Try While True (2) ‘ Deliberate Infinite Loop Strings.Add(Stream.ReadLine()) End While Catch eos As System.IO.EndOfStreamException (3) ‘ Empty ' No action is necessary; end of stream Catch IOExcep As System.IO.IOException ‘ Unexpected IO Error Strings = Nothing ' Caller must test Null Finally (4) Stream.Close End Try Return Strings (5) ‘ May Not Be Called End Function Improvements Function GetStrings(ByVal FileName As String) As Collection Dim Strings As New Collection Dim Stream As System.IO.StreamReader Try Stream = System.IO.File.OpenText(FileName) (1) Do While Stream.Peek() >= 0 (2) Strings.Add(Stream.ReadLine()) Loop Catch e As System.Exception (3) LogException(e) Strings = Nothing ' Caller must test Null Finally (4) Stream.Close() GetStrings = Strings (5) Stream = Nothing Strings = Nothing End Try End Function Hidden Memory Leaks Do Not Immediately Cause Errors Usually Pass Code Scans/Reviews Many Are Not Well Documented Luckily, Existence Can Be Determined by Load Testing Ten Ways To Leak Memory 1. 2. 3. Improper Structured Error Handling Implicit ADO/ADO.NET Connections Never Close Under Load Failure to call Server.Clear when trapping errors in Application_OnError causes a memory leak and WP resets Ten Ways To Leak Memory 4. 5. 6. Calling a delegate function with BeginInvoke() without calling a matching EndInvoke() Failure to Set Objects to Null Or Nothing Passing or setting an open ADO Connection object to a Property Ten Ways To Leak Memory 7. 8. 9. Use of Response.Redirect in a Catch Statement leaves threads open, severely limits performance Calling Transactional COM+ Components from ASP.NET Failure to Close Database or Stream Objects Ten Ways To Leak Memory 10. Failure to Properly Dispose of COM Interop and .NET COM Wrapper Objects Like: System.EnterpriseServices (COM+) System.DirectoryServices (ADSI) Coding Standards Compliance Standards Embody Time Tested Best Practices To Keep You Out Of Trouble Compliance Reduces Support and Maintenance Costs Avoid Common Structural Defects Use FXCop To Scan Code Adapt Rules To Particular Needs FXCop Demo http://www.gotdotnet.com/team/fxcop/ FXCop Stability Rules* Library design* Localization Interoperability* Mobility Portability Naming conventions Performance* Security* Usage* Performance Problems Cause Error Conditions Under Load Reduce Scalability Increase Hosting Costs Increase Support Costs Poor End User Experience Catch With Load/Stress Tests Common Database Issues Structure Problems Tuning “Opportunities” Inefficient Data Access Code Coding Standards Non-Compliance Failure To Use Caching Options Inadequate Maintenance Catch With Load/Stress Tests Test Methodology Test Driven Development (NUnit) Regular Code/Design Review (XP) Unit Testing (NUnit, custom harness) Functional Tests (formal plan) User Acceptance Tests Integration Tests (QC Servers) Load Test (ACT, LoadRunner) Stress Test (ACT, LoadRunner) Formal Load Tests Performed On Calibrated, Production Class Servers Used To Judge Impact To Shared Web Environments Use To Gate Deployment To Protect Infrastructure From Performance And Stability Problems Usually Requires Operations Involvement, Special Infrastructure Also Helps Uncover Configuration And Infrastructure Issues Desktop Load Testing Baseline Performance To Gauge Effectiveness Of Changes Uncover Performance and Stability Issues As Early As Possible Uncover Costly Design Flaws Early Cheap And Easy Insurance ACT Demo Application Center Test Can Determine Load Induced Error Memory Leaks Poor Performance Database Issues End User Experience Other Server Impact Cannot Determine Missing Structured Error Handling Adherence to Coding Standards Application Architecture Functional Issues Load Test Pass/Fail Criteria Metric Goal RPS/% CPU 50 RPS / 70% CPU % Committed Bytes < 22% Residual Memory < 10 MB Total Socket Errors 0 Total HTTP Errors 0 Time Outs 0 WP Restarts 0 Memory Slope ~0 Memory Problems Machine.Config Tuning Reduce idleTimeout from “Infinite” Adjust responseDeadlockInterval for long running applications Use <location/> Node To Enforce Best Practices and Control DEV, QC and PROD Settings Machine.Config Threading Configuration setting Default Recommendation maxconnection 2 12 * #CPUs maxIoThreads 20 100 maxWorkerThreads 20 100 minFreeThreads 8 88 * #CPUs minLocalRequestFree 4 Threads 76 * #CPUs More Information MSDN.Microsoft.com • Knowledge Base Articles • Patterns & Practices www.ASP.NET • Starter Kits • Tools www.Gotdotnet.com • FXCop • Help & Samples Bibliography as Web Links • Send email to Preston Questions? prestonpage@charter.net