Performance on a budget
Upcoming SlideShare
Loading in...5

Performance on a budget



My Austin Code Camp 2012 talk on Web Performance.

My Austin Code Camp 2012 talk on Web Performance.



Total Views
Views on SlideShare
Embed Views



3 Embeds 32 29 2 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • 2 minutes (4 total)-quickly cover what you mean by performance. Performance could mean different things to different people.
  • 3 minutes (7 minutes)
  • 4 minutes (11 minutes)Quickly go through different types of testingLive profilingAutomated testingIncludes unit tests (Nunit)Includes integration tests (Selenium)Load testingLoad  regular load testingStress  keep increasing number of users until it breaksPeak  test against peak loadBreak  kill various parts of the applications and monitor user experienceFailover  if you have multiple data centers/kill the data center and test againSTART DEMO HEREGo over what it is: facebook for DOGSRun through the applicationPoint out slowness in loading imagesPoint out slowness in loading entire pagePoint out how going back to the page does not help
  • 97% of mobile and 85% of desktop is spent on frontend
  • Measure with waterfall (SHOW EXAMPLE) - know the differences (  improves by 13-25%unavailable in some old browsersAsset size-don’t download unnecessary information (extra css and js)-excess dom-don’t download and hide content-don’t download and shrink content-minify js and cssCAVEAT: mobile pattern:-embed JS and CSS in the first response. save JS and CSS to localStorage on phone, then subsequent requests would only load those files that are needed based on cookie [set with stored js/css].  -More work needed to figure out which page to return but still faster.-caveat on caveat localStorage read is MUCH SLOWER than object read since it’s disk read [data size stored does not matter per key] ( DEMO (STEP 1):Turn on GZIPUse minified files
  • Optimize client rendering-optimize images with css sprites, gradients and incline images-use jsperf to compare different functions-understand dom behavior for different events-e.g. understand difference between bubbling and capturing events-avoid reflows (DOM manipulation, layout change, window resizing, table manipulation, absolute/fixed better than static/relative position, font change, stylesheet add/remove)-avoid repaints (add outline, change visibility, change bg color)-DOM manipulation -- add subtrees to dom tree at once-clean up objects after you’re done (delete key)-primitive operators over function callsFONTS- Host the fonts on a CDN- GZIP all except .woff (already compressed)- Cache all font files for 30+ days- Remove excess glyphs (characters) from the font files- Ensure @font-face is the first rule of the first stylesheet on the page (IE)- Load through javascript ( DEMO:-remove bad fonts and update through tool-remove unnecessary repaints and reflows
  • Progressive Enhancement-deliver HTML first and quickly (chunked encoding)-load visible content first-defer JS until the end-decorate after everything is loaded-use controlJSfile for async loading queries-Synchronous scripts block all following elements from rendering- insertBefore (async) vs. script tag (sync)- async- xhreval (bad), xhr injection, script in iframe, script dom element, script defer, document.write- use anywhere.js/bootstrap.js to ansynchronously load third-party libraries for JS with dependenciesSHOW DEMO (STEP 2):-put css on top-put js on the bottom
  • 3 minutes (24 minutes)-TCP slow start - to control congestion inside the network- server starts by sending a few packets, waits for acknowledgement and then increases the # subsequentlyTalk about latency fluctuations-65-145ms on desktop vs up to 900ms on mobileMobile-Wireless provider slowdown-Android closes TCP sockets after 6 secs of inactivity-3g device establishes radio link to carrier’s cell tower (consumes battery)takes 1-2 seconds to connect to cell towerSteve Souders wrote about these findings  is the fastest growing mobile commerce (Rakuten's Online Commerce Revenue, Desktop vs Mobile)-Cloudfront from Amazon (doesn’t do gzip out of the box)-Cloudflare (free service)No redirects-redirecting on mobile are expensive (due to latency)Avoid css importsDon’t download the same asset twice (especially if not cached)Prefetch-predownload necessary assets before they’re requiredCaching -adding Expires Header-35% reduction in bandwidthWork around TCP slow startDropped packetsDNS lookups DEMO (STEP 3):-add all images in webpage as sprite-combine js and cssfiles [COMBRES]-Turn on caching [web.config]
  • -blitz.iobrowsermob-proxy ( benchmark - allows benchmarking on functions ( cssstress - to test css profiling (
  • -> ( resizes the image to fit the physical screen (MOBILE)- imagealpha -> ( converts 24-bit PNG to paletted 8-bit PNG (MOBILE)8-bit is kinda like a gif (256 color palette)
  • Rely on service oriented architectureSeparate your data Transactional vs. reportingSeparate I/O and CPU bound processes on different machinesRandom disk i/oUtilize event sourcing patternsqueuing/messaging- service bus- cqrs- eventual consistency- concurrent operations- async- be careful of tail latency (concurrent requests that the parent request depends on all completing)- duplicate work to rid of latency (multiple requests for same data to different servers.  Only take the first responding and disregard the rest) ( EVENTUAL CONSISTENCY- use a write DB and a read DB and sync between the two.- no locks, etc.- ( understand the tradeoffs
  • CACHE- Use memcached or Redis- A significant portion of an application’s  resources is consumed by I/O operations, which usually are data accesses.- Increases memory usage- too much memory utilized will start using swap (& move pages to virtual mem - hard disk) - increased I/O - no caching benefits- If data req can be predicted, then prefetch into cache; else, lazy load- Primed cache - set of required resources can be predicted (PREFETCH)- Demand cache - set of required resources cannot be predicted- Cache Accessor- Cache Search Sequence- Cache Collector- Cache Replicator- Cache Statistics- ASP.NET Caching ( Application Cache - not stored in memory for life of the application- Page output cache - store rendered HTML- cache whole page or part of it- for those pages that are always the same (profile page)- Caching data for ASP.NETSHOW DEMO (STEP 4):-update to use memory cache
  • Long & short running queries- Write/write conflicts- Large joins
  • Be careful of Object Relational MappingsDon’t rely on Exceptions for logicPoolingThreadsDBbatch requests- batch responses (stored procedures returning multiple results)- Ahead of-time compilation (NGen in CLR) may improve over JIT- Large object fragmentation- Split arrays into smaller units so that they remain below 85kB (and so are never allocated on the large object heap).- You can allocate the largest and longest-living objects first (if your objects are files which are queued for processing, for example).- In some cases, it may be that periodically stopping and restarting the program is the only option.understand operation impact in .NET on performance- Integer arithmetic is 1 instruction count while object allocation can be more than 1,000 ( memory is good. The right memory is best:Memory consumption- CPU-intensive application manipulating large amount of data- L1 Cache ⇒ L2 Cache ⇒ RAM ⇒ DISK (speed decreases 10 fold in memory and 10,000 fold in disk- Cold startupWORKER THREADS- The request architecture of ASP.NET balances between request threads and available resources. - Thread gating - number of concurrently executing requests for which CPU power available. - Monitor thread gating in the Windows Performance monitor using the Pipeline Instance Count performance counter.- Page request generally stops until external resource (db/service requests) responds- The result can be many concurrently executing requests and many waiting threads in the ASP.NET worker process or application pool. - To reduce this effect on performance, you can manually set the limit on the number of threads in the process. - To do so, change the MaxWorkerThreads and MaxIOThreads attributes in the processModel section of the Machine.config file.- You can determine what the appropriate number of threads is by performance-testing your application.thread deadlocks- single thread can be blocked waiting on disk (fetching data), network (cross-machine resources), event/locks (waiting on other threads)
  • Show example forperfmon and measureit
  • 5 minutes (59 minutes)SDLCRequirementsperformance goals- Architecture - performance metrics/SLAs- Development - profiling/prototyping- Testing - load testing- Continuous Integration testing- with Selenium testing- profiling on Unit Tests - (VS 2010 profiler -
  • - Identify critical business transactions- frequently used transactions- performance-intensive transactions
  • - do not prematurely optimize- CQRS- split up write services and read services- Start with 3rd party tools but if necessary, roll out your own- Sometimes standard solutions don’t fit - don’t be afraid to write custom ( When diagnosing, go for bottleneck and retest. That will lead you to your next bottleneck- Identify critical business transactions- frequently used transactions- performance-intensive transactionsSPDY ( request per connectionexclusively client-initiated requestsuncompressed request/response headers (gzip doesn’t help)redundant headers (User-Agent, Host, Accept* are usually static)AFTER:- multiple requests per connection (TCP conn doesn’t close)- compresses/eliminates headers- SSL first class- request prioritization- server push (X-Associated-Content)- suggest to the client to ask for specific resources (X-Subresources)- HTTP 2.0 - MS - just a proposal unlike SPDY ( ResourceTiming ( ZeroMQSHOW DEMO:Show difference between the first response in waterfall to the last response in waterfall and the difference (RUN YSLOW AGAIN AND SHOW DIFFERENCE)

Performance on a budget Performance on a budget Presentation Transcript

  • Performance on a Budget Dimitry Ushakov June 9, 2012
  • Austin Code Camp 2012 Sponsors
  • Overview What is Performance? Why Performance? How to Measure Performance? Improving Performance Tools Best Practices
  • What is Performance? 3 parts of performance  Speed  How quickly application responds to a user  Scalability  How application handles expected users and beyond  Stability  How stable the application is under load  Could be expected or unexpected
  • Why Performance?
  • Slowdown Of 1 Second Costs $1.6 Billion
  • How to Measure Performance • Live Single user execution • How quickly the application responds throughout the full application call • Automated unit and integration testing • Multi-user performance testing • Load testing • Stress testing • Peak testing • Duration testing • Failover testing
  • Improving Performance 80-90% of the end-user response time is spent on the frontend. Start there - Steve Souders
  • Front End – Asset Size Compress assets • GZIP (all but PNG) • httpcompression Minify JS & CSS Don’t overload asset contents
  • Front End – Client Rendering Excess DOM Understand JS events DOM manipulation Fonts Avoid repaints and reflows Cache JS References: • profit-optimization-notes/ • performance-javascript/
  • Front End – ProgressiveEnhancement Chunked encoding • ASP.NET doesn’t chunk by default • If you turn it on and writeln to response, each write will get chunked (big perf hit for large HTML) AJAX Defer JS CSS on top JS on the bottom JS load asynchronously
  • Latency Use CDNs Caching • 35% reduction in bandwidth Combine JS and CSS files Load JS asynchronously Sprites Inline images Prefetch and cache assets for future use
  • Tools for Frontend Measurement Grading Tools Profiling Timing PageSpeed SpeedTracer -can be a plugin -Chrome add-on -mobile timing -can be a CDN Yslow! Web developer Webpagetest -add-on tools -can select location and enter URL -runs locally (Chrome/IE/Firefo -mobile section uses Blaze.IO x) Firebug PCAP Web Performance Analyzer -uses hars/pcaps to analyze webpages -tests how long page loads from various locations
  • Tools for Performance analysis Analytics Network Analyzer APIs Google analytics Fiddler Navigation timing (( R/navigation-timing/) -shows all parts of network in page load -doesn’t split by resource (future) Statsd Wireshark (not free) Boomerang.js -nodejs library that -measures network collects stats traffic Tcpdump -logs tcp calls to a url cUrl -web crawler Dig -investigates DNS
  • Tools for Content Delivery JSON deliver CDN Jdrop CloudFlare -Store JSON data in the cloud CloudFront PageSpeed
  • Tools for Testing Performance Testing Code analysis Benchmark.js -ability to run up to 250 users in 60 -framework for measuring method seconds for free. response times -can be automated (use in continuous integration) Browsermob-proxy -captures har data from tests during functional tests Css-stress -profiles css stylesheets
  • Tools for Mobile Optimizations Images Font FontSquirrel -resizes the image to fit the -generates font that’s best for your physical screen (mobile) device Imagealpha -converts 24-bit PNG to 8-bit PNG (mobile)
  • Backend - SOA • Rely on service oriented architecture • Separate your data • Transactional vs. reporting • Separate I/O and CPU bound processes on different machines • Utilize event sourcing patterns • Concurrent operations • Be careful! • Eventual Consistency
  • Backend - Cache • Cache as much as you can • But not too much! • Use the right caching tool • Understand different caching patterns • Primed Cache • Demand Cache • Take advantage of ASP.NET Caching
  • Backend - DB • Long running queries • Write/write conflicts • Large joins • indexes
  • Backend • BIGGEST PROBLEM  CONTEXT SWITCHING • Measure GC pauses • Optimize worker threads • Thread deadlocks • Be careful of Large object fragmentation • Be careful of Object Relational Mappings • Don’t rely on Exceptions for logic • Utilize Connection Pooling • Threads • DB • Utilize Batch requests and responses • Understand operation impact on .NET Performance • Take advantage of memory utilization
  • Tools for Profiling Web profiling Instrumented profiling Glimpse ANTS Performance Profiler ($499) Mini-profiler JetBrains dotTrace ($399) -can integrate with ReSharper dynaTrace (varies) -can be integrated with CI -can do monitoring/profiling/diagnostics and much more -most expensive VS2010 Profiler -offered in Visual Studio
  • Tools for Profiling Heap profiler Benchmarking PerfView MeasureIt PerfMon Comparison for APM and BTM tools ( Analysis-Application-Performance-Management-and- Business) Goes over a set of tools (none are free and most are enterprise) for monitoring and transaction tracing
  • Best Practices - SDLC Requirements Establish performance goals Architecture Document performance metrics Document SLAs Development Profiling Prototyping Continuous Integration Testing Performance testing
  • Best Practices – What to test • Critical business transactions • Frequently used transactions • Performance-intensive transactions
  • Best Practices - notes • DO NOT prematurely optimize • DO start with 3rd party tools but roll your own solutions if necessary for performance • DO NOT be afraid to modify standard solutions • DO go for the first bottleneck and always retest after • Follow “test  fix  retest” pattern
  • Contact @dimtruck entation • Will have slides of the presentation and references