ASP.NET Performance

Download Report

Transcript ASP.NET Performance

ASP.NET Performance
Chris Hay
Director, Roskakori Limited
2
About Me
Roskakori Limited
Coordinator for Cambridge Region of NxtGenUg
– .NET User Group in Cambridge
– www.nxtgenug.net
Blog aggregated on http://silverlight.net/community
3
Blog: http://silverlightuk.blogspot.com
Email: chrishayuk (at) googlemail.com
Agenda
Fiddler and Firebug
Reduce Page Requests
Reduce Page Size
AJAX
Bring Pages Closer to Users
Make backend more efficient
4
Tools
Firebug, Fiddle, Page Detailer
Fiddler
– IE & Firefox, Difficult to monitor local requests
Firebug
– Firefox Only, Monitors local requests
IBM Page Detailer
– Good for graphs
Used to monitor
– Requests, Request Sizes, Request Data
5
Fiddler, Firebug, Page Detailer
Chris Hay
Director
Roskakori Limited
6
Reduce Requests
The Big Culprit
HTTP requests are the biggest web performance killer
Reduce Requests, massively improve performance
7
Reduce Requests
Single JS and Single CSS Files
Combine all Javascript into one file
Combine all CSS into one file
8
Combine JS and CSS files
Chris Hay
Director
Roskakori Limited
9
Reduce Requests
Image Maps
Reduce Image Requests
Use Image Maps
Use Large Background Image
10
Multiple Images Vs ImageMaps
Chris Hay
Director
Roskakori Limited
11
Reduce Requests
IIS Caching
Stores Assets in Browser Cache
– .css, .js, images, static html
Saves Browser making future requests
Upgrades: Use Versioning Policy
– MyJs001.js
12
Reduce Requests
IIS Caching – IIS 6.0
Web Site / Application / File -> Right Click Properties
13
Reduce Requests
IIS Caching – IIS 7.0
Enable staticContent Override in applicationHost.config
Modify web.config
14
IIS Caching
Chris Hay
Director
Roskakori Limited
15
Reduce Requests
Home Page Performance
Home page is most important page
– Needs to be as fast as possible
Use Inline Scripts and CSS
– Only on home page
Can load future assets post UI
– UI Displays First
– Assets (CSS, JS) loaded post page display in background
– Cached for future page requests
16
Home Page Performance
Chris Hay
Director
Roskakori Limited
17
Reduce Requests
Avoid Redirects
Avoid Response.Redirect
– Invokes an extra client side HTTP Request
Use Server.Transfer instead
18
Avoid Redirects
Chris Hay
Director
Roskakori Limited
19
Reduce Page Size
The second biggest culprit
The smaller the page, the quicker the download
Especially important in these areas
–
–
–
–
20
Mobile Applications (Windows Mobile, IPhone, 3G Data Card)
Non Broadband Users
Many offices have less capacity than broadband
Developing Countries
Reduce Page Size
External Javascript + CSS
Avoid inline styles (except home page)
Avoid inline scripts (except home page)
External scripts mean smaller page sizes
– Especially shared styles + scripts
– Especially pages accessed multiple times
– Not necessarily more performant on the home page
21
Reduce Page Size
Use Silverlight in your Web Page
Single File Download (.xap file)
Resides in Browser Cache
Available to all pages in website
Reduces Page Size for Common Items
– Top Banner
– Menu Navigation
– Bottom Banner
22
Reduce Page Size - Silverlight
Chris Hay
Director
Roskakori Limited
23
Reduce Page Size
HTTP Compression IIS
Most Browsers support HTTP Compression
– GZIP & Deflate
– IE, Firefox etc
Drastically reduces page size
Steps
– Browser Passes Accept-Encoding in Request Header
– Data is compressed and sent to browser
– Browser decompresses html
24
Only GET is compressed, POST IS NOT Compressed
Reduce Page Size
HTTP Compression IIS 6.0
IIS -> Enable Compression
– Click Properties on Website, Select Services Tab
– Enable Compress Static & Application Files Checkboxes
IIS -> Web Service Extensions
– Add new Web Service Extension called HTTP Compression
– Set to <windowsdirectory>\system32\inetsrv\gzip.dll
– Check extension status to allowed
Edit Metabase.Xml
– For Deflate & Gzip, Set HcDynamicCompressionLevel=“9”
25
Reduce Page Size
HTTP Compression IIS 7.0
Control Panel -> Turn On Windows Features
– Ensure Static & Dynamic Compression are checked
26
Reduce Page Size
HTTP Compression IIS 7.0
applicationhost.config
– C:\Windows\System32\inetsrv\config\applicationhost.config
– Ensure doDynamicCompression=“true”
– Unless you are using page output caching
– Ensure doStaticCompression=“true”
27
HTTP Compression
Chris Hay
Director
Roskakori Limited
28
Reduce Page Size
Reducing Viewstate
Enable Viewstate only on those pages that need it
Compress Viewstate
Offload ViewState to the session
29
Reduce Page Size
Compress ViewState
Uses GZIP to compress viewstate
Page Load: No advantage over HTTP Compression
Good for PostBack
– HTTP Compression only works on GET
Override Page State Methods on Page
– LoadPageStateFromPersistenceMedium
– SavePageStateToPersistenceMedium
30
Reduce Page Size
Offload ViewState to session
Uses GZIP to compress viewstate
Store ViewState in the session
Override Page State Methods on Page
– LoadPageStateFromPersistenceMedium
– SavePageStateToPersistenceMedium
31
Reducing Viewstate
Chris Hay
Director
Roskakori Limited
32
AJAX
Update Panel
Update Panel
Calling Web Services
Script Combining
33
AJAX
Update Panel
Only refreshes the html within the update panel
Reduces Html Downloaded from postback
34
AJAX
Update Panel Pitfalls
Increases number of http requests
– Longer load time (harms performance)
– IIS Caching removes issue for subsequent requests
– Script Combining helps reduces requests
Posts Back full page
– Including Viewstate
– Push Viewstate to session can relieve issue
– Compressing Viewstate can relieve issue
Processes Full ASP.NET Page
35
AJAX – Update Panels
Chris Hay
Director
Roskakori Limited
36
AJAX
Web Services
Flow
– Expose Web Service to AJAX
– Call Web Service from Javascript
– Update DOM directly
Increases number of http requests
Only Posts Data that relates to the Operation
– No ViewState being posted
Only performs service operation
37
AJAX
Web Services – Exposing a Web Service
Create an ASMX Web Service (or WCF)
Mark ASMX Service as ScriptService
38
AJAX
Web Services – Referencing the Web Service
Reference in the ScriptManager
Generates Javascript Proxy Code
Inline Proxy Script Support
– Useful for Home Page
39
AJAX
Web Services – Calling the Web Service
Proxy is fully qualified namespace for method
40
AJAX – Web Services
Chris Hay
Director
Roskakori Limited
41
AJAX
Script Combining
Combines Microsoft AJAX Scripts & Javascript files
– ScriptResource.axd
– MyJavascript.js
Especially useful when using Ajax Control Toolkit
Does not combine WebResource.axd’s
Does not combine third party ScriptResource’s
Can use a third party script combiner
– http://www.codeproject.com/KB/aspnet/fastload.aspx
42
AJAX
Finding Scripts to Combine
Any .js script file
Look at name in loaded script (scriptresource.axd)
Use the ScriptReferenceProfiler
– Returns a list of referenced scripts on the page
– http://go.microsoft.com/?LinkID=8843390
43
AJAX
Script Combining
44
AJAX – Script Combining
Chris Hay
Director
Roskakori Limited
45
AJAX
Script Manager – Load Scripts Before UI
LoadScriptsBeforeUI = “false”
– Default is true
Page is rendered before loading scripts
– Faster for users
Pitfall: User interacts with script before loaded
– Be careful before messing with
46
AJAX
Silverlight Web Services to replace AJAX
Detect if user has Silverlight
– User has Silverlight: download .XAP file
– User does not have Silverlight: use AJAX
Use Html Bridge
Advantages
– Managed Code faster than Javascript
– Avoids extra HTTP Requests to download AJAX Scripts
Pitfalls
47
– Extra Code to Maintain
Silverlight – Web Services
Chris Hay
Director
Roskakori Limited
48
Bring Data Closer
Content Delivery Networks
Use a content delivery network to deliver static content
– Images
– Media
Saves Bandwidth
Closer Pages mean less latency
Silverlight Streaming
49
– Free 10Gb Account
– Host Silverlight Applications
– Host Silverlight Video
Back End Performance
Data Caching
Database is generally biggest bottleneck backend
Reduce calls to Database via Caching
Cache Providers
– ASP.NET Cache
– Velocity
– Memcached
50
Data Caching
Chris Hay
Director
Roskakori Limited
51
Back End Performance
Page Output Caching
Saves the Page / Control in the cache
Can use substitution control to vary data
52
Page Output Caching
Chris Hay
Director
Roskakori Limited
53
Back End Performance
Long Running Requests
Queue Request for Processing
– Asynchronous WCF Request
– Asynchronous Workflow Request
– MSMQ
AJAX Progress
Email Result
54
Back End Performance
Switch off Debug
Release Build
Web.Config
55
56
Resources
www.microsoft.com/teched
Tech·Talks
Live Simulcasts
Tech·Ed Bloggers
Virtual Labs
http://microsoft.com/expression
http://msdn.microsoft.com
Developer’s Kit, Licenses,
and MORE!
57
© 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.
58