Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Dev411

    1. 1. DEV411 ASP.NET: Best Practices For Performance Stephen Walther
    2. 2. Purpose of Talk <ul><li>What is the fastest method of displaying a set of database records in an ASP.NET Page? </li></ul>
    3. 3. Testing Tools <ul><li>Trace Tools </li></ul><ul><li>Profiler Tools </li></ul><ul><li>Load Tools </li></ul>
    4. 4. Trace Tools <ul><li>ASP.NET Page or Application Tracing </li></ul><ul><li>Display trace information on page </li></ul><ul><li>System.Diagnostics Tracing </li></ul><ul><li>Write trace information to custom listener </li></ul>
    5. 5. Trace Tools
    6. 6. Profiler Tools <ul><li>CLR Profiler </li></ul><ul><li>Free Profiler from Microsoft </li></ul><ul><li>ANTS Profiler </li></ul><ul><li>Available at </li></ul><ul><li>SQL Profiler </li></ul><ul><li>Included with Microsoft SQL Server </li></ul>
    7. 7. ANTS Profiler
    8. 8. Load Tools <ul><li>Application Center Test </li></ul><ul><li>Included with Visual Studio .NET Enterprise </li></ul><ul><li>Web Application Stress Tool (WAST) </li></ul><ul><li>Free download from Microsoft site </li></ul><ul><li>ANTS Load </li></ul><ul><li>Available at Includes calculated Frustration Coefficient </li></ul>
    9. 9. Test Setup <ul><li>Application Center Test (ACT) </li></ul><ul><li>.NET Framework 1.1 </li></ul><ul><li>SQL Server 2000 </li></ul><ul><li>Windows Server 2003 (Standard) </li></ul>
    10. 10. Performance Statistics <ul><li>Requests Per Second (RPS) </li></ul><ul><li>Time To Last Byte (TTLB) </li></ul><ul><li>Page Execution Time </li></ul>
    11. 11. Timer Module <ul><li>The Timer Module records the time interval between </li></ul><ul><ul><li>PreRequestHandlerExecute Event </li></ul></ul><ul><ul><li>PostRequestHandlerExecute Event </li></ul></ul>
    12. 12. Timer Module PostRequestEventHandlerExecute EndRequest Load Init Unload TimerModule.cs PreRequestEventHandlerExecute BeginRequest Application Events Page Events
    13. 13. Clock Resolution <ul><li>QueryPerformanceCounter is accurate to 1/3579545 of a second or about a millionth of a second </li></ul>
    14. 14. The Test <ul><li>Request page 1050 times </li></ul><ul><li>Discard first 50 requests </li></ul><ul><li>Log time of each request </li></ul><ul><li>Average results </li></ul>
    15. 15. Database Setup <ul><li>Four Database Tables </li></ul><ul><li>Products10 – 10 Rows </li></ul><ul><li>Products50 – 50 Rows </li></ul><ul><li>Products100 – 100 Rows </li></ul><ul><li>Products500 – 500 Rows </li></ul>
    16. 16. What’s Faster? <ul><li>DataReader </li></ul><ul><li>DataSet </li></ul>DisplayDataReader.aspx DisplayDataSet.aspx
    17. 17. DataReader
    18. 18. DataSet
    19. 19. DataReader Versus DataSet
    20. 20. DataReader Versus DataSet Final Results <ul><li>On average, a DataReader is 16% faster than DataSet </li></ul>
    21. 21. 3rd Option – ArrayList <ul><li>Using an ArrayList instead of a DataReader results in similar performance with the advantages of a static representation of data </li></ul>DisplayArrayList.aspx
    22. 22. ArrayList
    23. 23. What’s Faster? <ul><li>SqlDataReader </li></ul><ul><li>OleDbDataReader </li></ul>
    24. 24. OleDbDataReader
    25. 25. OleDbDataReader Final Results <ul><li>On average, a SqlDataReader is 115% faster than an OleDbDataReader </li></ul>
    26. 26. What’s Faster? <ul><li>Inline SQL </li></ul><ul><li>Stored Procedure </li></ul>
    27. 27. Stored Procedure
    28. 28. What’s Faster? <ul><li>DataReader Column Reference </li></ul><ul><li>By Name: </li></ul><ul><li>Response.Write(dr[“ProductName”]); </li></ul><ul><li>By Ordinal: </li></ul><ul><li>Response.Write(dr[0]); </li></ul><ul><li>By GetString(): </li></ul><ul><li>Response.Write(dr.GetString(0)); </li></ul>
    29. 29. Column Reference
    30. 30. Column Reference Final Results <ul><li>On average, ordinal reference is 11% faster than by name </li></ul>
    31. 31. What’s Faster? <ul><li>Proper Case </li></ul><ul><li>dr[“ProductName”] </li></ul><ul><li>Improper Case </li></ul><ul><li>dr[“PRODUCTNAME”] </li></ul>
    32. 32. Proper Case
    33. 33. Proper Case Final Results <ul><li>Using proper column case is 1% faster than improper column case </li></ul>
    34. 34. What’s Faster? <ul><li>Inline </li></ul><ul><li>ASP.NET Controls </li></ul>
    35. 35. DataGrid
    36. 36. DataGrid Final Results <ul><li>Inline script is 233% faster than a DataGrid </li></ul>
    37. 37. What’s Faster? <ul><li>DataGrid with ViewState Disabled </li></ul><ul><li>DataGrid with ViewState Enabled </li></ul>
    38. 38. ViewState
    39. 39. ViewState Final Results <ul><li>DataGrid with ViewState disabled is 66% faster than DataGrid with ViewState enabled </li></ul>
    40. 40. What’s Faster? <ul><li>AutoGenerateColumns </li></ul><ul><li>Template Columns </li></ul>
    41. 41. Template Columns
    42. 42. Template Columns Final Results <ul><li>A DataGrid without templates is 39% faster than a DataGrid with templates </li></ul>
    43. 43. What’s Faster? <ul><li>How to improve template performance? </li></ul><ul><li>DataBinder.Eval </li></ul><ul><li><%# DataBinder.Eval(Container.DataItem, “ProductName”) %> </li></ul><ul><li>Explicit Cast </li></ul><ul><li><%# ((DbDataRecord)Container.DataItem)[&quot;ProductName&quot;]%> </li></ul><ul><li>ItemDataBound </li></ul><ul><li>void ItemDataBound(Object s, DataGridItemEventArgs e) </li></ul>DisplayItemDataBound.aspx
    44. 44. Template Performance
    45. 45. Template Performance Final Results <ul><li>Explicit cast is 11% faster than using a databinding expression </li></ul>
    46. 46. Creating A Custom Control <ul><li>Would a custom DataGrid (with severely reduced functionality) be faster than the standard DataGrid? </li></ul>FastGrid.cs
    47. 47. Custom Control
    48. 48. Custom Control Final Results <ul><li>FastGrid is 37% faster than a standard DataGrid </li></ul>
    49. 49. What’s Faster? <ul><li>DataGrid with no caching </li></ul><ul><li>DataGrid with data caching </li></ul><ul><li>DataGrid with output caching </li></ul>
    50. 50. Data Caching
    51. 51. Data Cache Final Results <ul><li>Using the data cache is 637% faster than a standard DataGrid </li></ul>
    52. 52. Output Cache
    53. 53. Output Cache Final Results <ul><li>Using the output cache is infinitely faster than using a standard DataGrid </li></ul>
    54. 54. Conclusions <ul><li>A DataReader is faster than a DataSet </li></ul><ul><li>An inline DataReader is faster than a DataGrid </li></ul><ul><li>You pay a high price for ViewState </li></ul><ul><li>AutoGenerateColumns is faster than template columns </li></ul><ul><li>Caching is always a good idea! </li></ul>
    55. 55. Performance Resources <ul><li>Microsoft Sites </li></ul><ul><ul><li>Performance </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>Books </li></ul><ul><ul><li>Improving .NET Application Performance and Scalability </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>Software </li></ul><ul><ul><li>Web Application Stress Tool </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li>CLR Profiler </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><li>ANTS Profiler and ANTS Load </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul>
    56. 56. Q1: Overall satisfaction with the session Q2: Usefulness of the information Q3: Presenter’s knowledge of the subject Q4: Presenter’s presentation skills Q5: Effectiveness of the presentation Please fill out a session evaluation on CommNet