Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

fast asp.net websites
Nội dung xem thử
Mô tả chi tiết
MANNING
Dean Alan Hume
www.it-ebooks.info
Fast ASP.NET Websites
www.it-ebooks.info
www.it-ebooks.info
Fast ASP.NET
Websites
DEAN ALAN HUME
MANNING
SHELTER ISLAND
www.it-ebooks.info
For Emily — ngiyakuthanda
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 261
Shelter Island, NY 11964
Email: [email protected]
©2013 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
Manning Publications Co. Development editor: Jennifer Stout
20 Baldwin Road Technical proofreader: Adam West
Copyeditor: Laura Cheu
PO Box 261 Proofreader: Elizabeth Martin
Shelter Island, NY 11964 Typesetter: Dennis Dalinnik
Cover designer: Marija Tudor
ISBN: 9781617291258
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 19 18 17 16 15 14 13
www.it-ebooks.info
v
contents
preface xi
acknowledgments xii
about this book xiv
PART 1 DEFINING PERFORMANCE . .................................1
1 High-speed websites 3
1.1 Why optimize? 4
1.2 The financial impact 4
The business impact 5 ■ The search engine ranking impact 5
The mobile user impact 6 ■ The environmental impact 6
1.3 How to optimize 6
Profile 7 ■ Identify 7 ■ Implement 7 ■ Monitor 8
1.4 Where to optimize 8
1.5 The Performance Golden Rule 9
1.6 Summary 9
www.it-ebooks.info
vi CONTENTS
2 First steps toward a faster website 11
2.1 The basics of HTTP 11
Understanding an HTTP GET request 12 ■ Understanding
an HTTP GET response 13 ■ Understanding HTTP
status codes 15
2.2 Empty cache vs. primed cache 16
2.3 Tips and tools for interpreting performance charts 17
What does it all mean? 17 ■ Google Chrome developer tools 19
Internet Explorer developer tools 21 ■ Firebug 21
Safari Web Inspector 21 ■ HTTPWatch 22 ■ WebPagetest 22
Fiddler 22
2.4 Performance rules to live by 23
Yahoo! YSlow 24 ■ Google PageSpeed 25
2.5 Summary 26
PART 2 GENERAL PERFORMANCE BEST PRACTICES.........27
3 Compression 29
3.1 What is compression? 29
3.2 Why should I use compression? 30
3.3 Pros and cons of compression 32
3.4 Types of compression 32
Gzip 32 ■ Deflate 33 ■ SDCH 33
3.5 Accept-Encoding 33
3.6 The Surf Store application 34
3.7 Adding compression to your website 35
Using IIS to add compression to a website 36
Using a Web.config file to add compression to a website 38
Adding compression with other techniques 40
3.8 The results 40
3.9 Summary 42
4 Caching: The sell-by date 43
4.1 What is HTTP caching? 44
4.2 IIS and HTTP caching 46
4.3 Web.config settings 50
www.it-ebooks.info
CONTENTS vii
4.4 Caching considerations 50
4.5 Output caching 51
Output caching in an ASP.NET MVC application 51
Output caching in an ASP.NET Web Forms application 54
4.6 The results of HTTP caching 56
4.7 Summary 57
5 Minifying and bundling static files 59
5.1 What is minification? 59
5.2 What is bundling? 63
5.3 New bundling and minifying features in ASP.NET 4.5 63
5.4 Utilizing bundling in ASP.NET MVC 66
5.5 Utilizing bundling in ASP.NET Web Forms 68
5.6 The results 72
5.7 Summary 74
6 HTML optimization tips 75
6.1 Where to position CSS and JavaScript in a web page to
achieve the best performance 76
CSS 76 ■ JavaScript 78
6.2 How the order of styles and scripts affects rendering 79
The impact of duplicate scripts 81
6.3 HTML5 81
6.4 A note on HTML5 browser support 82
HTML5 asynchronous JavaScript 83
HTML5 Web Workers 85 ■ Browser support for HTML5
Web Workers 86 ■ HTML5 Web Workers in an ASP.NET
MVC application 87 ■ Web Workers in an ASP.NET
Web Forms application 89
6.5 HTML5 application cache 91
HTML5 application cache considerations 92
HTML5 application cache in an ASP.NET MVC application 93
HTML5 application cache in an ASP.NET Web
Forms application 96 ■ Application cache support 97
6.6 Summary 98
www.it-ebooks.info
viii CONTENTS
7 Image optimization 99
7.1 What’s the big deal with image optimization? 100
7.2 Online image optimization tools 100
Smush.it 101 ■ Kraken 101
7.3 Command line image optimization tools 102
Pngcrush 103 ■ Jpegtran 103
7.4 Image Optimizer—a Visual Studio extension 103
7.5 Using data URIs 104
Implementing data URIs in an ASP.NET MVC application 106
Implementing data URIs in an ASP.NET Web
Forms application 109
7.6 The importance of specifying image dimensions 113
7.7 The results 114
7.8 Summary 115
8 ETags 117
8.1 What are ETags? 118
8.2 Why should I change ETags? 120
8.3 Removing ETags in ASP.NET Web Forms and ASP.NET
MVC applications 121
8.4 The results 122
8.5 Summary 123
9 Content Delivery Networks 125
9.1 What is a Content Delivery Network? 126
9.2 CDN options 128
9.3 Domain sharding 128
9.4 Developing with a CDN 130
ASP.NET MVC HTML helper for CDN development 130
ASP.NET Web Forms helper for CDN development 132
9.5 The results 134
9.6 Summary 136
www.it-ebooks.info
CONTENTS ix
PART 3 ASP.NET-SPECIFIC TECHNIQUES....................137
10 Tweaking ASP.NET MVC performance 139
10.1 Using only the view engines that you need 140
10.2 Release mode vs. Debug mode 141
10.3 The importance of a favicon 143
10.4 Utilizing a code profiler 146
MiniProfiler for database profiling 152
10.5 Summary 154
11 Tweaking ASP.NET Web Forms performance 155
11.1 HTML improvements 156
11.2 Web.config settings 158
Publishing your application in Release mode 158
Disable tracing if it’s not used 159 ■ Disable session state 160
Disable ViewState when it’s not needed 160
11.3 Response.Redirect vs. Server.Transfer 161
11.4 Utilizing a code profiler 162
11.5 Fixing the issue 168
11.6 Summary 169
12 Data caching 171
12.1 Server-side data caching 172
12.2 System.Runtime.Caching 172
12.3 What should I cache? 175
12.4 The sample application 175
12.5 Notes on distributed caching 177
12.6 Summary 178
12.7 Look at how far you’ve come! 178
appendix 181
index 185
www.it-ebooks.info
www.it-ebooks.info
xi
preface
Ever since I began building websites, I’ve been interested in learning how to make
them more efficient. It’s a great feeling when you transform a slow website into a
finely tuned engine that makes people say “Wow!” In my pursuit to improve my websites’ performance, I’ve trawled the net and spent long hours trying to find the best
techniques. Technology is constantly developing and improving, and developers are
finding newer and ever more ingenious ways of speeding up their sites.
If you’re a developer who is new to coding and website performance, the plethora
of resources can be quite overwhelming. Until I wrote Fast ASP.Net Websites, I hadn’t
seen a book that teaches the ASP.NET developer the exact formula, in a step-by-step
process, how to shave seconds off their page load times and drastically improve the
performance of their websites.
I hope you agree this is that book.
www.it-ebooks.info
xii
acknowledgments
Until now I never thought about all the research and background work that goes into
writing a technical book. This book would definitely not have been possible without
the help of many people.
Most importantly, I want to thank my partner Emily for her encouragement and
for sticking with me through all the early mornings and weekends it took to finish this
book. Every time my alarm went off, you never complained, not even once. Thank you
for all your support!
Sincere thanks to Jennifer Stout at Manning Publications for being the best
development editor…ever. Your cheerful attitude and brilliant work were instrumental in the evolution of this book. Thank you for always listening to my ideas
and being so efficient. Thanks to Michael Stephens for believing in me and in
the idea I had for this book. You guided me through each step of the process and
this book wouldn’t have been possible without your advice! Thanks to Candace
Gillhoolley for your help with the marketing of this book and to Rebecca Rinehart
for working with me on my idea for the book cover. Many thanks also to everyone on
the Manning production team for guiding me through the process and bringing the
book to press.
Special thanks to Sam Saffron for helping me review the MiniProfiler content. Sam
was one of the creators of MiniProfiler and helped me, even though he and his wife
had a new baby on the way, and he was in the process of launching a new website!
I am also grateful to all the reviewers who helped shape and improve the manuscript
during its development: Bryn Keller, Danylo Kizyma, Ivo Štimac, James Berkenbile,
www.it-ebooks.info
ACKNOWLEDGMENTS xiii
Jason Hales, Jeff Smith, Mark Sponsler, Michael Roberts, Onofrio Panzarino, and
Wyatt Barnett.
Special thanks to Robin Osborne and Tim Clarke for their reviewing help, and to
Adam West for his technical proofread of the final manuscript.
I would like to thank my family for their encouragement throughout the writing
process. You have been fantastic!
Finally, thank you for purchasing this book. I hope you enjoy reading it as much as
I enjoyed writing it. I hope you will learn valuable techniques you can use and apply to
all your websites.
Let’s make the web faster!
www.it-ebooks.info
xiv
about this book
This book is designed to allow you, as a developer, to get the best performance out
of your websites. This book delivers details, best practices, caveats, tips, and tricks to
improve the performance of your websites and reduce the load times of your web pages.
How to use this book
Every new chapter in this book is intended to teach the reader a new web performance concept. As you follow along with each chapter, and open the accompanying
source code, you will be able to follow the steps provided to improve the performance
of the sample website. Each chapter in the book is also designed to work as a standalone concept; that is, you can chose a chapter and apply just that technique and you
will improve your website. As we progress through the chapters, we will be constantly
improving the sample website and each technique will take the sample website closer
to performance nirvana.
Who should read this book
This book is for web developers who are looking to improve the performance of their
web pages. It is also for developers who are looking to dive a little deeper into web
development and understand the page lifecycle that is happening as a user loads their
website. This book covers fundamental techniques that are applicable to web pages
regardless of the programming language. The techniques that are covered are generally universal, but aimed toward the ASP.NET website developer.
www.it-ebooks.info