You are previewing ASP.NET Site Performance Secrets.
O'Reilly logo
ASP.NET Site Performance Secrets

Book Description

Simple and proven techniques to quickly speed up your ASP.NET website

  • Speed up your ASP.NET website by identifying performance bottlenecks that hold back your site's performance and fixing them

  • Tips and tricks for writing faster code and pinpointing those areas in the code that matter most, thus saving time and energy

  • Drastically reduce page load times

  • Configure and improve compression – the single most important way to improve your site's performance

  • Written in a simple problem-solving manner – with a practical hands-on approach and just the right amount of theory you need to make sense of it all

  • In Detail

    Do you think that only experts with a deep understanding of the inner workings of ASP.NET, SQL Server, and IIS can improve a website's performance? Think again – because this book tosses that notion out of the window. It will help you resolve every web developer's nightmare – a slow website – with angry managers looking over your shoulder, raging calls from advertisers and clients – the lot. You don't have the time or energy to gain a thorough and complete understanding of ASP.NET performance optimization – You just need your site to run faster! This book will show you how.

    This hands-on book shows how to dramatically improve the performance of your ASP.NET-based website straight away, without forcing you through a lot of theoretical learning. It teaches you practical, step-by-step techniques that you can use right away to make your site faster with just the right amount of theory you need to make sense of it all.

    Start reading today and you could have a faster website tomorrow.

    Unlike other performance-related books, here you'll first learn how to pinpoint the bottlenecks that hold back your site's performance, so you can initially focus your time and energy on those areas of your site where you can quickly make the biggest difference. It then shows you how to fix the bottlenecks you found with lots of working code samples and practical advice, and just the right amount of theoretical detail.

    The first chapter details techniques for diagnosing performance issues using Waterfall charts. Subsequent chapters then each focus on one individual aspect of your website, providing you with numerous real-life scenarios and performance-enhancing techniques for each of them. In the last chapter, you learn how to effectively load-test your environment in order to measure the change in performance of your site without having to update your production environment – whether it is a new release or simply a small change in the database.

    Table of Contents

    1. ASP.NET Site Performance Secrets
      1. ASP.NET Site Performance Secrets
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Errata
          2. Piracy
          3. Questions
      6. 1. High Level Diagnosis
        1. Assuring good performance
          1. Continuous monitoring
          2. Setting performance goals
          3. Iterative improvements
          4. Monitoring services
        2. High-level diagnosis with Waterfall charts
        3. Creating a Waterfall chart using Firebug
          1. Installing Firebug
          2. Creating the Waterfall chart
          3. Interpreting the Waterfall chart
            1. Request and response information
            2. Timeline information
            3. Page-level information
            4. Saving information
        4. Categorizing bottlenecks using Waterfall charts
          1. Scenario 1: Main .aspx file takes long to arrive
          2. Scenario 2: Main .aspx file takes long to load over the Internet
          3. Scenario 3: Images take long to load
          4. Scenario 4: JavaScript file blocks rendering
        5. Waterfall generators for other browsers
          1. Fiddler (browser-independent)
          2. Internet Explorer via WebPagetest
          3. Google Chrome
          4. Apple Safari
        6. More Waterfall chart generators
        7. Find out more
        8. Summary
      7. 2. Reducing Time to First Byte
        1. Pinpointing bottlenecks
          1. Memory
            1. Windows Server 2003
            2. Windows Vista, Windows Server 2008, or Windows 7
            3. Simulating a memory shortage
          2. Caching
          3. CPU
          4. Thread usage
          5. Long wait times
        2. Additional measures
          1. Deployment
            1. Building projects in release mode
            2. Publishing your website
            3. Disabling debug mode
            4. Reducing number of assemblies
          2. Reducing round trips
            1. Using Server.Transfer instead of Response.Redirect
            2. Always specifying the default file in URLs
            3. Permanent redirects
            4. Minimizing CNAME records
          3. SSL
          4. Unwanted requests
            1. Search engine bots
            2. Hotlinking
            3. CAPTCHA
            4. Scrapers
            5. Usability testing
        3. Find out more
        4. Summary
      8. 3. Memory
        1. Managed resources
          1. Life cycle
          2. Generations
          3. Large Object Heap
          4. Counters
          5. CLR profiler
          6. Garbage collector versions
          7. Acquire late
            1. Release early
            2. Using StringBuilder to concatenate strings
              1. Using StringBuilder
                1. When not to use StringBuilder
                2. StringBuilder capacity
              2. Using Compare for case-insensitive compares
              3. Using Response.Write buffer
              4. Pooling objects over 85 KB
        2. Unmanaged resources
          1. IDisposable
          2. Counters
        3. Sessions
          1. Reducing session state life time
            1. Reducing space taken by session state
            2. Using another session mode
            3. Stop using session state
        4. Find out more
        5. Summary
      9. 4. CPU
        1. Identifying bottlenecks
        2. Tools
        3. Data access
          1. Connection pooling
          2. DataSet versus List
          3. Returning multiple result sets
          4. Sending multiple inserts in one go
          5. Using native data providers
        4. Exceptions
          1. Revealing the time taken by exceptions
          2. Counters
        5. DataBinder.Eval
        6. Garbage collector
        7. Threading
        8. StringBuilder
        9. Regex instantiation
        10. UtcNow
        11. Foreach
        12. Virtual properties
        13. Avoid unnecessary processing
        14. Trimming HTTP pipeline
        15. Find out more
        16. Summary
      10. 5. Caching
        1. Browser caching
          1. OutputCache directive
          2. Enabling caching in code
          3. Disabling caching in code
        2. Proxy caching
          1. Caching different versions of the same page
          2. Cookies
          3. Removing query string from URL
            1. URLRewrite extension to IIS 7
            2. RewritePath method in Global.asax
            3. Resetting the form action attribute
        3. Output caching
          1. What to cache and what not to cache
          2. Enabling output caching
          3. Output cache example
          4. VaryByParam
          5. VaryByHeader
          6. VaryByCustom
          7. VaryByCustom by browser
          8. Fragment caching
          9. Post-cache substitution
          10. Output cache provider
            1. Creating an output cache provider
              1. Set
              2. Get
              3. Add
              4. Remove
            2. Using an output cache provider
        4. Kernel caching and IIS 7 output caching
          1. Configuring IIS caching
          2. Limitations of kernel caching
          3. Checking the contents of the kernel cache
        5. Data caching
          1. Basic use
          2. Expiry
          3. Priority
          4. File dependencies
          5. Database dependencies
            1. Restrictions on queries
            2. Starting the Service Broker
            3. Starting the listening service
            4. Creating the dependency
          6. Item removed callback
            1. Things to keep in mind
          7. Optimal use of server cache
        6. Find out more
        7. Summary
      11. 6. Thread Usage
        1. Asynchronous web service access
          1. Synchronous version
          2. Asynchronous version
        2. Asynchronous data access layer
          1. Usage
          2. Implementation
          3. Performance testing
        3. Asynchronous generic handlers
          1. Synchronous version
          2. Asynchronous version
          3. Implementation
          4. Performance testing
        4. Asynchronous file writes
          1. Synchronous version
          2. Asynchronous version
          3. A word of caution
        5. Asynchronous web requests
        6. Configuration changes
          1. IIS 6, IIS 7 Classic Mode
            1. I/O-related configuration
              1. ASP.NET 2.0
            2. IIS 7 integrated mode
            3. Maximum queue size
        7. Setting timeouts aggressively
        8. Find out more
        9. Summary
      12. 7. Reducing Long Wait Times
        1. Measuring wait times
          1. Creating custom counters
            1. Creating counters with Visual Studio
            2. Creating counters programmatically
          2. Updating counters in your code
          3. Viewing custom counters in perfmon
        2. Waiting concurrently
        3. Retrieving multiple result sets from the database
        4. Reducing overhead by using off-box session modes
          1. Reducing trips to the database
          2. Setting EnableSessionState
          3. Reducing serialization and transfer overhead
          4. Cutting your dependence on sessions
        5. Thread locking
          1. Minimizing the duration of locks
          2. Using granular locks
          3. Using System.Threading.Interlocked
          4. Using ReaderWriterLock
            1. Acquiring a reader lock
            2. Acquiring a writer lock
            3. Alternating readers and writers
        6. Optimizing disk writes
          1. Avoiding head seeks
          2. Using FileStream.SetLength to avoid fragmentation
          3. Using 64 K buffers
          4. Disabling 8.3 filenames
        7. Find out more
        8. Summary
      13. 8. Speeding up Database Access
        1. Pinpointing bottlenecks
          1. Missing indexes and expensive queries
            1. Missing indexes
            2. Expensive queries
            3. Unused indexes
          2. Locking
          3. Execution plan reuse
            1. Performance counters
            2. dm_exec_query_optimizer_info
            3. sys.dm_exec_cached_plans
          4. Fragmentation
          5. Memory
          6. Disk usage
          7. CPU
        2. Fixing bottlenecks
          1. Missing indexes
            1. Clustered index
            2. Non-clustered index
            3. Included columns
            4. Selecting columns to give an index
              1. When to use an index
              2. When not to use an index
                1. Column updated often
                2. Low specificity
              3. When to use a clustered index
            5. Maintaining indexes
          2. Expensive queries
            1. Cache aggregation queries
            2. Keeping records short
            3. Considering denormalization
            4. Being careful with triggers
            5. Using table variables for small temporary result sets
            6. Using full-text search instead of LIKE
            7. Replacing cursors with set-based code
              1. Minimizing traffic from SQL server to web server
              2. Object naming
              3. Using SET NOCOUNT ON
              4. Using FILESTREAM for values over 1 MB
              5. Avoiding functions on columns in WHERE clauses
              6. Using UNION ALL instead of UNION
              7. Using EXISTS instead of COUNT to find existence of records
              8. Combining SELECT and UPDATE
            8. Locking
              1. Gathering detailed locking information
              2. Reducing blocking
              3. Reducing deadlocks
            9. Execution plan reuse
              1. Ad hoc queries
              2. Simple parameterization
              3. sp_executesql
              4. Stored procedures
              5. Preventing reuse
            10. Fragmentation
              1. Index rebuild
              2. Index reorganize
              3. Heap table defragmentation
            11. Memory
            12. Disk usage
              1. Optimizing query processing
              2. Moving the logfile to a dedicated physical disk
              3. Reducing fragmentation of the NTFS filesystem
              4. Considering moving the tempdb database to its own disk
              5. Splitting the database data over two or more disks
              6. Moving heavily-used database objects to another disk
              7. Using the optimal RAID configuration
            13. CPU
        3. Find out more
        4. Summary
      14. 9. Reducing Time to Last Byte
        1. Pinpointing bottlenecks
          1. Compression
          2. ViewState
            1. What is ViewState?
            2. Why reduce ViewState?
            3. Measuring your ViewState
              1. Quickly finding where ViewState is an issue
              2. Measuring compressed ViewState
              3. Having ViewState size on status bar
          3. Optimizing forms
          4. White space
        2. Fixing bottlenecks
          1. ViewState
            1. Seeing ViewState generated by each control
            2. Disabling Viewstate
            3. Identifying controls that do not need ViewState
            4. Reloading from database cache
            5. Storing a shorter version of the property value
            6. Storing ViewState on the server
            7. Compressing Viewstate
              1. Implementing compression
              2. Implementing decompression
              3. Using ViewState compression on a page
          2. Reducing white space
            1. Creating the filter
            2. Creating the HTTP Module
            3. Adding the HTTP Module to web.config
        3. Additional measures
          1. Event validation
          2. Inline JavaScript and CSS
          3. Avoiding inline styling
          4. Reducing space taken by ASP.NET IDs
            1. Opting out of IDs
            2. Keeping IDs short
          5. Using ASP.NET comments instead of HTML comments
          6. Using Literal control instead of Label control
          7. Avoiding repetition
            1. Using shorter URLs
        4. Find out more
        5. Summary
      15. 10. Compression
        1. Agreeing on compression
        2. Configuring compression in IIS 7
          1. Installing the dynamic content compression module
          2. Enabling compression
          3. Setting compression by site, folder, or file
          4. Compression level
          5. Disabling compression based on CPU usage
          6. Setting the request frequency threshold for static compression
          7. Caching compressed dynamic files
            1. Storing compressed files in the output cache
            2. What if a client doesn't accept compressed content?
        3. Configuring compression in IIS 6
          1. Switching on compression in the IIS Manager
          2. Setting permissions on the folder where compressed static files are cached
        4. Updating the metabase
          1. Resetting the IIS server
        5. Configuring compression in IIS 5
        6. Using compression in your development environment
          1. Installing IIS 7
          2. Enabling compression
          3. Creating a development site in IIS
          4. Modifying your project so it uses the development site
        7. Measuring the difference compression makes
        8. Improving the compressibility of your pages
        9. Is compression the magic bullet?
        10. Find out more
        11. Summary
      16. 11. Optimizing Forms
        1. Client-side validation
          1. ASP.NET validation controls
            1. Quick example
            2. Available validator controls
            3. Validation summary
            4. Disabling validation
            5. Overhead
          2. Validate JavaScript library
            1. Initialization
            2. Built-in validation methods
            3. Adding validation methods
            4. Formatting error messages
            5. Content Delivery Network (CDN)
            6. Overhead
        2. Submitting forms asynchronously
          1. UpdatePanel control
          2. Page Methods
          3. Web service
          4. Generic handler
            1. Building the generic handler
            2. Cross Site Request Forgery (CSRF) attacks
            3. Calling the generic handler
          5. WCF Data Services and the Entity Framework
            1. Creating the entity data model
            2. Creating the WCF Data Service
            3. Calling the WCF Data Service from JavaScript
            4. Bugs and limitations in WCF Data Services
              1. Allowing the precompiled site to be updatable when publishing your site
              2. Providing database access to [NT AUTHORITY\NETWORK SERVICE]
              3. Disabling batch compilation
          6. Creating an activity indicator
        3. AJAX-type grids
          1. Free, lightweight grids
          2. Paid, heavyweight grids
          3. Building your own grid
          4. Working sample code
        4. Find out more
        5. Summary
      17. 12. Reducing Image Load Times
        1. Caching
          1. Cache-Control response header
          2. Preventing conditional requests
          3. Expires header
          4. Configuring Cache-Control in IIS 7
          5. Configuring Cache-Control in IIS 6
          6. Giving identical URLs to identical images
        2. Serving images from cookieless subdomains
        3. Parallel loading
        4. Image control adapter
        5. Image size
          1. Using the optimal image format
          2. Why to avoid scaling images in HTML
          3. Tools
            1. PNGOUT
            2. Pngcrush
            3. Jpegtran
            4. NConvert
            5. ImageMagick
        6. Combining images
        7. Ways to avoid images
          1. Rounded corners in CSS
          2. Utility symbols
        8. Shortcut icon
        9. Content Delivery Network
        10. Find out more
        11. Summary
      18. 13. Improving JavaScript Loading
        1. Problem: JavaScript loading blocks page rendering
          1. Confirming with a test site
          2. Approaches to reduce the impact on load times
        2. Approach 1: Start loading after other components
        3. Approach 2: Loading JavaScript more quickly
          1. Techniques used with images
          2. Free Content Delivery Networks
          3. GZIP compression
          4. Minifying a JavaScript file
            1. Tools
            2. Impact of minification
            3. Implementing minification
              1. HTTP handler
              2. Configuring the handler in web.config
              3. Enabling GZIP compression for dynamic files
          5. Combining or breaking up
            1. When and why to combine
            2. When and why to break up
            3. Measuring each scenario
            4. Preventing 304 messages
            5. Implementing automatic file combining
              1. ASP.NET ScriptManager Control
              2. Compression (deflate) and HTML, CSS, JS Minification in ASP.NET
              3. Combres 2.0
              4. FileCombine
          6. Removing unused code
        4. Approach 3: Loading JavaScript on demand
          1. Separating user interface code from render code
          2. OnDemandLoader library
          3. Initializing OnDemandLoader
          4. Invoking not-yet-loaded functions
          5. Preloading
        5. Approach 4: Loading Javascript without blocking
          1. Moving all <script> tags to the end of the page
          2. Separating user interface code and render code
          3. Introducing page loading indicator
          4. Loading code in parallel with page
            1. Initializing the loader object
            2. Loading the code while the page is loading
            3. Ensuring that code runs after the page is rendered
        6. Improving ad loading
        7. Improving CSS Loading
          1. Minifying CSS
          2. Removing unused CSS selectors
          3. Loading CSS without blocking rendering
        8. Find out more
        9. Summary
      19. 14. Load Testing
        1. Using a load test environment
          1. Load testing individual components during development
          2. Testing page-by-page
          3. Testing performance improvements
          4. Acceptance testing
          5. Additional best practices
        2. Building a load test environment
        3. Hosting the website under load
          1. Installing IIS on Vista or Windows 7
            1. Limitations
            2. Installation
            3. Opening the Firewall
            4. Installing Windows Server 2008
            5. Running Windows Server 2008 alongside Vista or Windows 7
            6. Installing Virtual PC 2007
            7. Installing Windows Server 2008
            8. Getting more out of Virtual PC
              1. Keystrokes
              2. Screen size
              3. Installing Virtual Machine Additions
            9. Creating a shared folder
            10. Installing .NET 4 or .NET 3.5 Service Pack 1
            11. Installing and configuring IIS 7 on Windows Server 2008
            12. Opening the Firewall
          2. Installing Windows Server 2003
            1. Running Windows Server 2003 alongside Vista or Windows 7
            2. Installing Windows Server 2003
            3. Creating a shared folder
            4. Installing .NET 4 or .NET 3.5 Service Pack 1
            5. Installing and configuring IIS 6 on Windows Server 2003
            6. Opening the Firewall
          3. ASP.NET test site
        4. Installing a database
          1. Downloading
          2. Installation
          3. Opening port in Firewall
          4. Enabling remote administration
          5. Test page
        5. Setting up a load generator
          1. Load generation services
          2. WCAT
            1. Installing WCAT
            2. Creating a simple test
              1. Settings and scenario file
              2. Scenario attributes
              3. Default element
              4. Transaction
            3. Running a simple test
            4. Recording scenario files using fiddler
            5. Adding thinking time
            6. Other free load generation tools
          3. Visual Studio Team System 2008 Test Edition/Visual Studio 2010 Ultimate
            1. Setting up a demo site
            2. Creating a simple web test
            3. Creating a second web test
            4. Coded web tests
            5. Combining web tests into a load test
            6. Running the load test
            7. Load testing components
          4. Making sure the load generator isn’t bottlenecking
        6. Find out more
        7. Summary