High Performance
Drupal Web Sites

     360°

      jbguerraz@skilld.fr
Who's that punk ?!
●
    A French nerd !
●   Used to manage complex IT projects for the
    last 10 years
●
    And many more nights on performances

    issues   ... :)
●
    ... who just created his own company
Summary



    Cache

    Upload & Download

    Browser rendering

    Compression

    Drupal pages architecture

    Analyze
Cache
What should we cache ?

                            EVERY POSSIBLE THING !!!

●   PHP (opcode)
●   Computations (functions results)
●   Datas (views, DB requests)
●   Entities & Fields (nodes, comments, taxonomy terms, user profiles,…)
●   Images (imagecache/styles)
●   HTML (page, block, panel, pane, views)
●   Any static ressource (CSS, JS, SWF, SVG, PDF, ZIP, Video, Sound, …)




                                   jbguerraz@skilld.fr
Cache
How should we cache ?


PHP (opcode) :                                // allocate one segment of 32Mb
                                              apc.shm_segments=1
                                              apc.shm_size=32

                                              // do not check if php file was updated
                                              apc.stat= 0
●
    APC                                       // never expire
                                              apc.ttl = 0
●
    Eaccelerator (a bit faster,               // use kernel anonymous memory

    seg fault)                                apc.mmap_file_mask = /dev/zero.




                        jbguerraz@skilld.fr
Cache
How should we cache ?

                                         function mymodule_complex_calculation_crazy_cache() {
Computations (functions results) :         static $cache ;
                                           if (isset($cache)) {
                                             return $cache ;
                                           }
● drupal_static (sometime static if big    if ($cache = &drupal_static(__FUNCTION__)) {
                                             return $cache;
  amount of calls within a page load) }
                                           if ($cache = cache_get('my_cache_item', 'my_cache_bin')) {
                                             $cache = $cache->data;
                                           }
● cache_set / cache_get (use your own else {

  bin if lot of data to store and/or need // some = complex_calculation(); kitten
                                             $cache
                                                      heavy calculations that kills

  more control over your cache)              cache_set('my_cache_item', $cache, 'my_cache_bin');
                                           }
                                           return $cache ;
                                         }




                                       jbguerraz@skilld.fr
Cache
                                                 Note :
How should we cache ?                            A custom cache bin require, at
                                                 module install, to create a new cache
                                                 table using the same schema as core
                                                 cache tables (see table below).
                                                 Copy/paste from :
Datas (views, DB requests) :                     includes/system.install

●
    Views cache                                        Field          Type      Null
                                                 cid           varchar(255)   no
●
    Views per user cache
                                                 data          longblob       yes
●
    Own cache bin for DB requests (if            expire        int            no
    somehow there is a good reason               created       int            no
    to request DB directly)                      headers       text           yes
                                                 serialized smallint          no




                           jbguerraz@skilld.fr
Cache
How should we cache ?

Entities (nodes, comments, taxonomy terms,
 user profiles,…) :             Entity cache
                                                       Load 1 000 users
●
    Entity cache                50
                                45
                                40
                                35                                                 First load
                                30                                                 Next loads
                      seconds



                                25
                                20
                                15
                                10
                                 5
                                 0
                                     No entity cache                Entity cache




                   jbguerraz@skilld.fr
Cache
How should we cache ?


Images (imagecache/styles) :
●
    ImageCache
●
    ImageCache External




                    jbguerraz@skilld.fr
Cache
How should we cache ?


HTML (page, block, panel, pane, views) :
●
    Panels Page Cache
●
    Panels Hash Cache
●
    Varnish
●
    Varnish ESI


                    jbguerraz@skilld.fr
Cache
How should we cache ?


Any static ressource (CSS, JS, SWF, SVG,
 PDF, ZIP, Video, Sound, …) :
●
    Varnish
●
    Nginx
●
    CDN far-future



                     jbguerraz@skilld.fr
Cache
What cache backends ?




             http://janezurevc.name/static/bcn_cache



                    jbguerraz@skilld.fr
Cache
What about cache invalidation ?

●
    Cache actions
●
    Cachetags
●
    Views content cache
●
    Expire
●
    Entity cache flusher


                       jbguerraz@skilld.fr
Upload & Download

Cookie free domains

CDN module in order to use static resources
 dedicated domain(s) which differ from main
 domain (for instance, static.mysite.com)

$cookie_domain have to be set to main domain in
  settings.php (for instance www.mysite.com)


                   jbguerraz@skilld.fr
Upload & Download

Aggregates

●
  JS (Advanced aggregate)
                                        Note :
●
  CSS (Advanced aggregate)              Sprites are now obselete, use CSS
                                        embeded image instead
●
  Images & CSS (CSS
                                        (for IE, images <32Kb can be
   embeded image)                       embeded)

●
  Images (spritesheets)



                  jbguerraz@skilld.fr
Upload & Download

Parrallelization

●
  CDN (URL
  sharding)
●
  Head.js
                                                                    CSS
●
  Yepnopejs
(Interesting but not yet drupal integrated)
                                                                    JS




                                              jbguerraz@skilld.fr
Browser rendering

