PHPNW09 Thomas Weinert Optimizing Your Frontend Performance
About Me ● Application Developer – PHP – XSLT/XPath – (some) Javascript ● papaya CMS – PHP based Content Management System – uses XSLT for Templates
How to scale you webpage? ● Hardware ● Backend ● Frontend
Hardware ● More hardware ● Better hardware ● Moores Law – Transistors doubling every 18 months – Transistors != Performance ● Distributed systems are complex ● Think about environment
Backend ● External data sources are slow – SQL – Files – Network ● Locking is slower ● Memory is faster – but less secure
Mini/Micro Optimisations ● Myths – echo vs. print – ' vs. " ● Objects vs. functions vs. linear source ● Template systems
Mini/Micro Optimisations DON'T DO IT
Yahoo! ● Yahoo!'s Exceptional Performance team – Yahoo!'s Exceptional Performance team evangelizes best practices for improving web performance. They conduct research, build tools, write articles and blogs, and speak at conferences. Their best practices center around the rules for high performance web sites. – http://developer.yahoo.com/performance/
Results ● 80% of the end-user response time is spent on the front-end.
Firebug ● Firefox extension ● Analyze and manipulate – requests, page structure, CSS ● Javascript debugger
Firebug Requests
Firebug Request II
HTTPFox ● Firefox Extension ● Log of all HTTP requests – Redirects – Cached requests
YSlow ● Why (is your web page) slow ● Set of rules ● Firebug extension
Yslow: Performance View
Yslow: Stats
Yslow: Components
Yslow: Tools
Google Page Speed ● Firebug extension ● CSS complexitiy
Google Page Speed Screenshot
Make Fewer HTTP Requests ● Combined files – CSS – JavaScript ● CSS sprites
Combined files ● Release process ● CSS – Consider URLs ● JavaScript – Minify – Obfuscate
CSS Sprites ● Elements with fixed size ● Background image ● Disable repeat ● Negative positions
CSS Icons ● Raster of icons ● No repeat ● Fixed size <div> or <span>
CSS Backgrounds ● Gradient ● Repeat in one direction
Minify Javascript ● Most JS libraries provide a minified version ● YUI Compressor: http://developer.yahoo.com/yui/compressor/ – JS and CSS ● Packer – Webpage, .Net, Perl, PHP – http://dean.edwards.name/packer/
#2 Use A CDN ● Content Delivery Network – Akamai Technologies – Mirror Image Internet – Limelight Networks ● Bring the content to your users – Geographic distance – Physics is still here ● Only for large sites ● Dynamic content is complex
Headers ● Expires ● Cache-Control – Session-Management ● 304 Not Modified
Expires ● Apache mod_expire ● ● <IfModule mod_expires.c> ● ExpiresDefault "access plus 1 month" ● ExpiresActive on ● </IfModule>
Cache-Control ● None – no caching – default for sessions ● Private – cacheable in browser cache ● Public – cacheable in browser cache and proxies
304 Not Modified ● Send Etag and Modfication date – Etag: "some hash" – Last-Modified: Thu, 12 Sep 2008 11:00:00 GMT ● Request headers – If-Modified-Since: Tue, 12 Sep 2008 11:00:00 GMT – If-None-Match: "some hash" ● Response headers – HTTP/1.1 304 Not Modified
Gzip Components ● Gzip != Zip – only compression – no packaging – tar.gz ● Good browser support – Accept-Encoding: gzip, deflate – Content-Encoding: gzip –
Gzip in Apache ● mod_gzip ● mod_deflate – filter chain, works on dynamic content, too ● – http://httpd.apache.org/docs/2.2/mod/mod_deflate.html
Gzip In PHP 5 <?php ob_start('ob_gzhandler'); ...
Gzip In PHP 5 <?php if (function_exists('ob_gzhandler')) { ob_start('ob_gzhandler'); } ...
Gzip Problems ● Not supported: – Transfer-Encoding: chunked – HTTP 1.0 client (old Squids)
Configure ETags ● Browser still asks webserver ● Server specific – CDN – Load balancer with multiple servers ● Apache – FileETag none ● IIS – http://support.microsoft.com/?id=922733
Split requests across domains ● HTTP 1.1 suggests 2 parallel requests per domain ● Separate content by function – www.domain.tld – themes.domain.tld – usercontent.domain.tld (security) ● Optimisation tools change the option ● Consider cookie domains
Reduce DNS Lookups ● DNS maps host names to ips ● Needs time – 20-120 milliseconds ● Cached in browser
Avoid Redirects ● Obviously addition requests ● Only cached with explicit headers ● http://www.domain.tld – → http://www.domain.tld/
Put Stylesheets at the Top ● Progressive display of the page ● Page appears to load faster ● W3C specifications
Put Scripts at the Bottom ● Scripts block parallel downloads – defer attribute in MSIE ● onload() event – used by most libraries ● Problem: document.write() – Counter – Banners
Avoid CSS Expressions ● MSIE from version 5 – cross browser experience ● JavaScript inside CSS ● Evaluated – page render – resize – scrolling – mouse movements (hover)
JavaScript And CSS Files ● Do not embed JS/CSS in your pages – No <script>...</script> – No <style>...</style> ● Seperate caching headers ● Revision parameter (style.css?rev=1234) – Get parameter – Unique URL for browser – Possibly in path/filename
Remove Duplicate Scripts ● Team size ● Standard scripts – XMLRPC, JQuery, Prototype ● Script module for your template system ● $templatesystem->addScript('foo.js');
Make Ajax Cacheable ● Often AJAX applications avoid caching – http://some.domain.tld/ajax.file?t=randomvalue ● A lot of requests ● Use more static URLs
References ● Slides: http://www.a-basketful-of-papayas.net/ ● Yahoo Performance Team – http://developer.yahoo.com/performance/ ● Google Page Speed – http://code.google.com/intl/en-UK/speed/page-speed/ ● Apache GZIP – http://httpd.apache.org/docs/2.2/mod/mod_deflate.html ● No Etag in IIS – http://support.microsoft.com/?id=922733

