350 likes | 546 Views
Improving .NET Application Performance and Scalability. Keith Pleas Guided Design keithp@guideddesign.com. Agenda. Microsoft patterns & practices Performance & Scalability Guide (Overview) Top 10 List Download Links. Problem Statement. Unsuccessful IT projects - CIO Survey
E N D
Improving .NET Application Performance and Scalability Keith Pleas Guided Design keithp@guideddesign.com
Agenda • Microsoft patterns & practices • Performance & ScalabilityGuide (Overview) • Top 10 List • Download Links
Problem Statement • Unsuccessful IT projects - CIO Survey • 73% of IT projects considered failures • Not on time or budget, don’t meet requirements • 50% of time considered “churn time” • Unproductive time, not directly related to deliverable • Lack of standard target architectures • Disconnect between dev and operations
Architecture Guidance (Marketing Alert!)Microsoft patterns & practices • Proven Based on field and partner experience • Authoritative Best advice available • Accurate Technically validated and tested • Actionable Provide the steps to success • Relevant Address real world scenarios Available onlinehttp://www.microsoft.com/practices Books now available from Amazon.com
Application Security Application Architecture Authentication Windows Forms, Passport, None (Custom) Authorization Web Permissions, NTFS Permissions, URL authz, File authz, Principal permissions, .NET Roles Authentication Windows, SQL Authorization Logins, Permissions, Roles IIS Anonymous Basic Digest Integrated Windows Certificate Operational Management Security Communication User Interface Components User Process Components Optimizing Changing IIS/ASP.NET Web Application IIS/ASP.NET Web Services IIS/ASP.NET Remoting Clients Service Interfaces SQL Server Business Entities Business Workflows Business Components Database Server Enterprise Services Web Server RPC None Connect Cal Packet Integrity Privacy Authentication RPC Authorization ES (COM+) Roles NTFS Permissions Data Access Logic Components Service Agents Data Source Service Secure Communication (SSL/IPSec) MOF Patterns Lifecycle Practices Operating Supporting Infrastructure Database Application Deployment Optimize cost, performance, capacity, and availability Introduce new service solutions, technologies, systems, applications, hardware, and processes Release Approved Architecture Design SLA Review Release Readiness Track and resolve incidents, problems, and inquiries quickly. Facilitate CRM Execute day-to-day operations tasks effectively Implementation Operations Review patterns & practices - Examples
Sample App – Financial Application User Interface Process Smart Client Offline Agent Caching Authorization andProfile ExceptionManagement Service Aggregation Application Updater Configuration Data Access (SQL, plan: Oracle, OleDB) Persistent Asynchronous Invocation ShippedIn Development Application Blocks (& Application) for .NET Presentation Services Communication Security Operational Management Business Services Service Integration Data Services Data Sources Services
Application Challenges • Defining performance targets • Measuring performance • Troubleshooting performance problems • 80/20 Rule: 80% of performance problems are caused by 20% of the issues • Architecting systems that have the right balance of security, performance, manageability and scalability • Performance: The time it take to complete a single request • Scalability: Add capacity by adding more hardware
Code First, Optimize Later • Performance is typically ignored until there is a problem. There are several problems with this approach: • Performance problems are often introduced early in the design • Design issues can’t always be fixed through tuning or more efficient coding • Fixing an architecture or design issue later in the cycle is inefficient and often very expensive.
Make it fast approach • Developers work on optimizing everything up front. This results in: • “Gold Plated” code • Highly optimized code that is rarely executed • Missed schedules
Need a better way • Build performance and scalability thinking in the development lifecycle • Balance trade offs and document the thinking • Security vs. Performance • Maintainability vs. Performance • Regularly measures performance trends When you can measure what you are speaking about, and express it in numbers, you know something about it; but when you cannot express it in numbers, your knowledge is of a meager and unsatisfactory kind; it may be the beginning of knowledge, but you have scarcely in your thoughts advanced to the state of science. - Lord Kelvin (William Thomson)
Performance and Scalability framework Performanceand Scalability Roles Principles Categories Lifecycle Performance Modeling Requirements Gathering Design Development Testing Deployment Maintenance
Top 10 developer misconceptions about performance and scalability with .NET
“I don’t need to worry about resource management – .NET handles that for me auto-magically!” 10 • .NET is a managed execution environment which uses garbage collection to clean up resources • Understanding the garbage collection strategy used by the CLR can improve your application performance Memory Management Recommendations • Prevent the promotion of short lived objects • Allocate long lived objects at application initialization • Minimize Hidden allocations • Avoid pre-allocating and chunking memory • Avoid or minimize complex object hierarchies • Identify and analyze your application’s allocation profile • Do not call GC.Collect
Prevent promotion of short lived objects 10 • Object allocation lifecycle • Strategy: Compact heap by retaining longer lived objects and moving them to contiguous blocks of memory • Short lived objects referenced from long lived objects become long lived
Do not call GC.Collect 10 • GC.Collect causes a full collection • Runs the finalization process on a high priority thread • GC is designed to be self-tuning – don’t try to help it • If you must call collect be sure to call WaitForPendingFinalizers // Don’t do this unless you have to System.GC.Collect(); // Block this thread until finalizers are done System.GC.WaitForPendingFinalizer(); // Now survivors get promoted to next generation System.GC.Collect();
“The Visual Studio debugger is all I need to troubleshoot performance and scalability problems” 9 • VS.NET is good but the pros use a wide variety of tools Debugging Tools
Guide How Tos… 9 • Step by step guidance on how to use a tool • Where to get it • How it works • Walkthrough of using the tool with various scenarios
“The session support in ASP.NET is so cool – I use it for everything!” 8 • Session state can be a powerful convenience but it can introduce performance and scalability problems • Resource Affinity • Excessive Serialization Costs ASP.NET Recommendations • Partition your application logically • Avoid affinity in your design • Reduce roundtrips • Avoid blocking on long-running tasks • Design for caching • Separate secure and non-secure pages • Design code to avoid exceptions
Avoid Affinity 8 • Avoiding affinity is the key to scalability in all the tiers • Avoid affinity with • Web server resources (session state, disk files etc.) • Middle tier components (COM+ Shared Property Manager) • Threads (Thread Local Storage) • User Identity (enables pooling) • Freedom to execute wherever resources are available
Session State 8 • Prefer basic types to reduce serialization costs • ASP.NET optimizes serialization of basic types (Int, Byte, Decimal, Float, DateTime and String ) • Storing 2 strings is faster than storing an object with 2 string members • Disable session state when you do not use it • Disable in machine.config or web.config • <sessionState mode='Off' /> • Disable at page level • <% @Page EnabeSessionState="false" . . .%>
Testing Pre-Requisites Fixing Collecting Analyzing “I reserved some time at the end of the project for performance tuning” 7 PRE-REQUISITES: Goals; Test Plan; Baseline TESTING: Test plan; Tools; Automated COLLECTING: Sources; Format; Extended Period ANALYZING: Symptoms-Root Cause; Document; Compare FIXING: Prioritize; Apply one at a time …go back to testing!!
Performance Tuning 7 Bottlenecks Others System Application • Your Code • IIS • - ASP.NET • - Enterprise Services • - Web Services • - Remoting • - Interop • - SQL Server - CPU - Memory - I/O - Policies & Procedures - People - Infrastructure
“VB6 COM objects work just fine from ASP.NET –Why bother updating them to VB.NET?” 6a • ASP.NET runs in the MTA by default • All calls to VB6 COM objects must be marshaled into STA • To avoid this use ASPCOMPAT mode on your pages • <%@Page language="vb" aspcompat="true" %> • Avoid creating STA components to initialize variables • ‘ This call is executed on an MTA thread even with ASPCOMPAT • Dim myComp as new MySTAComponent() • Never store an STA component where it can be accessed by other threads (cache, session state) • Porting the code to VB.NET will make it run much faster
“PInvoke is a great way to invoke unmanaged code –you can’t make it any faster” 6b • The CLR does expensive CAS security stack walks on every call into that method to insure that all callers have unmanaged code access permissions • For non-security sensitive scenarios, disable the security check for better performance // Use only when security is not a major concern [DllImport("kernel32.dll"), SuppressUnmanagedCodeSecurity] public static extern bool Beep(int frequency, int duration);
TLBImp and /unsafe switch 6 • TLBIMP generates interop assemblies • Disable the CAS stack-walks that by building interop assemblies with the TLBIMP /unsafe switch • Forces TLBIMP to generate code that creates RCWs that perform link demands rather than full demands • Use with caution! Can open the door to luring attacks C:\>tlbimp mycomponent.dll /out:UnSafe_MyComponent.dll /unsafe
r "application must run fast" “Page should load quickly" a "3 second response time" "25 transactions per second" “The application seems fast enough” 5 • Measure, don’t guess • Find the metric that defines success and work towards it • Run trend analysis on each build • Goals must be SMART • S – Specific • M – Measurable • A – Achievable • R – Results Oriented • T – Time Specific
“I love to use the DOM for XML processing” 4 • XmlDocument must load the entire file into memory • What about those 100MB XML files? • Use the right XML Class for the job
“I can help ASP.NET run faster by creating threads to make long running calls to remote web services” 3 • Creating threads is an expensive operation and you must clean up threads that were created • Using the CLR thread pool consumes threads that ASP.NET would use to process other server requests
Long Running Operation Recommendations 3 • Avoid polling to retrieve asynchronous results • Consider WaitHandle to retrieve asynchronous results • Use HTTP event handlers for greater scalability • Use the OneWay attribute to enforce asynchronous calls • Use server side asynchronous calls for long running operations • Consider the Asynchronous Invocation Framework • Provide timeout or cancel options for asynchronous operations
“I love SQL…SELECT * works for everything!” 2 • Most applications spend the majority of their time waiting on database access • Reduce database access by caching • Optimize database access by • Optimizing queries • Constructing UI to narrow the scope of search • Retrieving only what you need (NOT SELECT *) • Using Indexes appropriately • Do not “Over Normalize” your database
“Of course it’s fast enough – it runs fine on my machine!” 1
Build-in Performance, Scalability and Security 1 • Use performance modeling as a key part of your process • Performance becomes part of your design • Evaluate trade-offs before you build the solution • You avoid performance surprises when you go into production
Call To Action • Use patterns & practiceshttp://www.microsoft.com/practices • Join the GotDotNet workspacehttp://workspaces.gotdotnet.com/perfscal • Performance-oriented samples (PetShop 3.x, etc.)www.gotdotnet.com/team/compare