350 likes | 546 Views
Improving .NET Application Performance and Scalability. Steven A Smith ASPAlliance.com ssmith@aspalliance.com Blog: http://aspadvice.com/blogs/ssmith/. Microsoft Regional Director Microsoft ASP.NET MVP ASPInsiders Board Member INETA Speaker Bureau Member Author and Trainer Owner of
E N D
Improving .NET Application Performance and Scalability Steven A Smith ASPAlliance.com ssmith@aspalliance.com Blog: http://aspadvice.com/blogs/ssmith/
Microsoft Regional Director Microsoft ASP.NET MVP ASPInsiders Board Member INETA Speaker Bureau Member Author and Trainer Owner of ASPAlliance.com LakeQuincy.com DevAdvice.com RegExLib.com About Me
INETAThe Face of .NET • World-wide association of .NET User Groups • 860+ groups world-wide • Over 810,000 developers • Started and run by .NET User Group leaders • Sponsors and supports both .NET and other professional user groups all around the world • Sponsors • Microsoft and MSDN • New sponsors welcome! • North America (NORAM) INETA Speaker Bureau consists of 57+ world-renowned speakers www.ineta.org
Agenda • What is Performance? Scalability? • Plan For Performance • Design For Performance • Design Principles • Measuring Performance • Tuning Performance • Best Practices • Tools • Resources
What is Performance? Scalability? • Performance • “Achieving response times, throughput, and resource utilization levels that meet objectives.” • Scalability • “The ability to handle additional workload, without adversely affecting performance, by adding resources such as CPU, memory, etc.”
Plan For Performance • Define Objectives – Be Specific • Key Scenario • Maximum Response Time • Throughput (tx or requests per second) • Resource Utilization (CPU, etc.) • Workload (total users, concurrent users, total data size, total transaction volume, etc.) • “The pages should load fast” – not enough
Example Scenario: eStore Checkout • Scenario: eStore Checkout Transaction • Response Time: less than 2 seconds • Throughput: Up to 25 concurrent transactions • CPU must be kept under 25% on web and database servers • Workload: • 50,000 registered users • 500 concurrent shopping carts • 5,000 items in store • 25,000 total transactions
Why Is This Needed? • Without a plan: • What is “good enough” performance? • Late Feedback • Performance easily forgotten in design
Example – The “Pure OO” Website • With 1 User – Works Great! Let’s ship it! • … • With 5 Users, a bit slow… • With 10+ Users – Crawl.
Design For Performance and Scalability • Measure Key Scenarios Early • Example: Login, Checkout, Search • Measure High Traffic APIs Early • Example: Authentication, Data Access • Record Results with Date/Build • Monitor to see if trending toward or away from goals • Make Key Variables Configurable • Example: ASP.NET Session
Design Principles • Use Course-Grained Services • Chunky – not chatty – communication • Minimize Round Trips – Batch Work • Especially across network • Acquire Late. Release Early. • When using shared resources • Put the processing closer to the resources it needs • Colocate data and data processing
Design Principles (cont.) • Avoid Context Switching • Batch work whenever possible • Avoid Server (or any resource) Affinity • Avoid Reliance on Local Files • Avoid Reliance on Local Memory • Avoid unnecessary work • Things should be made as simple as possible—but no simpler. -- Albert Einstein
Design Principles (again) • Coupling • Degree of dependency between parts of a system • Cohesion • Measures how many parts of a system take advantage of shared data and logic • Design applications and application layers for loose coupling and high cohesion
Measuring Performance • Start early • Create an automated performance testing suite and run it regularly • Monitor how many tests pass required performance level • Record results with date/build • Read – How to Time Managed Code • http://tinyurl.com/kfzsf
Performance Measuring Tools • CLR Profiler • NetMon • SQL Profiler • Application Instrumentation • WAST/ACT/ANTS/LoadRunner • Performance Monitor
Tuning Performance • “There is always exactly one bottleneck.” (me) • Find. Remove. Repeat. • Iterative and Repeatable • Adjust Configuration Settings, not code • If necessary, refactor code to be configurable • Capture metrics, analyze, repeat • Test one thing at a time!
Good Performance Best Practices • using() statements and try-catch-finally • Avoid passing open connections between layers • Avoid exceptions unless exceptional • Use StringBuilder for string concatenations within loops • Defrag and Rebuild database indices regularly
More Best Practices • Instrument code • Debug, Trace statements • Wrap system calls with your own • Consider Logging (EntLib, Log4Net, etc) • Data Access • Minimize server resources required • Minimize data passed over wire • Use transactions sparingly
Even MORE Best Practices Sprocs or Inline SQL? • Either can be done wrong; either can perform well. • Use Parameters (not string building) for each • Avoid dynamic sql and business logic in sprocs • Batch statements whenever possible • Consider means to maintain, version non-sproc sql statements
Still MORE Best Practices DataSet or DataReader? • Favor the DataReader, unless… • Crossing machine or app layer boundaries • Data can (and should) be cached • Do NOT Cache DataReaders • Also, do not cache SqlDataAdapters • Avoid passing DataReaders across layers • Know the danger; use delegate pattern if you must do so. • http://aspalliance.com/526
Build Efficient ASP.NET Pages • Keep page size small • Use css/script files for reuse • Ensure all graphics are cached on client • Use IsPostBack to minimize first-load setup • Disable ViewState Everywhere Possible • EnableViewState=“false” • Consider storing ViewState in Cache • Favor InProc Sessions if needed • Turn off or set to readonly where possible • EnableSessionState=“false|readonly”
Async Pages • Greatly Improve Scalability • Fewer threads blocked in wait state • More threads available to process incoming requests • On pages making multiple DB calls, can improve performance • 3 queries at 1s each = 3s in serial • 3 queries at 1s each = 1s in parallel • Server and Client Side Methods
Async Pages • Method 1: @Page Async=True • Call AddOnPreRenderCompleteAsync() • Method 2: @Page Async=True • Call RegisterAsyncTask(task); • Supports Timeouts and multiple tasks • Method 3: Use AJAX • Page simply renders divs and script • Script blocks get content from web services
More Async – Client Side • Many rich sites have dozens of external resources per page • Most browsers only download 2 or 4 external resources at a time (HTTP standard) per domain • Recommendation: Use subdomains (or different domains) to partition css, images, script into separate domains from main site
Steve’s Top 5 Perf Tips for ASP.NET • Add Caching Where Appropriate • Output/Fragment/Cache API • Minimize ViewState and Excess HTML • Move CSS/JavaScript to library files • Eliminate HTML Comments, Whitespace • Add Indexes to DB Where Needed • Batch and/or call async all DB calls per page • Replace Heavy Data Controls with Response.Write() for data display
Profiling / Tuning Tools • FxCop / Static Code Analysis (VSTS) • CLR Profiler • SQL Profiler / SQL Analyzer • MS ACT / WAST / VSTS • DbgView, ProcView (SysInternals) • Performance Monitor • VS 2005 Test
Case Study - Profiling • Launched new app 8/1 on 3-server farm • CPU spiked up to 40-80% from 20-40% • Using profiler and load tester, isolated problem to a URL rewriting HTTP Module – removed the module • Module was present before upgrade • Module was not changed! • CPU dropped to 5-15%
Case Study: ASPAlliance.com • Maps all requests to ASP.NET to allow shorter URLs with no extensions • Home page uses about 10 script files and images • Page was sluggish to render, taking 2-4 seconds • Moved these to separate domain • Page loads in about 1 second
Demo Performance Tuning In Action Caching Impact
Resources • Improving .NET Application Performance and Scalability (ISBN: 0-7356-1851-8) • Errata: http://tinyurl.com/crv98 • ASP.NET Performance Mailing List • http://tinyurl.com/d6qes • MS Patterns and Practices: Performance and Scalability • http://tinyurl.com/8o2sp • Time Managed Code • http://tinyurl.com/kfzsf
More Resources • SQL Tuning (ISBN: 0-596-00573-3) • ASP.NET Caching Reference • http://aspalliance.com/ref/caching.aspx • Red-Gate ANTS • http://red-gate.com/products/ANTS_Profiler/index.htm • Mercury LoadRunner • http://www.mercury.com/us/products/ • FxCop • http://www.gotdotnet.com/team/fxcop/ • ViewState in Cache Article • http://aspalliance.com/72 • http://www.eggheadcafe.com/articles/20040613.asp
Thank You! • Please fill out evaluation forms and drop them off in the basket near the exit, or at the conference registration desk. • Enjoy the rest of the conference!
Contact Info • Email • ssmith@aspalliance.com • Blog • http://aspadvice.com/blogs/ssmith/ • Websites • http://ASPAlliance.com/ • http://ASPAdvice.com/ • http://RegExLib.com/