Let's speed it up a bit (AmsterdamPHP)

34
More pageviews coming up? Improve page loading speed? Add more boxes!” - Anonymous developer Thursday, January 17, 13

description

It is said that when asking people how a website can be optimised for speed, about 14 answers come up. For both simple HTML sites on shared hosting and advanced multi-server web applications, I'm sharing these answers as strategies to speed up your (php powered) website in this talk.

Transcript of Let's speed it up a bit (AmsterdamPHP)

Page 1: Let's speed it up a bit (AmsterdamPHP)

“More pageviews coming up?Improve page loading speed?

Add more boxes!”- Anonymous developer

Thursday, January 17, 13

Page 2: Let's speed it up a bit (AmsterdamPHP)

Let’s speed it up a bit

Pascal de Vink (@pascaldevink)

13 performance optimizing strategies[*]

Thursday, January 17, 13

Page 3: Let's speed it up a bit (AmsterdamPHP)

About me

Thursday, January 17, 13

Page 4: Let's speed it up a bit (AmsterdamPHP)

Why does it matter?

Thursday, January 17, 13

Page 5: Let's speed it up a bit (AmsterdamPHP)

So, it hurts...

Thursday, January 17, 13

Page 6: Let's speed it up a bit (AmsterdamPHP)

So, it hurts...

• Your users

Thursday, January 17, 13

Page 7: Let's speed it up a bit (AmsterdamPHP)

So, it hurts...

• Your users

• Your search ranking[1]

Thursday, January 17, 13

Page 8: Let's speed it up a bit (AmsterdamPHP)

So, it hurts...

• Your users

• Your search ranking[1]

• Your profit[2]

Thursday, January 17, 13

Page 9: Let's speed it up a bit (AmsterdamPHP)

Measure it

• Request/second benchmarking with AB or jmeter

• Page load speed with WebPageTest[3]

Thursday, January 17, 13

Page 10: Let's speed it up a bit (AmsterdamPHP)

#1 HTTP cache headers

“If the content of the script changes only depending on what’s in the URL, it is cacheable; if the output depends on a cookie, authentication information or other external criteria, it probably isn’t”[4]

- Mark Nottingham

Thursday, January 17, 13

Page 11: Let's speed it up a bit (AmsterdamPHP)

#1 HTTP cache headers

• Add:

• But, be aware of its use, eg with authentication pages

Cache-Control: public max-age=[seconds]Last-Modified: Mon, 29 Jun 1998 02:28:12 GMT

Thursday, January 17, 13

Page 12: Let's speed it up a bit (AmsterdamPHP)

#2 Minification• Place javascript at the bottom of the html

• Place CSS at the top of the html

• Use Minify JavaScript & CSS

• Always write JavaScript & CSS in external file

• Try to reduce number of JavaScript & CSS files

• Assetic[5] can help you use tools

Thursday, January 17, 13

Page 13: Let's speed it up a bit (AmsterdamPHP)

#3 GZIP compression<IfModule mod_deflate.c> SetOutputFilter DEFLATE # Don’t compress SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary #Dealing with proxy servers <IfModule mod_headers.c> Header append Vary User-Agent </IfModule></IfModule>

Thursday, January 17, 13

Page 14: Let's speed it up a bit (AmsterdamPHP)

#4 CDN

• Content Delivery Network

• Used for streaming video

• Based on the browser limit of 2 requests (not applicable anymore)

Thursday, January 17, 13

Page 15: Let's speed it up a bit (AmsterdamPHP)

#4 CDN

Thursday, January 17, 13

Page 16: Let's speed it up a bit (AmsterdamPHP)

#4 CDN

• CloudFlare is a free CDN[6]

• Amazon CloudFront is cheap[7]

• Amazon S3 works too[8]

• Roll your own

Thursday, January 17, 13

Page 17: Let's speed it up a bit (AmsterdamPHP)

#5 Static site generation

• Generate page(s) only if changed

• Hype: blogging on dropbox/github pages[9]

Thursday, January 17, 13

Page 18: Let's speed it up a bit (AmsterdamPHP)

#6 Tune apache

• KeepAlive, KeepAliveTimeout, StartServers, MaxClients[10]

Thursday, January 17, 13

Page 19: Let's speed it up a bit (AmsterdamPHP)

#6b Get rid of apache

Thursday, January 17, 13