Optimizing Your Frontend Performance

  • 1.
    PHPNW09 Thomas Weinert Optimizing Your Frontend Performance
  • 2.
    About Me ● Application Developer – PHP – XSLT/XPath – (some) Javascript ● papaya CMS – PHP based Content Management System – uses XSLT for Templates
  • 3.
    How to scaleyou webpage? ● Hardware ● Backend ● Frontend
  • 4.
    Hardware ● More hardware ● Better hardware ● Moores Law – Transistors doubling every 18 months – Transistors != Performance ● Distributed systems are complex ● Think about environment
  • 5.
    Backend ● External data sources are slow – SQL – Files – Network ● Locking is slower ● Memory is faster – but less secure
  • 6.
    Mini/Micro Optimisations ● Myths – echo vs. print – ' vs. " ● Objects vs. functions vs. linear source ● Template systems
  • 7.
  • 8.
    Yahoo! ● Yahoo!'s Exceptional Performance team – Yahoo!'s Exceptional Performance team evangelizes best practices for improving web performance. They conduct research, build tools, write articles and blogs, and speak at conferences. Their best practices center around the rules for high performance web sites. – http://developer.yahoo.com/performance/
  • 9.
    Results ● 80% of the end-user response time is spent on the front-end.
  • 10.
    Firebug ● Firefox extension ● Analyze and manipulate – requests, page structure, CSS ● Javascript debugger
  • 11.
  • 12.
  • 13.
    HTTPFox ● Firefox Extension ● Log of all HTTP requests – Redirects – Cached requests
  • 14.
    YSlow ● Why (is your web page) slow ● Set of rules ● Firebug extension
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    Google Page Speed ● Firebug extension ● CSS complexitiy
  • 20.
  • 21.
    Make Fewer HTTPRequests ● Combined files – CSS – JavaScript ● CSS sprites
  • 22.
    Combined files ● Release process ● CSS – Consider URLs ● JavaScript – Minify – Obfuscate
  • 23.
    CSS Sprites ● Elements with fixed size ● Background image ● Disable repeat ● Negative positions
  • 24.
    CSS Icons ● Raster of icons ● No repeat ● Fixed size <div> or <span>
  • 25.
    CSS Backgrounds ● Gradient ● Repeat in one direction
  • 26.
    Minify Javascript ● Most JS libraries provide a minified version ● YUI Compressor: http://developer.yahoo.com/yui/compressor/ – JS and CSS ● Packer – Webpage, .Net, Perl, PHP – http://dean.edwards.name/packer/
  • 27.
    #2 Use ACDN ● Content Delivery Network – Akamai Technologies – Mirror Image Internet – Limelight Networks ● Bring the content to your users – Geographic distance – Physics is still here ● Only for large sites ● Dynamic content is complex
  • 28.
    Headers ● Expires ● Cache-Control – Session-Management ● 304 Not Modified
  • 29.
    Expires ● Apache mod_expire ● ● <IfModule mod_expires.c> ● ExpiresDefault "access plus 1 month" ● ExpiresActive on ● </IfModule>
  • 30.
    Cache-Control ● None – no caching – default for sessions ● Private – cacheable in browser cache ● Public – cacheable in browser cache and proxies
  • 31.
    304 Not Modified ● Send Etag and Modfication date – Etag: "some hash" – Last-Modified: Thu, 12 Sep 2008 11:00:00 GMT ● Request headers – If-Modified-Since: Tue, 12 Sep 2008 11:00:00 GMT – If-None-Match: "some hash" ● Response headers – HTTP/1.1 304 Not Modified
  • 32.
    Gzip Components ● Gzip != Zip – only compression – no packaging – tar.gz ● Good browser support – Accept-Encoding: gzip, deflate – Content-Encoding: gzip –
  • 33.
    Gzip in Apache ● mod_gzip ● mod_deflate – filter chain, works on dynamic content, too ● – http://httpd.apache.org/docs/2.2/mod/mod_deflate.html
  • 34.
    Gzip In PHP5 <?php ob_start('ob_gzhandler'); ...
  • 35.
    Gzip In PHP5 <?php if (function_exists('ob_gzhandler')) { ob_start('ob_gzhandler'); } ...
  • 36.
    Gzip Problems ● Not supported: – Transfer-Encoding: chunked – HTTP 1.0 client (old Squids)
  • 37.
    Configure ETags ● Browser still asks webserver ● Server specific – CDN – Load balancer with multiple servers ● Apache – FileETag none ● IIS – http://support.microsoft.com/?id=922733
  • 38.
    Split requests acrossdomains ● HTTP 1.1 suggests 2 parallel requests per domain ● Separate content by function – www.domain.tld – themes.domain.tld – usercontent.domain.tld (security) ● Optimisation tools change the option ● Consider cookie domains
  • 39.
    Reduce DNS Lookups ● DNS maps host names to ips ● Needs time – 20-120 milliseconds ● Cached in browser
  • 40.
    Avoid Redirects ● Obviously addition requests ● Only cached with explicit headers ● http://www.domain.tld – → http://www.domain.tld/
  • 41.
    Put Stylesheets atthe Top ● Progressive display of the page ● Page appears to load faster ● W3C specifications
  • 42.
    Put Scripts atthe Bottom ● Scripts block parallel downloads – defer attribute in MSIE ● onload() event – used by most libraries ● Problem: document.write() – Counter – Banners
  • 43.
    Avoid CSS Expressions ● MSIE from version 5 – cross browser experience ● JavaScript inside CSS ● Evaluated – page render – resize – scrolling – mouse movements (hover)
  • 44.
    JavaScript And CSSFiles ● Do not embed JS/CSS in your pages – No <script>...</script> – No <style>...</style> ● Seperate caching headers ● Revision parameter (style.css?rev=1234) – Get parameter – Unique URL for browser – Possibly in path/filename
  • 45.
    Remove Duplicate Scripts ● Team size ● Standard scripts – XMLRPC, JQuery, Prototype ● Script module for your template system ● $templatesystem->addScript('foo.js');
  • 46.
    Make Ajax Cacheable ● Often AJAX applications avoid caching – http://some.domain.tld/ajax.file?t=randomvalue ● A lot of requests ● Use more static URLs
  • 47.
    References ● Slides: http://www.a-basketful-of-papayas.net/ ● Yahoo Performance Team – http://developer.yahoo.com/performance/ ● Google Page Speed – http://code.google.com/intl/en-UK/speed/page-speed/ ● Apache GZIP – http://httpd.apache.org/docs/2.2/mod/mod_deflate.html ● No Etag in IIS – http://support.microsoft.com/?id=922733