Mark Friedman Rico Mariani Vance Williams Architect Developer Division Microsoft Corporation RicoM RicoM VanceM VanceM VanceM MarkFr MarkFr Rico Mariani Architect Microsoft Corporation Managed Code http://blogs.msdn.com/ricom http://msdn.microsoft.com/en-us/library/aa338212.aspx http://blogs.msdn.com/maoni/ http://blogs.msdn.com/tess/ Vance Morrison Performance Architect Microsoft Corporation Stopwatch sw = Stopwatch.StartNew(); // Something being measured sw.Stop(); Console.WriteLine("Time = {0:f3} MSec", sw.Elapsed.TotalMilliseconds); •If you don’t do this, CPU is typically throttled to save power •Throttling causes less consistent measurements 1) Go to Control panel’s Power Options 2) Set to High Performance Vance Morrison Vance Morrison Related Sessions Session Title Speaker Day Time Location Sameer Chabungbam Mon 12:45-1:30PM 515B Hazim Shafi Mon 1:45-3:00PM 502A WCF: Zen of Performance and Scale Nicholas Allen Tues 12:45-1:30PM 515B SQL Server 2008: Developing Large Scale Web Applications and Services Jose Blakeley Tues 1:45-3:00PM 411 Using Instrumentation and Diagnostics to Develop High Quality Software Ricky Buch Tues 5:15-6:30PM 408B TL24 Improving .NET Application Performance and Scalability Steve Carol Wed 1:15-2:30PM 153 Parallel Programming for Managed Developers with the Next Version of Microsoft Visual Studio Daniel Moth Wed 10:30-11:45AM Petree Hall PC53 Lunch: Building High Performance JScript Apps Microsoft Visual Studio: Bringing out the Best in Multicore Systems Related Labs HOL Code TLHOL11 Title VSTS 2010: Diagnostics and Performance Vance Morrison Performance Architect Microsoft Corporation L1 Cache L2 Cache Memory Disk Size 64K 512K 2000K 100,000K Access Time .4ns 4ns 40ns 4,000,000ns Select Columns Working Set tends to Overestimates Memory Impact (shared OS files) But does not account for read files. Private Working Set Underestimates Impact Small < 20Meg WS Med ~ 50 Meg WS Small < 5 Meg Private Med ~ 20 Meg Private Large > 100 Meg WS Large > 50 Meg Private Adding New Counters 1 Open .NET CLR Memory 2 Select Counters 3 Select Process 4 Add 5 OK Set Display to Report GC Heap Size % Time in GC So 7.3 Meg of the 8.6 Meg of private working set is the GC Heap % Time in GC ideally less than 10% Ratios of GC Generations Gen0 = 10 x Gen 1, Gen 1 = 10 x Gen 2 1 Total WS 2 Breakdown 3 DLL Breakdown GC Heap Here Only These Affect Cold Startup In Either Case when Working Set Large (> 10Meg) Throughput is lost due to cache misses Server workloads are typically cache limited Thus it Pays to Think about Memory Early! Finding Anomalous Memory use (AKA Leaks) XmlView Demo Related Sessions Session Title Speaker Day Time Location Sameer Chabungbam Mon 12:45-1:30PM 515B Hazim Shafi Mon 1:45-3:00PM 502A WCF: Zen of Performance and Scale Nicholas Allen Tues 12:45-1:30PM 515B SQL Server 2008: Developing Large Scale Web Applications and Services Jose Blakeley Tues 1:45-3:00PM 411 Using Instrumentation and Diagnostics to Develop High Quality Software Ricky Buch Tues 5:15-6:30PM 408B TL24 Improving .NET Application Performance and Scalability Steve Carol Wed 1:15-2:30PM 153 Parallel Programming for Managed Developers with the Next Version of Microsoft Visual Studio Daniel Moth Wed 10:30-11:45AM Petree Hall PC53 Lunch: Building High Performance JScript Apps Microsoft Visual Studio: Bringing out the Best in Multicore Systems Related Labs HOL Code TLHOL11 Title VSTS 2010: Diagnostics and Performance Vance Morrison Performance Architect Microsoft Corporation 5 6 For all the same Reasons it is for People Anonymous Delegate 6 0 You can capture variables int loopCount = 10; for (int tId = 1; tId <= 3; tId++) { // Create three work items. ThreadPool.QueueUserWorkItem(delegate { for (int i = 0; i < loopCount; i++) { Console.WriteLine("Thread i={0} time: {1}", i, DateTime.Now); Thread.Sleep(1000); } }); } Console.WriteLine("Waiting for workers. Hit return to end program."); Console.ReadLine(); 160 140 Reasons for perf degradation 1. Context switches 2. Memory contention 3. Hot Locks 4. Disk Contention 5. Network Contention 120 Throughput 100 80 60 40 20 0 0 5 10 15 20 25 30 Concurrency Level 35 40 45 50 Work items with 10% CPU on a dual core system using ThreadPool.QueueUserWorkItem(). 6 2 Tasks now have handles You can cancel work You can wait on the handle to synchronize Task task1 = Task.Create(delegate { Console.WriteLine("In task 1"); Thread.Sleep(1000); // do work Console.WriteLine("Done task 1"); }); Task task2 = Task.Create(delegate { Console.WriteLine("In task 2"); Thread.Sleep(2000); // do work Console.WriteLine("Done task 2"); }); If (userAbort) { task1.Cancel(); task2.Cancel(); } Task.WaitAll(task1, task2); Exceptions in tasks propagated on wait Left and right represent integers, but they may not be computed yet Asking for the value forces the wait if the future is not already done Future<int> left = Future.Create(delegate { Thread.Sleep(2000); // do work return 1; }); Future<int> right = Future.Create(delegate { Thread.Sleep(1000); // do work return 1; }); Console.WriteLine("Answer {0}", left.Value + right.Value); In this case the right value was done, so no wait was needed Loops for(int i = 0; i < n; i++) { work(i); } foreach(T e in data) { work(e); } Parallel.For(0, n, i => { work(i); } Parallel.ForEach(data, e => { work(e); } Enable LINQ developers to leverage parallel hardware Augments LINQ-to-Objects, doesn’t replace it Related Sessions Session Title Speaker Day Time Location Microsoft Visual Studio: Bringing out the Best in Multicore Systems Hazim Shafi Mon 1:45-3:00PM 502A TL24 Improving .NET Application Performance and Scalability Steve Carol Wed 1:15-2:30PM 153 Parallel Programming for Managed Developers with the Next Version of Microsoft Visual Studio Daniel Moth Wed 10:30-11:45AM Petree Hall David Callahan Thurs 8:30-10:00AM 515A Dave Detlefs Thurs 12:00-1:30PM 515A Parallel Symposium: Addressing the Hard Problems with Concurrency Parallel Symposium: Future of Parallel Computing Mark Friedman Architect Developer Division Microsoft Corporation 72 Dynamic <A HREF="resumepage.html">my resume</A> <LINK REL=STYLESHEET HREF="mystyles.css" TYPE="text/css"> 73 connectionless sessionless 74 Postback HttpContext ViewState Session State 75 76 On the client Performance counters Network Monitor traces ETW traces (IE, etc.) Direct measurements inside Javascript code On the server IIS logs (especially Time-Taken fields) Performance counters web app response time data is not available ETW traces (IIS, ASP.NET, CLR, etc.) volume considerations Direct measurement inside .NET code e.g., Stopwatch 77 End-to-End Multiple tiers 78 network latency Client-side script Client-side script Client-side script Network Latency Server-side .aspx Unit Test Network Latency Network Latency Server-side .aspx Load Test e.g., VS TeamTest Server-side .aspx Production 79 Page Load Time Ready 80 81 82 VRTA 84 85 Semi-dynamic 86 may be 87 effective Caching Architecture Guide for .NET Framework Applications 88 w3wp.exe Common Language Runtime (CLR) JIT compiler Garbage Collection threads MyApp.dll mscoree.dll mscorsvr.dll 89 runat=“server” Form: <asp:DropDownList id="FileDropDownList" style="Z-INDEX: 111; LEFT: 192px; POSITION: absolute; TOP: 240px“ runat="server" Width="552px" Height="40px“ AutoPostBack="True"></asp:DropDownList> Code behind: Private Sub FileDropDownList_SelectedIndexChanged _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles FileDropDownList.SelectedIndexChanged Session("FileSelected") = FileDropDownList.SelectedValue End Sub 90 IIS Architecture Application Pool Application Pool IIS Administration net.tcp net.tcp http http Metabase Cache Windows Authentication WAS FTP SSL SMTP W3SVC NNTP LSSAS Inetinfo SVCHOST Default.aspx Default.aspx <code-behind>.dll <code-behind>.dll Mscoree.dll Mscoree.dll W3wp W3wp W3wp W3wp User Kernel HTTP HTTP Response Cache TCP IP Network Interface HTTP Request HTTP Kernel Mode Driver (http.sys) (Physical Memory) 91 http.sys Response object cache Web Service Cache\Kernel: Current URIs Cached, etc. See “IIS 7.0 Output Caching” at http://learn.iis.net/page.aspx/154/iis-7-output-caching/ 92 w3wp.exe 93 w3wp.exe From a command line: \windows\system32\inetsrv\appcmd list WP 94 96 97 Request Life-cycle Events Begin Request Authentication Authorization Resolve Cache Map Handler Acquire State Execute Handler Release State Update Cache Log Request Send Request 98 Event Event BeginRequest PreRequestHandlerExecute AuthenticateRequest PostRequestHandlerExecute PostAuthenticateRequest ReleaseRequestState AuthorizeRequest PostReleaseRequestState PostAuthorizeRequest PostMapRequestHandler ResolveRequestCache PostMapRequestHandler PostResolveRequestCache PostMapRequestHandler MapRequestHandler UpdateRequestCache PostMapRequestHandler PostUpdateRequestCache AcquireRequestState LogRequest PostAcquireRequestState EndRequest 99 <modules> 100 RequestNotification Mark Friedman Architect Developer Division 102 103 Level = 5 (Diagnostic level) 104 tracerpt tracerpt filename.etl –of CSV –o mytrace.csv logparser logparser "select * from filename.etl" -e 20 -o:DATAGRID -rtp 30 -compactModeSep "|" xperf 105 106 107 UseUrlFilter to control the volume of data Configure the TraceUriPrefix See “How to Trace Requests for a Specific URL or Set of URLs” at http://www.microsoft.com/technet/pro dtechnol/WindowsServer2003/Library/II S/c56d19af-b3d1-4be9-8a6f4aa86bacac3f.mspx?mfr=true 108 HttpContext Request Response Session Cache 109 HttpContext.Request HttpMethod (GET, POST, etc.) URL Cookies collection Headers InputStream UserHostAddress (IP address of the Requestor) etc. The ASP.NET programming model provides several facilities to persist User/Application state 110 Event Usage PreInit Create dynamic controls, set the Theme; master page, etc. Init Read or initialize control properties InitComplete Raised by the Page object. PreLoad Perform any processing on your page or control before the Load event. Load The Page calls the OnLoad event method on the Page, then recursively for each child control and its children Control events Button Clicks and other Control events are processed after Page_Load LoadComplete Fires after all controls on the page are loaded. PreRender Data binding for controls occurs now. SaveStateComplete Fires when the ViewState for all controls is complete and saved. Render Method that writes out the html markup associated with the control. Unload Do final cleanup, such as closing files or database connections 111 Session State Cache e.g., Presentation Layer Business Logic Layer Data Layer 112 113 HttpApplicationState 114 Application Page HttpApplicationState Page.Cache Session 115 State Management ViewState Stored in _VIEWSTATE hidden field ControlState Override if ViewState is turned off on the Page HiddenField control Cookies Add cookie data to the Cookies collection in the HttpResponse object Query strings Application State HttpApplicationState Session State Profiles SqlProfileProvider 116 ViewState 117 ViewState Passed to the client in _ViewState hidden field in the Response message Returned to the Server on a postback Request Inspect using View html Source on the client 3rd party tools like Fiddler Be careful of Data bound controls (GridView, etc.) Large TreeViews, DropDownLists, etc. 119 120 Page.Application Application.Lock(); Application["PageRequestCount"] = ((int)Application["PageRequestCount"])+1; Application.UnLock(); 121 ASP.NET Session state HttpContext.Session Data associated with a logical sequence of Requests that need to be persisted across interactions Unique session IDs are passed along automatically with each Request as either cookies or embedded in the URL Session data is stored in a Dictionary collection, allowing individual session state variables to be accessed directly by Key name Three external storage options InProc StateServer SQL Server Timeout management 122 HttpContext.Session InProc option provides fastest service, but does not permit access to Session data from a different process in a Web garden application or a different Web server in a cluster Using alternatives to InProc session storage has significant performance implications StateServer SQL Server Custom provider Measure impact using the IIS RequestNotification events e.g., AcquireRequestState, PostAcquireRequestState 123 Session State options InProc StateServer SQLServer 124 125 Session State options InstallSqlState.sql InstallPersistSqlState.sql sessionState 126 Frequency of re-use Cost to re-generate Size Max(Cache Hit %) Min(Space x Time) 127 Cache Size diminishing returns 0 25 50 75 100 Cache Hit % 128 IIS kernel mode Cache ASP.NET Application cache ASP.NET Page cache 129 Note: effective difficult complex 130 HttpContext.Cache Page.Cache @ OutputCache VaryByParam <PartialCaching> Substitution.MethodName HttpResponse.WriteSubstitution 131 frequency of use x object size Underused 132 133 Cache Dictionary IEnumerable 134 Add Insert Get Remove 135 AggregateCacheDependency SqlCacheDependency http://msdn.microsoft.com/enus/library/system.web.caching.sqlcachedependency.a spx 136 effectiveness Cache API Entries Cache API Hits Cache API Misses Cache API Hit Ratio Cache API Turnover Rate 137 public void RemovedCallback (String k, Object v, CacheItemRemovedReason r){ } Examine the CacheItemRemovedReason 138 InstrumentedCacheValue // unfortunately, it is a sealed class; otherwise… // private Int64 _refcount; // private DateTime _lastreftimestamp; 139 Count EffectivePrivateBytesLimit EffectivePercentagePhysicalMemoryLimit disableMemoryCollection disableExpiration privateBytesLimit percentagePhysicalMemoryUsedLimit privateBytesPollTime="HH:MM:SS" 140 @ OutputCache Duration VaryByParam Location 141 HttpCachePolicy 142 Response.Cache.SetCacheability HttpCacheability 143 effectiveness Cache Entries Cache Hits Cache Misses Cache Hit Ratio Cache Turnover Rate 144 When page caching will not work due to dynamic content [PartialCaching(120)] public partial class CachedControl : System.Web.UI.UserControl // Class Code Banner ads, etc. 145 not change that did AJAX requests Javascript asynchronous HTTP 146 ICallbackHandler <asp:ScriptManager EnablePartialRendering="True“ /> 147 XMLHttpRequest xmlhttp.open('GET', url, true); xmlhttp.onreadystatechange = AsyncUpdateEvent; xmlhttp.send(null); 148 UpdatePanel “ Triggers ChildrenAsTriggers AJAX Application Architecture, Part 1 149 Sys.Application.Init Sys.Application.Load UpdatePanel 150 PageRequestManager pageLoading pageLoaded endReques 151 ICallbackEventHandler RaiseCallbackEvent GetCallbackResul Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web Pages 152 REST AJAX library wraps asynchronous WCF service endpoints Try the new Asynchronous WCF HTTP Module/Handler for long running Requests New in VS 2008 SP1 Releases the IIS worker thread immediately after processing http://msdn.microsoft.com/en-us/library/cc907912.aspx See Wenlong Dong’s blog for details 153 WebHttpBinding 154 Caution: your mileage will vary. Binding Binding Response Time (msecs) Throughput wsHttpBinding 1300 1200 basicHttpBinding 1150 1800 netTcpBinding 400 5100 netNamedPipeBinding 280 7000 Throughput 8000 Source: Resnik, Crane, & Bowen, Essential Windows Communication Foundation, Addison Wesley, 2008. Chapter 4. 6000 4000 2000 0 See also, “A Performance Comparison of Windows Communication Foundation (WCF) with Existing Distributed Communication Technologies” 155 [ServiceContract(Session = true)] interface IMyContract {...} [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] class MyService : IMyContract {...} <netTcpBinding> <binding name="TCPSession"> <reliableSession enabled="true" inactivityTimeout="00:25:00"/> </binding> </netTcpBinding> they are received conversation in the order in which [ServiceBehavior( InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple )] class MySingleton : IMyContract, IDisposable ConcurrencyMode.Multiple requires locking and synchronization of shared state data ConcurrencyMode.Reentrant serviceThrottling <behavior name="Throttled"> <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions=“30" maxConcurrentInstances=“20" /> performanceCounters Mark Friedman Architect Developer Division <system.serviceModel> <diagnostics performanceCounters="All"></diagnostics> But <diagnostics performanceCounters=“ServiceOnly"></diagnostics> is recommended (less overhead) 161 162 “Using Service Trace Viewer for Viewing Correlated Traces and Troubleshooting” Essential Windows Communication Foundation 163 164 165 string using new "http://localhost:8000/HelloService" "http://localhost:8000/HelloService/MyService" new new "Press <enter> to terminate service" 166 Measure: 167 Fiddler 168 TCP Window Size without 169 Best Practices for Improving Page Load Time 171 172 173 174 (serialized) 175 176 177 178 Request Get 179 Silverlight 2 End to End Tutorial: Building a Digg Search Client Silverlight interoperability http://silverlight.net/learn/tutorials.aspx 180 Window StackPanel XAML Label String ListBox StackPanel ListBoxItem ListBoxItem Button Button String String String String Base value Evaluate Animate Coerce Validate See Adam Nathan, Windows Presentation Foundation Unleashed, SAMS, 2007. 181 WPF Class Hierarchy Object DispatcherObject DependencyObject Freezable Visual ContentElement UIElement FrameworkContentElement FrameworkElement Control 182 WPF DispatcherObject WPF uses single thread affinity (STA) to interoperate with IE, the Clipboard, etc. single UI thread render thread (not exposed to developers) additional threads as necessary DispatcherObject.priority can be adjusted dynamically See the DispatcherPriority Enumeration Use DispatcherObject.BeginInvoke to update the UI asynchronously from one of your other threads See Shawn Wildermuth, “WPF Threads: Build More Responsive Apps With The Dispatcher”. MSDN Magazine, October 2007. 183 WPF Performance considerations Uses hardware rendering, when available DirectX vector graphics & PixelShader 2.0+ at least DirectX 9.0 for best results GPU performance varies widely! Falls back on software rendering whenever necessary Big difference in performance Understand Layout measure Pass arrange Pass optional additional Layout passes e.g., Build the logical tree from the top down 184 WPF Performance considerations Utilize virtualized UIElements Virtualized version of the UIElement renders only those child elements that are visible StackPanel vs. <StackPanel DataContext="{Binding Source={StaticResource Leagues}}"> <TextBlock Text="{Binding XPath=@name}" FontFamily="Arial" FontSize="12" Foreground="Black"/> <ListBox VirtualizingStackPanel.IsVirtualizing="True" ItemsSource="{Binding XPath=Team}" ItemTemplate="{DynamicResource NameDataStyle}"/> </StackPanel> 185 WPF Performance considerations When data binding using an ItemsSource, use an ObservableCollection where possible supports CollectionChanged and PropertyChanged events Freeze objects when possible (read-only) if (myBrush.CanFreeze) { // Makes the brush unmodifiable. myBrush.Freeze(); } See “WPF Performance Whitepaper” for more tuning tips & advice. 186 See “What's New for Performance Profiling Tools for WPF” Demo courtesy of Jossef Goldberg, Program Manager for WPF Performance 188 189 VisualBrush BlurBitmapEffect 190 WClientLayout WClientMeasure WClientArrange WClientRenderHandler WClientAnimRenderHandler WClientMediaRender WClientUceNotifyPresent (computes the frame rate) WClientTimeManagerTick Event Tracing in Windows Presentation Foundation 191 192 Presentation Layer Business Logic Layer Database layer 193 x n x d Network Load Balancing Router Network Load Balancing m Web servers Web services DBMS servers (Presentation Layer) (Business Layer) (Data Layer) 194 m n mxnxd 195 Network Load Balancing (NLB) session-aware 196 mxnxd 197 Web Server W3wp.exe W3wp.exe Middleware Server svchost.exe svchost.exe User Kernel DatabaseServer http.sys Tcpip sqlservr.exe sqlservr.exe Each application component has separate and distinct CPU, Memory, Disk, and Network bandwidth requirements!!! 198 ServiceModelService Web Server W3wp.exe W3wp.exe Middleware Server svchost.exe svchost.exe User Kernel DatabaseServer http.sys Tcpip sqlservr.exe sqlservr.exe ASP.NET Apps .NET CLR Memory .NET CLR LocksAndThreads MSSQL$SQL Statistics 199 © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.