JS

●
    Rendering sequence : request as soon as it
    parse
       ●
           JS in footer scope
●
    Head.js / lab.js (defer)




                          jbguerraz@skilld.fr
Browser rendering

CSS

●
  Translate3D, no DOM position change
●
  Pay attention to selectors performances ! No
  CSS3 selectors (great but slow)
●
  Avoid blinking > no JS to hide an element on
  load, hide by CSS and show from JS
  (perception matters!)


                    jbguerraz@skilld.fr
Compression

What should we compress ?

●
  Images
●
  CSS
●
  JS
●
  HTML
●
  «Any» static file



                       jbguerraz@skilld.fr
Compression

How should we compress ?

Images

●
    Imageapi optimize
       ●
           jpegtran
       ●
           advpng




                        jbguerraz@skilld.fr
Compression

How should we compress ?

CSS

●
  Advagg CSS compress
●
  CSS Compressor
       ●
           faster than CSSTidy
●
    gzip


                         jbguerraz@skilld.fr
Compression

How should we compress ?

JS

●
  Advagg JS compress
●
  JSMin
       ●
           faster than JSMin+
●
    Gzip


                          jbguerraz@skilld.fr
Compression

How should we compress ?

HTML

●
    Gzip




                jbguerraz@skilld.fr
Compression

How should we compress ?

Any static files

●
    Gzip




                    jbguerraz@skilld.fr
Drupal pages architecture

How to build a performant page ?

      USE AS FEW MODULES AS POSSIBLE !

  ●Search_api (solr, mongo)          ●Panels
  ●Mongodb                           ●Panels everywhere

  ●Redis                             ●Views

  ●Memcache                          ●Rules

  ●Elysia cron                       ●Entities




                              jbguerraz@skilld.fr
Drupal pages architecture

Why homogeneity maters ?

           Blocks, panes, pick one !

One single way of managing « blocks » helps
 using ESI caching for instance; it also makes
 easier the cache invalidation management for
 these «blocks».
      Keep it simple : one API to implement


                   jbguerraz@skilld.fr
Analyze

Browser level

●
  Gtmetrix.com
●
  Yslow
●
  Firebug
●
  Chrome / Safari developer tools
●
  Whichloadsfaster.com



                    jbguerraz@skilld.fr
Analyze

Drupal level

●
  Devel
●
  Watchdog (redirected to syslog if needed on live
  server)




                    jbguerraz@skilld.fr
Analyze

PHP level

●
  FirePHP / ChromePHP
●
  Xhprof (eventually in addition to Xdebug)
●
  PHP error log
●
  PHP-FPM slow logs
●
  MySQL Slow query logs



                    jbguerraz@skilld.fr
Analyze

System level

●
  strace
●
  tcpdump & wireshark




                   jbguerraz@skilld.fr
Spasibo bolshoe !
Your time now, any question ?




         Wanna get these slides ?


    Search for jbguerraz on Slideshare ;)




              jbguerraz@skilld.fr

