Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Perf by design

PDC 2008 Presentation by Rico Mariani and Vance Morrison

  • Be the first to comment

  • Be the first to like this

Perf by design

  1. 1.  Rico Mariani Architect Microsoft Corporation
  2. 2.  
  3. 3.     
  4. 4.   
  5. 5.    
  6. 6.      
  7. 7.         
  8. 8.        
  9. 9.    
  10. 10.     
  11. 11.    
  12. 12.     
  13. 13.     
  14. 14.  
  15. 15.        
  16. 16.       
  17. 17.  
  18. 18.    
  19. 19.       
  20. 20.   http://blogs.msdn.com/ricom   http://msdn.microsoft.com/en-us/library/aa338212.aspx   http://blogs.msdn.com/maoni/   http://blogs.msdn.com/tess/
  21. 21. © 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.
  22. 22. Vance Morrison Performance Architect Microsoft Corporation
  23. 23.        
  24. 24.         
  25. 25.    Stopwatch sw = Stopwatch.StartNew(); // Something being measured sw.Stop(); Console.WriteLine("Time = {0:f3} MSec", sw.Elapsed.TotalMilliseconds);      
  26. 26. 1) Go to Control panel’s Power Options 2) Set to High Performance •If you don’t do this, CPU is typically throttled to save power •Throttling causes less consistent measurements
  27. 27.  Vance Morrison
  28. 28.             
  29. 29.         
  30. 30.  Vance Morrison
  31. 31.           
  32. 32.         
  33. 33. Related Sessions Session Title Speaker Day Time Location PC53 Lunch: Building High Performance JScript Apps Sameer Chabungbam Mon 12:45-1:30PM 515B Microsoft Visual Studio: Bringing out the Best in Multicore Systems 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 HOL Code Title TLHOL11 VSTS 2010: Diagnostics and Performance Related Labs
  34. 34. © 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.
  35. 35. Vance Morrison Performance Architect Microsoft Corporation
  36. 36.         
  37. 37.  L1 Cache L2 Cache Memory Disk Size 64K 512K 2000M 100,000M Access Time .4ns 4ns 40ns 4,000,000ns         
  38. 38.           
  39. 39.            
  40. 40.   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 Large > 100 Meg WS Small < 5 Meg Private Med ~ 20 Meg Private Large > 50 Meg Private
  41. 41.  
  42. 42. Adding New Counters
  43. 43. 1 Open .NET CLR Memory 2 Select Counters 4 Add 5 OK 3 Select Process
  44. 44. Set Display to Report GC Heap Size 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 % Time in GC
  45. 45.  1 Total WS 2 Breakdown 3 DLL Breakdown GC Heap Here Only These Affect Cold Startup
  46. 46.            In Either Case when Working Set Large (> 10Meg)  Throughput is lost due to cache misses  Server workloads are typically cache limited
  47. 47.          Thus it Pays to Think about Memory Early!
  48. 48.                     
  49. 49.    
  50. 50.                 
  51. 51.        Finding Anomalous Memory use (AKA Leaks) XmlView Demo
  52. 52.            
  53. 53. Related Sessions Session Title Speaker Day Time Location PC53 Lunch: Building High Performance JScript Apps Sameer Chabungbam Mon 12:45-1:30PM 515B Microsoft Visual Studio: Bringing out the Best in Multicore Systems 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 HOL Code Title TLHOL11 VSTS 2010: Diagnostics and Performance Related Labs
  54. 54. © 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.
  55. 55. Vance Morrison Performance Architect Microsoft Corporation
  56. 56.           6 0
  57. 57. For all the same Reasons it is for People           
  58. 58.     
  59. 59.        
  60. 60.   6 4 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(); You can capture variables Anonymous Delegate
  61. 61.           
  62. 62. 6 6 0 5 10 15 20 25 30 35 40 45 50 0 20 40 60 80 100 120 140 160 Concurrency Level Throughput Work items with 10% CPU on a dual core system using ThreadPool.QueueUserWorkItem(). Reasons for perf degradation 1. Context switches 2. Memory contention 3. Hot Locks 4. Disk Contention 5. Network Contention
  63. 63.         
  64. 64. 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); Tasks now have handles Exceptions in tasks propagated on wait You can cancel work You can wait on the handle to synchronize
  65. 65. 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); 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 In this case the right value was done, so no wait was needed
  66. 66. 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); }
  67. 67.  Enable LINQ developers to leverage parallel hardware        Augments LINQ-to-Objects, doesn’t replace it   
  68. 68.         
  69. 69.          
  70. 70. 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 Parallel Symposium: Addressing the Hard Problems with Concurrency David Callahan Thurs 8:30-10:00AM 515A Parallel Symposium: Future of Parallel Computing Dave Detlefs Thurs 12:00-1:30PM 515A
  71. 71. © 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.
  72. 72.  Mark Friedman Architect Developer Division Microsoft Corporation
  73. 73.           78
  74. 74.  Dynamic    <A HREF="resumepage.html">my resume</A>      <LINK REL=STYLESHEET HREF="mystyles.css" TYPE="text/css"> 79
  75. 75.          connectionless sessionless   80
  76. 76.   Postback  HttpContext   ViewState  Session State   81
  77. 77.       82
  78. 78.   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 83
  79. 79.   End-to-End  Multiple tiers 84
  80. 80.  network latency Network Latency Client-side script Server-side .aspx Network Latency Client-side script Server-side .aspx Network Latency Client-side script Server-side .aspx Unit Test Load Test e.g., VS TeamTest Production 85
  81. 81.  Page Load Time           Ready 86
  82. 82. 87
  83. 83. 88
  84. 84.  VRTA 90
  85. 85.              91
  86. 86.          Semi-dynamic 92
  87. 87.  may be       93
  88. 88.           effective  Caching Architecture Guide for .NET Framework Applications 94
  89. 89. w3wp.exe Common Language Runtime (CLR) JIT compiler Garbage Collection threads mscoree.dll mscorsvr.dll MyApp.dll 95
  90. 90.  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 96
  91. 91. 97 IIS Architecture User Kernel HTTP TCP IP Network Interface HTTP Kernel Mode Driver (http.sys) HTTP Response Cache (Physical Memory) LSSAS Windows Authentication SSL Inetinfo IIS Administration FTP Metabase SMTP NNTP SVCHOST W3SVC W3wp W3wp Mscoree.dll http <code-behind>.dll Default.aspx Application Pool W3wp W3wp Mscoree.dll <code-behind>.dll Default.aspx Application Pool WAS Cache net.tcp net.tcp http HTTP Request
  92. 92. 98  http.sys  Response object cache    Web Service CacheKernel: Current URIs Cached, etc. See “IIS 7.0 Output Caching” at http://learn.iis.net/page.aspx/154/iis-7-output-caching/
  93. 93. 99  w3wp.exe   
  94. 94. 100  w3wp.exe  From a command line: windowssystem32inetsrvappcmd list WP
  95. 95. 102
  96. 96. 103
  97. 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 104
  98. 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 105
  99. 99.       <modules>     106
  100. 100. RequestNotification  Mark Friedman Architect Developer Division
  101. 101. 108 
  102. 102. 109 
  103. 103. 110  Level = 5 (Diagnostic level)
  104. 104. 111   tracerpt tracerpt filename.etl –of CSV –o mytrace.csv  logparser logparser "select * from filename.etl" -e 20 -o:DATAGRID -rtp 30 -compactModeSep "|"  xperf
  105. 105. 112
  106. 106. 113
  107. 107. 114   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-8a6f- 4aa86bacac3f.mspx?mfr=true
  108. 108. 115      HttpContext   Request  Response  Session  Cache
  109. 109. 116   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. 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 117
  111. 111. 118  Session State  Cache   e.g.,  Presentation Layer  Business Logic Layer  Data Layer
  112. 112. 119    
  113. 113. 120          HttpApplicationState  
  114. 114. 121   Application HttpApplicationState  Page   Page.Cache  Session         
  115. 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 122
  116. 116.  ViewState       123
  117. 117. 125  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.
  118. 118.   126
  119. 119. 127  Page.Application  Application.Lock(); Application["PageRequestCount"] = ((int)Application["PageRequestCount"])+1; Application.UnLock();
  120. 120. 128 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
  121. 121. 129  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
  122. 122. 130 Session State options  InProc  StateServer     SQLServer   
  123. 123. 131
  124. 124. 132 Session State options  InstallSqlState.sql InstallPersistSqlState.sql  sessionState
  125. 125.   Frequency of re-use  Cost to re-generate  Size  Max(Cache Hit %) Min(Space x Time) 133
  126. 126.      diminishing returns 134 0 25 50 75 100 Cache Size Cache Hit %
  127. 127.    IIS kernel mode Cache  ASP.NET Application cache  ASP.NET Page cache       135
  128. 128.       effective  Note: difficult complex  136
  129. 129. 137   HttpContext.Cache  Page.Cache    @ OutputCache  VaryByParam    <PartialCaching>   Substitution.MethodName  HttpResponse.WriteSubstitution 
  130. 130. 138     frequency of use x object size      Underused 
  131. 131.  139
  132. 132. 140  Cache Dictionary  IEnumerable    
  133. 133. 141   Add     Insert  Get  Remove
  134. 134. 142         AggregateCacheDependency  SqlCacheDependency   http://msdn.microsoft.com/en- us/library/system.web.caching.sqlcachedependency.a spx
  135. 135. 143  effectiveness  Cache API Entries  Cache API Hits  Cache API Misses  Cache API Hit Ratio  Cache API Turnover Rate
  136. 136. 144  public void RemovedCallback (String k, Object v, CacheItemRemovedReason r){ }  Examine the CacheItemRemovedReason      
  137. 137.  InstrumentedCacheValue // unfortunately, it is a sealed class; otherwise… // private Int64 _refcount; // private DateTime _lastreftimestamp; 145
  138. 138. 146   Count  EffectivePrivateBytesLimit   EffectivePercentagePhysicalMemoryLimit   disableMemoryCollection disableExpiration privateBytesLimit percentagePhysicalMemoryUsedLimit privateBytesPollTime="HH:MM:SS"
  139. 139. 147    @ OutputCache  Duration  VaryByParam  Location      
  140. 140. 148  HttpCachePolicy
  141. 141. 149   Response.Cache.SetCacheability HttpCacheability      
  142. 142. 150  effectiveness  Cache Entries  Cache Hits  Cache Misses  Cache Hit Ratio  Cache Turnover Rate
  143. 143. 151  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.
  144. 144.    that did not change  AJAX Javascript asynchronous HTTP requests   152
  145. 145.    ICallbackHandler      <asp:ScriptManager EnablePartialRendering="True“ />      153
  146. 146.   XMLHttpRequest xmlhttp.open('GET', url, true); xmlhttp.onreadystatechange = AsyncUpdateEvent; xmlhttp.send(null); 154
  147. 147.  UpdatePanel       Triggers  ChildrenAsTriggers  “ AJAX Application Architecture, Part 1 155
  148. 148.   Sys.Application.Init     Sys.Application.Load  UpdatePanel  156
  149. 149.  PageRequestManager     pageLoading  pageLoaded   endReques  157
  150. 150.    ICallbackEventHandler  RaiseCallbackEvent  GetCallbackResul       Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web Pages 158
  151. 151.  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   See Wenlong Dong’s blog for details   http://msdn.microsoft.com/en-us/library/cc907912.aspx 159
  152. 152.     WebHttpBinding   160
  153. 153.  Binding 161 Binding Response Time (msecs) Throughput wsHttpBinding 1300 1200 basicHttpBinding 1150 1800 netTcpBinding 400 5100 netNamedPipeBinding 280 7000 0 2000 4000 6000 8000 Throughput Source: Resnik, Crane, & Bowen, Essential Windows Communication Foundation, Addison Wesley, 2008. Chapter 4. See also, “A Performance Comparison of Windows Communication Foundation (WCF) with Existing Distributed Communication Technologies” Caution: your mileage will vary.
  154. 154.    [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> 
  155. 155.    in the order in which they are received  conversation  
  156. 156.     [ServiceBehavior( InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple )] class MySingleton : IMyContract, IDisposable
  157. 157.   ConcurrencyMode.Multiple  requires locking and synchronization of shared state data  ConcurrencyMode.Reentrant   serviceThrottling <behavior name="Throttled"> <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions=“30" maxConcurrentInstances=“20" />
  158. 158. performanceCounters  Mark Friedman Architect Developer Division
  159. 159.  <system.serviceModel> <diagnostics performanceCounters="All"></diagnostics> But <diagnostics performanceCounters=“ServiceOnly"></diagnostics> is recommended (less overhead) 167
  160. 160. 168
  161. 161. 169   “Using Service Trace Viewer for Viewing Correlated Traces and Troubleshooting”  Essential Windows Communication Foundation
  162. 162. 170
  163. 163. 171
  164. 164.  new "http://localhost:8000/HelloService" string "http://localhost:8000/HelloService/MyService" using new new "Press <enter> to terminate service"  172
  165. 165.         Measure:     173
  166. 166.  Fiddler 174
  167. 167.        TCP Window Size    without     175
  168. 168. Best Practices for Improving Page Load Time
  169. 169. 177
  170. 170. 178
  171. 171.      179
  172. 172.  180
  173. 173.     (serialized)          181
  174. 174. 182
  175. 175. 183
  176. 176.            184
  177. 177.      Get Request   185

×