Performance and Scalability


Published on

Introduction to Drupal Performance tuning.

Published in: Technology

Performance and Scalability

  1. 1. Performance & Scalability Andy Thornton
  2. 2. <ul><li>This is my apology for making my slideshow very text heavy .... </li></ul>An opening Apology However, I added some fun pictures, to hopefully compensate ...  And yes, these slides will be available for download.
  3. 3. Who's this presentation for? Who's this presentation for?
  4. 4. Agenda <ul><li>Capacity Planning and Architecture </li></ul><ul><li>Testing Tools </li></ul><ul><li>Pressflow & Varnish Where are the pinch points? </li></ul><ul><li>PHP </li></ul><ul><li>Caching </li></ul><ul><li>MySQL </li></ul><ul><li>Apache Drupal </li></ul><ul><li>Tweaks Handy Tools Further Reading Questions? </li></ul>
  5. 5. <ul><li>Determine Metrics  »  Test Plan  »  Analyse Results  »  Rinse and repeat </li></ul>Capacity Planning “ However Beautiful the strategy, you should occasionally look at the results ” - Winston Churchill
  6. 6. Testing Tools <ul><li>The Grinder </li></ul><ul><li>A java based load testing tool. </li></ul><ul><li>JMeter </li></ul><ul><li>Java based tool, designed to load test functional behavior and measure performance. </li></ul><ul><li> </li></ul><ul><li>Openload </li></ul><ul><li> </li></ul><ul><li>Soasta </li></ul><ul><li>Leveraging resources from the cloud, Soasta is fast becoming a leader in performance testing. (Acquia Partner) </li></ul><ul><li> </li></ul><ul><li>RRD Tool </li></ul><ul><li>OpenSource high performance data logging and graphing system for time series data. </li></ul>XHProf Profiler for PHP providing some valuable metrics and reporting. l
  7. 7. Acquia &   Soasta <ul><li>CloudTest Dashboard </li></ul>
  8. 8. <ul><li>Drupal Distribution Optimised for performance and scale </li></ul><ul><ul><li>Support for database replication </li></ul></ul><ul><ul><li>Support for Squid or Varnish as reverse proxy caches </li></ul></ul><ul><ul><li>Optimised for MySQL </li></ul></ul><ul><ul><li>Optimised for PHP </li></ul></ul><ul><ul><li>Supported by Acquia </li></ul></ul>Varnish Varnish stores web pages in memory so the web servers don't have to create the same web page over and over again. The web server only recreates a page when it is changed. Reporting from Varnish can provide some good data to help with capacity planning.
  9. 9. Investigating Issues <ul><li>Memory Tapped Out? </li></ul><ul><li>     Apache is generally the culprit, fine tune your config and possibly add more memory. </li></ul><ul><li>CPU is maxing out? </li></ul><ul><li>     PHP is your prime suspect as it's CPU intensive, look into using an Opcode cache. </li></ul><ul><li>Hard Drives are getting Thrashed! </li></ul><ul><li>     Take a look at MySQL, enabling the query cache may help. Fine tune your queries. </li></ul>
  10. 10. Pain  points? Apache PHP MySQL
  11. 11. <ul><li>Accelerators (Opcode Caching) </li></ul><ul><ul><li>Alternative PHP Cache ( APC ) </li></ul></ul><ul><ul><li>eAccelerator </li></ul></ul><ul><ul><li>XCache </li></ul></ul><ul><ul><li>Nusphere PhpExpress </li></ul></ul><ul><ul><li>Zend Accelerator  </li></ul></ul><ul><li>Application Profiling </li></ul><ul><li>Some applications work well on small scale sites, but get bogged down when moved into production. </li></ul><ul><ul><li>Zend Studio </li></ul></ul><ul><ul><li>Komodo </li></ul></ul><ul><ul><li>Eclipse (PDT) </li></ul></ul><ul><ul><li>Xdebug </li></ul></ul><ul><ul><li>Aptanta Studio </li></ul></ul>PHP parses and compiles all code into an intermediary series of opcodes, or more accurately an intermediary series of opcodes for each request. <ul><li>Application Processes </li></ul><ul><li>Creating new processes is a slow operation. CGI applications that fork a new  process on every invocation will run substantially slower the more processes are running. </li></ul><ul><li>Running PHP in multi-threaded mode can improve response times, but consider disabling unused services, for example: </li></ul><ul><ul><li>telnetd, inetd, atd, ftpd, lpd, sambad </li></ul></ul><ul><ul><li>sendmail </li></ul></ul><ul><ul><li>portmap </li></ul></ul><ul><ul><li>xsf, windows manager, xinit, X </li></ul></ul>
  12. 12. <ul><li>Tip 1: I f APC is compiled with mmap support, you can use /dev/zero (usually the default) </li></ul><ul><li>/dev/zero </li></ul><ul><li>apc.mmap_file_mask = /dev/zero </li></ul><ul><li>Andy will explain why /dev/zero is handy... </li></ul><ul><li>Tip 2:  Make sure to give it enough memory. </li></ul><ul><li>Tip 3:  Always measure and adjust as needed. </li></ul>When Drupal Attacks! Sometimes, a cache setup can slow things down!
  13. 13. Andy's Page of Weird Apache Commands <ul><li>Display currently loaded modules </li></ul><ul><li>apachectl -t -D DUMP_MODULES </li></ul><ul><li>Good candidates to remove, mod_cgi, mod_dav, mod_ldap * </li></ul><ul><li>How do I see if that made a difference? </li></ul><ul><li>ab -n 100 -c 5 </li></ul><ul><li>Is the swap file being used? </li></ul><ul><li>vmstat 1 60 </li></ul><ul><li>Runs vmstat every 1 second 60 times </li></ul><ul><li>Decreasing Apache Timeouts </li></ul><ul><li>Normally the default is set to 5 minutes (300 seconds), how about reducing it to 20 seconds? </li></ul><ul><li>TimeOut 20  </li></ul><ul><li>You can do this in your virtual host or server config. </li></ul>
  14. 14. Maxclients Setting in Apache Whats a good way to find the maxclients number?      MaxClients ≈ (Ram - size of all other processes) / (size of the apache process) We are making an educated guess based on dividing the system memory (our physical RAM) by the maximum size of an apache process, with enough wiggle room to have the operating system run smoothly. To find the size of a running process;      ps -ylC apache2 --sort:rss Divide the size by 1024 to get the process size in meg. You can also use pmap,  use top to find the pid, then use;      pmap -x <pid id> Another handy way of seeing how your memory is doing.      free -m  and good old VMSTAT to see if your memory is being paged      vmstat 5 60
  15. 15. Huge List of Apache Tips <ul><li>mod_expires Allows Drupal to send out http expires headers caching all files in the users browsers (~2 weeks) or until a new page is made available. </li></ul><ul><li>This is good for all static files / images / css / javascript. </li></ul><ul><li>Drupal is pre-configured to use mod_exp if it's available. Configure it's use in your .htaccess </li></ul><ul><li># Cache files for 2 weeks after access </li></ul><ul><li>ExpiresActive On </li></ul><ul><li>ExpiresDefault A1209600 </li></ul><ul><li># Don't Cache Dynamic pages </li></ul><ul><li>ExpiresByType text/html A1 </li></ul><ul><li>You don't let Apache cache HTML content as  Drupal's content isn't static. This is why Drupal uses it's own cache. </li></ul>Reduce DNS Lookups You can tell Apache to not perform a DNS lookup on files by their mime type. HostnameLookups Off <Files ~&quot;.(html/cgi)$&quot;>      HostnameLookup On </Files> File Negotiation Be specific when specifying filenames rather than wildcards (when possible). Instead of: DirectoryIndex index Use: DirectoryIndex index.cgi index.php
  16. 16. More Apache Tips ... <ul><li>mod_deflate this allows your web server to compress files before being sending them to the users browser </li></ul><ul><li>AddOutputFilterByType Deflate text/html text/css </li></ul><ul><li>More advanced example: </li></ul><ul><li>SetOutputFilter DEFLATE </li></ul><ul><li># Netscape 4.x has some problems... </li></ul><ul><li>BrowserMatch ^Mozilla/4 gzip-only-text/html </li></ul><ul><li># Netscape 4.06-4.08 have some more problems </li></ul><ul><li>BrowserMatch ^Mozilla/4.0[678] no-gzip </li></ul><ul><li># MSIE masquerades as Netscape, but it is fine </li></ul><ul><li># BrowserMatch bMSIE !no-gzip !gzip-only-text/html </li></ul><ul><li># Don't compress images </li></ul><ul><li>SetEnvIfNoCase Request_URI </li></ul><ul><li>.(?:gif|jpe?g|png)$ no-gzip dont-vary </li></ul>htaccess If you have access to your VirtualHosts in Apache, move your directives out of htaccess and move them in to the VirtualHost for your website.  The reason for this is that Apache loads your virtual host once when started but Drupal searches for htaccess files in multiple directories at runtime. Disable htaccess lookups with: <Directory />    AllowOveride None </Directory>
  17. 17. Webserver <ul><li>Worker </li></ul><ul><li>Uses multiple child processes with many threads each. Each thread handles one connection at a time. </li></ul><ul><li>Prefork </li></ul><ul><li>Uses multiple child processes each with one connection at a time. On many systems it's comparable to worker (in speed) but it uses more memory. Generally, recommended for Drupal due to it's threading model. </li></ul>Alternatives to Apache Nginx (engine-X) Faster than Apache and has more predictable memory usage.  Not as straight forward to setup (rewrite rules for example). LIGHTHTTPD Good performance. Although there has been discussion on the drupal forums as to its ability to cleanly run Drupal 7. Not for the feint of heart. Microsoft Web Matrix Runs Drupal under IIS with PHP, good for Microsoft shops. Apache supports pluggable concurrency modules, called M ulti P rocessing M odules. So which is a good fit for my website?
  18. 18. <ul><li>Drupal does a lot of work in the database, especially for authenticated users and modules, so how can we get the best out of our database? </li></ul>Database Optimisations Enable Query Cache This feature is generally disabled by default. To enable it, assign a value to query_cache_size in your mysql configuration file. [mysqld] query_cache_size =  64M You can query the setting as it's a variable.  SHOW VARIABLES LIKE 'have_query_cache'; You may have to do some testing to find the best value to use. Logging Slow Queries You can instruct MySQL to log all queries that take too long to run, for later analysis. [mysqld] log_slow_queries=/var/log/slow-queries.log log_query_time=5 Leaving off the query time will default it to 10 seconds. Analysing Slow Queries Prepend your query with EXPLAIN and run it for more information, or analyse your query with MAATKIT. EXPLAIN will show which indices are being used, sometime just indexing the table can be a good fix.
  19. 19. <ul><li>Table locking can be a good indicator of problems in your database.  </li></ul>More Database Tips MyISAM and InnoDb Performance wise, they both stand up well, but whats the difference for Drupal? Table Locking MyISAM = Table Level Locking InnoDB   = Row level Locking How do I know if I need to make a change? Take a look at  SHOW STATUS LIKE 'Table%'; Table_locks_immediate 1151552 Table_locks_waited 15324 How do I change a table type? ALTER TABLE accesslog TYPE='InnoDB'; <ul><li>Good Candidates for InnoDB </li></ul><ul><li>Which tables would benefit from changing to use row level locking. </li></ul><ul><ul><li>Cache tables </li></ul></ul><ul><ul><li>Watchdog </li></ul></ul><ul><ul><li>Sessions </li></ul></ul><ul><ul><li>Accesslog </li></ul></ul><ul><li>Tips </li></ul><ul><li>mysqlreport can be automated to show any wait times for transactions. </li></ul><ul><li>Keep your cron short to prune tables regularly if tables get too big. </li></ul><ul><li>You can use the Devel module to identify Query expensive pages in Drupal. </li></ul>
  20. 20. Drupal 6 <ul><li>Caching in Drupal can be enabled through the Performance module in Admin and settings.php.  </li></ul><ul><li>Drupal Ships with 6 Cache tables </li></ul><ul><ul><li>cache </li></ul></ul><ul><ul><li>cache_block </li></ul></ul><ul><ul><li>cache_page </li></ul></ul><ul><ul><li>cache_filter </li></ul></ul><ul><ul><li>cache_menu </li></ul></ul><ul><ul><li>cache_form </li></ul></ul><ul><li>Developer Tip </li></ul><ul><li>When writing your own modules and need to cache data, think about using your own tables. It reduces write contention with Drupal for using the cache tables and doesn't bloat the table with your data. </li></ul><ul><li>Most functions also have a $reset parameter which instructs the function to clear down it's internal cache. </li></ul><ul><li>Consolidate Javascript & CSS </li></ul><ul><li>Enabling these options &quot;smushes&quot; javascript and stylesheets into single files and minifies them.  </li></ul>Drupal Performance Module Options Normal: Drupal bootstraps in phases, when normal is selected it uses just enough phases to load a page from cache. Keeping db queries to a minimum . Aggressive : Completely bypasses loading of all modules. Boot and Exit hooks are never called for cached pages. This means less PHP is parsed since no modules are loaded. There's are also fewer database calls.  Fastpath : Not enabled from the admin panel, this option is enabled from settings.php. The idea is that a call to the file system is faster as there's no ramping up for a database query. This may not scale across load balanced hosting..
  21. 21. Drupal 6 <ul><li>Drupal doesn't store session information for the first anonymous visitor. This is so webcrawlers and spiders don't fill your session tables up. However, these tables can get very large.  </li></ul>Garbage Collection Default value for garbage collection is a little over 2 days, you can also increase the frequency of collection with session.gc_maxlifetime (seconds) session.cache_expire    (minutes) Note : When you adjust maxlifetime, adjust cache_expire to be the same. Tip: As Drupal can serve cached pages to anonymous users and anonymous users don't normally use interactive features of Drupal. How about reducing the time they are logged in or log them out when they close their browser. # 86400 seconds = 24 hours session.cookie_lifetime, 86400 # Logout on browser close session.cookie_lifetime, 0 Pruning Sessions Drupal controls when session start by turning off PHP's session autostart functionality in htaccess. php_value session.auto_start 0 The session table is cleared out when PHP's garbage collection runs. The lifetime of a session record is determined by session.gc.maxlifetime (seconds) Other settings you can experiment with: session.cache_expire session.cache_limiter session.cookie_lifetime session.save_handler session.use_only_cookies session.use_trans_sid
  22. 22. <ul><li>Not a great deal has changed with Drupal 7, there's been a lot of code optimisation and some elements of Pressflow have made their way into the Drupal 7 codebase.  </li></ul><ul><li>However, the following changes are notable. </li></ul><ul><li>Removed </li></ul><ul><ul><li>Caching Mode Option on the performance page </li></ul></ul><ul><ul><li>Throttling Block throttling. </li></ul></ul><ul><li>New Features </li></ul><ul><ul><li>New Performance Logging </li></ul></ul><ul><ul><li>Improved performance on uncached pages </li></ul></ul><ul><ul><li>When logged in, performs fewer queries on path alias lookups </li></ul></ul><ul><ul><li>Tighter Integration with APC </li></ul></ul>Whats Changed in Drupal 7
  23. 23. <ul><li>Google CSE </li></ul><ul><li>Google's custom search service with some impressive features, including branding support and on-the-fly indexing support. Well documented API, but requires some work to get setup initially. </li></ul><ul><li> </li></ul>Alternatives to Search Acquia Search Built upon Lucene and Solr from Apache, hosted service with easy integration with Drupal. All administration tasks are built in to the admin panel. A very powerful alternative to Drupal's built in search.  Apache Solr Java based open source enterprise search platform from the Apache Lucene project.
  24. 24. <ul><li>Theme Optimisation </li></ul><ul><li>Use of sprite maps / minified code / Optimised Images / Google JSAPI / Browser Caching Expiring Content / Reduce HTTP requests / Non Blocking Javascript </li></ul><ul><li>Throttling and Block Caching </li></ul><ul><li>Enabling throttling allows you to turn off modules & blocks when the system starts to get sluggish. You can set the threshold in the admin panel, and determine which modules and blocks to turn off from their respective admin pages. </li></ul><ul><li>Modules </li></ul><ul><li>Cacherouter / Boost / Devel / Authcache </li></ul>More Tweaks <ul><li>Content Delivery Networks </li></ul><ul><li>The capacity sum of strategically placed servers can result in an impressive boost in the number of concurrent users.  </li></ul><ul><ul><li>Akamai Technologies </li></ul></ul><ul><ul><li>Amazon CloudFront </li></ul></ul><ul><ul><li>BitGravity </li></ul></ul><ul><ul><li>CacheFly </li></ul></ul><ul><ul><li>CD Networks </li></ul></ul><ul><ul><li>Windows Azure </li></ul></ul>
  25. 25. <ul><li>Web Load Testing & Page Audit </li></ul><ul><li> </li></ul><ul><li>Open Source Testing Tools </li></ul><ul><li> </li></ul><ul><li>JMeter  </li></ul><ul><li> </li></ul><ul><li>Apache Bench </li></ul><ul><li>mysqlreport </li></ul><ul><li> </li></ul>Handy Tools YSlow (Firebug) Performance Articles (Google Code) Devel Module (Drupal) Ubuntu, Landscape SOASTA CloudTest Client (browser based tools) Boomerang / Firebug / Chrome Dev Tools  /  MAATKIT
  26. 26. <ul><li>The Art Of Capacity Planning </li></ul><ul><li>Web-based companies live or die by the ability to scale their infrastructure to accommodate increasing demand. This book is a hands-on and practical guide to planning for such growth.  </li></ul><ul><li>O'Reilley Media : Amazon </li></ul>Further  Reading High Performance MySQL High Performance MySQL is the definitive guide to building fast, reliable systems with MySQL. This book covers every aspect of MySQL performance in detail and focuses on robustness, security and data integrity. O'Reilley Media : Amazon Even Faster Websites Steve Sounders works on the performance team at Google and has written a couple of great books on performance.  O'Reilley Media : Bio
  27. 27. Questions? <ul><li>[email_address] </li></ul><ul><li>Slides will be made available on   after the presentation. </li></ul>