Page 20: Let's speed it up a bit (AmsterdamPHP)

#7 Opcode caching

• Pre-compiling PHP in memory[10]

• APC, eAccelerator, Zend server

Thursday, January 17, 13

Page 21: Let's speed it up a bit (AmsterdamPHP)

#8 Varnish

• Load balancing

• Serving assets

• Maintenance mode

Thursday, January 17, 13

Page 22: Let's speed it up a bit (AmsterdamPHP)

#9 Memcached• In-memory object caching system[11]

Thursday, January 17, 13

Page 23: Let's speed it up a bit (AmsterdamPHP)

#10 Improve your SQL

• Use lazy-loading

• Tune MySQL[12]

• Use NoSQL

Thursday, January 17, 13

Page 24: Let's speed it up a bit (AmsterdamPHP)

#11 Load it off the request

• Message queues (ActiveMQ, RabbitMQ)

• Load off :

• Mails (notifications)

• Background changes (change of product 1 changes product 2)

• Logging (non vital)

• Cache updates

Thursday, January 17, 13

Page 25: Let's speed it up a bit (AmsterdamPHP)

#12 Improve your file handling

• Reduce your include_path to a single entry or don't use it at all

• Include or require absolute paths

• Ensure that each try to include a file is a hit

• Avoid file_exists(), is_file(), is_dir(), is_link() etc.

• Avoid autoloading[13]

Thursday, January 17, 13

Page 26: Let's speed it up a bit (AmsterdamPHP)

#13 Use output buffering

• Specially for huge pages

• StreamedResponse in Symfony 2.1

Thursday, January 17, 13

Page 27: Let's speed it up a bit (AmsterdamPHP)

Bonus #14 Use Google SPDY

• Prioritizing & multiplexing

• Encryption & compression baked in

• Hint/push content

• Chromium, Firefox & Opera only

Thursday, January 17, 13

Page 28: Let's speed it up a bit (AmsterdamPHP)

Bonus #15 Use WebSockets

• Faster then AJAX request

• Great for instant client communication

Thursday, January 17, 13

Page 29: Let's speed it up a bit (AmsterdamPHP)

In conclusion• #1 HTTP Cache Headers

• #2 GZIP Compression

• #3 Minification

• #4 CDN

• #5 Static site generation

• #6 Tune or ditch Apache

• #7 Opcode caching

• #8 Varnish

• #9 Memcached

• #10 Improve your SQL

• #11 Load it off the request

• #12 Improve file handling

• #13 Use output buffering

• Bonus #14 Use Google SPDY

• Bonus #15 Use websockets

Thursday, January 17, 13

Page 30: Let's speed it up a bit (AmsterdamPHP)

One more thing

Thursday, January 17, 13

Page 31: Let's speed it up a bit (AmsterdamPHP)

One more thing

Thursday, January 17, 13

Page 32: Let's speed it up a bit (AmsterdamPHP)

One more thing

Thursday, January 17, 13

Page 33: Let's speed it up a bit (AmsterdamPHP)

Questions?

@pascaldevink

https://joind.in/talk/view/8066

Thursday, January 17, 13

Page 34: Let's speed it up a bit (AmsterdamPHP)

References• infographics: http://mashable.com/2012/11/22/slow-websites/

• varnish: http://deglos.com/blog/2010/09/22/drupal-and-varnish-quick-intro

• Webservers performance http://www.rootusers.com/web-server-performance-benchmark/

• [1] http://copperegg.com/slow-websites-hurt-search-engine-rankings/

• [2] http://econsultancy.com/nl/blog/11274-67-of-consumers-cite-slow-websites-as-the-main-cause-of-basket-abandonment

• [3] http://www.webpagetest.org

• [4] http://www.mnot.net/cache_docs/

• [5] http://symfony.com/doc/2.0/cookbook/assetic/asset_management.html

• [6] http://www.cloudflare.com/

• [7] http://aws.amazon.com/cloudfront/

• [8] http://aws.amazon.com/s3/

• [9] http://octopress.org/

• [10] http://www.bootstrappingindependence.com/technology/how-to-improve-website-performance-with-drupal-php-mysql-and-apache/

• [11] http://memcached.org/

• [12] http://www.mysqlperformanceblog.com/2006/09/29/what-to-tune-in-mysql-server-after-installation

Thursday, January 17, 13