Drupal Camp Kiev 2012 - High Performance Drupal Web Sites

  • 1.
    High Performance Drupal WebSites 360° jbguerraz@skilld.fr
  • 2.
    Who's that punk?! ● A French nerd ! ● Used to manage complex IT projects for the last 10 years ● And many more nights on performances issues ... :) ● ... who just created his own company
  • 3.
    Summary  Cache  Upload & Download  Browser rendering  Compression  Drupal pages architecture  Analyze
  • 4.
    Cache What should wecache ? EVERY POSSIBLE THING !!! ● PHP (opcode) ● Computations (functions results) ● Datas (views, DB requests) ● Entities & Fields (nodes, comments, taxonomy terms, user profiles,…) ● Images (imagecache/styles) ● HTML (page, block, panel, pane, views) ● Any static ressource (CSS, JS, SWF, SVG, PDF, ZIP, Video, Sound, …) jbguerraz@skilld.fr
  • 5.
    Cache How should wecache ? PHP (opcode) : // allocate one segment of 32Mb apc.shm_segments=1 apc.shm_size=32 // do not check if php file was updated apc.stat= 0 ● APC // never expire apc.ttl = 0 ● Eaccelerator (a bit faster, // use kernel anonymous memory seg fault) apc.mmap_file_mask = /dev/zero. jbguerraz@skilld.fr
  • 6.
    Cache How should wecache ? function mymodule_complex_calculation_crazy_cache() { Computations (functions results) : static $cache ; if (isset($cache)) { return $cache ; } ● drupal_static (sometime static if big if ($cache = &drupal_static(__FUNCTION__)) { return $cache; amount of calls within a page load) } if ($cache = cache_get('my_cache_item', 'my_cache_bin')) { $cache = $cache->data; } ● cache_set / cache_get (use your own else { bin if lot of data to store and/or need // some = complex_calculation(); kitten $cache heavy calculations that kills more control over your cache) cache_set('my_cache_item', $cache, 'my_cache_bin'); } return $cache ; } jbguerraz@skilld.fr
  • 7.
    Cache Note : How should we cache ? A custom cache bin require, at module install, to create a new cache table using the same schema as core cache tables (see table below). Copy/paste from : Datas (views, DB requests) : includes/system.install ● Views cache Field Type Null cid varchar(255) no ● Views per user cache data longblob yes ● Own cache bin for DB requests (if expire int no somehow there is a good reason created int no to request DB directly) headers text yes serialized smallint no jbguerraz@skilld.fr
  • 8.
    Cache How should wecache ? Entities (nodes, comments, taxonomy terms, user profiles,…) : Entity cache Load 1 000 users ● Entity cache 50 45 40 35 First load 30 Next loads seconds 25 20 15 10 5 0 No entity cache Entity cache jbguerraz@skilld.fr
  • 9.
    Cache How should wecache ? Images (imagecache/styles) : ● ImageCache ● ImageCache External jbguerraz@skilld.fr
  • 10.
    Cache How should wecache ? HTML (page, block, panel, pane, views) : ● Panels Page Cache ● Panels Hash Cache ● Varnish ● Varnish ESI jbguerraz@skilld.fr
  • 11.
    Cache How should wecache ? Any static ressource (CSS, JS, SWF, SVG, PDF, ZIP, Video, Sound, …) : ● Varnish ● Nginx ● CDN far-future jbguerraz@skilld.fr
  • 12.
    Cache What cache backends? http://janezurevc.name/static/bcn_cache jbguerraz@skilld.fr
  • 13.
    Cache What about cacheinvalidation ? ● Cache actions ● Cachetags ● Views content cache ● Expire ● Entity cache flusher jbguerraz@skilld.fr
  • 14.
    Upload & Download Cookiefree domains CDN module in order to use static resources dedicated domain(s) which differ from main domain (for instance, static.mysite.com) $cookie_domain have to be set to main domain in settings.php (for instance www.mysite.com) jbguerraz@skilld.fr
  • 15.
    Upload & Download Aggregates ● JS (Advanced aggregate) Note : ● CSS (Advanced aggregate) Sprites are now obselete, use CSS embeded image instead ● Images & CSS (CSS (for IE, images <32Kb can be embeded image) embeded) ● Images (spritesheets) jbguerraz@skilld.fr
  • 16.
    Upload & Download Parrallelization ● CDN (URL sharding) ● Head.js CSS ● Yepnopejs (Interesting but not yet drupal integrated) JS jbguerraz@skilld.fr
  • 17.
    Browser rendering JS ● Rendering sequence : request as soon as it parse ● JS in footer scope ● Head.js / lab.js (defer) jbguerraz@skilld.fr
  • 18.
    Browser rendering CSS ● Translate3D, no DOM position change ● Pay attention to selectors performances ! No CSS3 selectors (great but slow) ● Avoid blinking > no JS to hide an element on load, hide by CSS and show from JS (perception matters!) jbguerraz@skilld.fr
  • 19.
    Compression What should wecompress ? ● Images ● CSS ● JS ● HTML ● «Any» static file jbguerraz@skilld.fr
  • 20.
    Compression How should wecompress ? Images ● Imageapi optimize ● jpegtran ● advpng jbguerraz@skilld.fr
  • 21.
    Compression How should wecompress ? CSS ● Advagg CSS compress ● CSS Compressor ● faster than CSSTidy ● gzip jbguerraz@skilld.fr
  • 22.
    Compression How should wecompress ? JS ● Advagg JS compress ● JSMin ● faster than JSMin+ ● Gzip jbguerraz@skilld.fr
  • 23.
    Compression How should wecompress ? HTML ● Gzip jbguerraz@skilld.fr
  • 24.
    Compression How should wecompress ? Any static files ● Gzip jbguerraz@skilld.fr
  • 25.
    Drupal pages architecture Howto build a performant page ? USE AS FEW MODULES AS POSSIBLE ! ●Search_api (solr, mongo) ●Panels ●Mongodb ●Panels everywhere ●Redis ●Views ●Memcache ●Rules ●Elysia cron ●Entities jbguerraz@skilld.fr
  • 26.
    Drupal pages architecture Whyhomogeneity maters ? Blocks, panes, pick one ! One single way of managing « blocks » helps using ESI caching for instance; it also makes easier the cache invalidation management for these «blocks». Keep it simple : one API to implement jbguerraz@skilld.fr
  • 27.
    Analyze Browser level ● Gtmetrix.com ● Yslow ● Firebug ● Chrome / Safari developer tools ● Whichloadsfaster.com jbguerraz@skilld.fr
  • 28.
    Analyze Drupal level ● Devel ● Watchdog (redirected to syslog if needed on live server) jbguerraz@skilld.fr
  • 29.
    Analyze PHP level ● FirePHP / ChromePHP ● Xhprof (eventually in addition to Xdebug) ● PHP error log ● PHP-FPM slow logs ● MySQL Slow query logs jbguerraz@skilld.fr
  • 30.
    Analyze System level ● strace ● tcpdump & wireshark jbguerraz@skilld.fr
  • 31.
    Spasibo bolshoe ! Yourtime now, any question ? Wanna get these slides ? Search for jbguerraz on Slideshare ;) jbguerraz@skilld.fr