63
@manarth Marcus Deglos Performance all teh things!

Performance all teh things

Embed Size (px)

DESCRIPTION

Slidedeck from Drupal Dev Days Dublin 2013

Citation preview

Page 1: Performance all teh things

@manarthMarcus Deglos

Performance all teh things!

Page 2: Performance all teh things

BREAKING NEWSDrupal is Slow!

Page 3: Performance all teh things

JUST HOW SLOW ARE WE TALKING, EXACTLY?

Page 4: Performance all teh things

BENCHMARKINGA single request and response

Page 5: Performance all teh things

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" version="XHTML+RDFa 1.0" dir="ltr" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#"><head profile="http://www.w3.org/1999/xhtml/vocab"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link rel="shortcut icon" href="http://testbed.local/misc/favicon.ico" type="image/vnd.microsoft.icon" /><meta name="Generator" content="Drupal 7 (http://drupal.org)" /> <title>Welcome to Site-Install | Site-Install</title>

Compare:- Plain old HTML- Plain old HTML, with PHP overhead- Drupal

Page 6: Performance all teh things

What HTML?<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" version="XHTML+RDFa 1.0" dir="ltr" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#"><head profile="http://www.w3.org/1999/xhtml/vocab"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link rel="shortcut icon" href="http://testbed.local/misc/favicon.ico" type="image/vnd.microsoft.icon" /><meta name="Generator" content="Drupal 7 (http://drupal.org)" /> <title>Welcome to Site-Install | Site-Install</title>

Drupal 7’s homepage.

Page 7: Performance all teh things

ApacheBench

ab -n 1000 -c 10 $url

Page 8: Performance all teh things

ApacheBench

ab -n 1000 -c 10 $url

1000 requests 10 concurrentrequests

Page 9: Performance all teh things

How fast?

HTML 1776

PHP 1437

Drupal 32

Requests per second(mean, averaged over 3 runs of 1000 requests)

Page 10: Performance all teh things

How fast?

HTML 1776

PHP 1437

Drupal 32

Requests per second(mean, averaged over 3 runs of 1000 requests)

Page 11: Performance all teh things

HOW DO WE MAKE IT FASTER?

Page 12: Performance all teh things

How do we make it faster?

APCMemcach

e

Varnish

Drupal page cache

Drupal block cache

Apache tuning

Custom-compiled

PHP

MySQL tuning

Front-end optimisatio

n

ESI

Page 13: Performance all teh things

How do we make it faster?

APCMemcach

e

Varnish

Drupal page cache

Drupal block cache

Apache tuning

Custom-compiled

PHP

MySQL tuning

Front-end optimisatio

n

ESI

(fairly)simple

rocketscience!

Page 14: Performance all teh things

SIMPLE QUICK WINSAPC, memcache, and Drupal caches

Page 15: Performance all teh things

APerfectCure?

Page 16: Performance all teh things

JUST WHAT IS THIS APC THING ANYWAY?APC: an opcode cache.Welcome to the world of PHP internals

Page 17: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

Page 18: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

Page 19: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

<?phpfunction display_data(array $data) {

$buf = '';foreach ($data as $k=>$v) {

$buf .= sprintf("%s: %s n", $k, $v);}return $buf;

}

Page 20: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

Page 21: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

1 OPEN_TAG <?php2 WHITESPACE3 FUNCTION function3 WHITESPACE3 STRING display_data3 OPEN BRACKET (3 ARRAY array3 WHITESPACE3 VARIABLE $data3 CLOSE_BRACKET )3 WHITESPACE4 OPEN_CURLY {4 WHITESPACE… …………………

Page 22: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

Page 23: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

Tokens are compiled into op-codes

Each op-code is represented by an integer.

For example:

Opcode Opcode numberCONCAT 8DECLARE_CONST 143UNSET_VAR 74

Page 24: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

Page 25: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

zend_compile_file

zend_execute

Page 26: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

APC replaces this

zend_execute

Page 27: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute zend_execute

The compiled opcodes foreach file are cached inmemory.No need to parse again.

Page 28: Performance all teh things

Simplified workflowinclude “foo.php”

Read contents of “foo.php”

Tokenize

Convert tokens to opcodes

execute

APC Summary

You do not need to change your codeAPC cache is not shared between PHP-FPM workersApache + mod php is better than PHP-FPM/CGI.

Page 29: Performance all teh things

Installing APC$ sudo apt-get install php-apcReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following NEW packages will be installed: php-apc0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/79.2 kB of archives.After this operation, 233 kB of additional disk space will be used.Selecting previously unselected package php-apc.(Reading database ... 68234 files and directories currently installed.)Unpacking php-apc (from .../php-apc_3.1.7-1_i386.deb) ...Processing triggers for libapache2-mod-php5 ... * Reloading web server config apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName [ OK ]Setting up php-apc (3.1.7-1) ...$ sudo apache2ctl restart

Page 30: Performance all teh things

Installing APC$ sudo apt-get install php-apcReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following NEW packages will be installed: php-apc0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/79.2 kB of archives.After this operation, 233 kB of additional disk space will be used.Selecting previously unselected package php-apc.(Reading database ... 68234 files and directories currently installed.)Unpacking php-apc (from .../php-apc_3.1.7-1_i386.deb) ...Processing triggers for libapache2-mod-php5 ... * Reloading web server config apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName [ OK ]Setting up php-apc (3.1.7-1) ...$ sudo apache2ctl restart

Use `sudo` if you need moreprivileges than your useraccount has.

Page 31: Performance all teh things

Installing APC$ sudo apt-get install php-apcReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following NEW packages will be installed: php-apc0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/79.2 kB of archives.After this operation, 233 kB of additional disk space will be used.Selecting previously unselected package php-apc.(Reading database ... 68234 files and directories currently installed.)Unpacking php-apc (from .../php-apc_3.1.7-1_i386.deb) ...Processing triggers for libapache2-mod-php5 ... * Reloading web server config apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName [ OK ]Setting up php-apc (3.1.7-1) ...$ sudo apache2ctl restart

Package manager commandmay vary according to O/S.Redhat/CentOS usually uses`yum install` instead.

Page 32: Performance all teh things

Installing APC$ sudo apt-get install php-apcReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following NEW packages will be installed: php-apc0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/79.2 kB of archives.After this operation, 233 kB of additional disk space will be used.Selecting previously unselected package php-apc.(Reading database ... 68234 files and directories currently installed.)Unpacking php-apc (from .../php-apc_3.1.7-1_i386.deb) ...Processing triggers for libapache2-mod-php5 ... * Reloading web server config apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName [ OK ]Setting up php-apc (3.1.7-1) ...$ sudo apache2ctl restart

The name of the packagevaries according to distro,for example `php-pecl-apc`in some variations.

Page 33: Performance all teh things

Installing APC$ sudo apt-get install php-apcReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following NEW packages will be installed: php-apc0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/79.2 kB of archives.After this operation, 233 kB of additional disk space will be used.Selecting previously unselected package php-apc.(Reading database ... 68234 files and directories currently installed.)Unpacking php-apc (from .../php-apc_3.1.7-1_i386.deb) ...Processing triggers for libapache2-mod-php5 ... * Reloading web server config apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName [ OK ]Setting up php-apc (3.1.7-1) ...$ sudo apache2ctl restart

Restart Apache for thechange to take effect.

Page 34: Performance all teh things

Verifying APC is running$ php -i |grep –i apcAdditional .ini files parsed => /etc/php5/cli/conf.d/apc.ini,apcAPC Support => disabledAPC Debugging => Disabledapc.cache_by_default => On => Onapc.canonicalize => On => Onapc.coredump_unmap => Off => Offapc.enable_cli => Off => Offapc.enabled => On => Onapc.file_md5 => Off => Offapc.file_update_protection => 2 => 2apc.filters => no value => no valueapc.gc_ttl => 3600 => 3600

Page 35: Performance all teh things

Verifying APC is running$ php -i |grep –i apcAdditional .ini files parsed => /etc/php5/cli/conf.d/apc.ini,apcAPC Support => disabledAPC Debugging => Disabledapc.cache_by_default => On => Onapc.canonicalize => On => Onapc.coredump_unmap => Off => Offapc.enable_cli => Off => Offapc.enabled => On => Onapc.file_md5 => Off => Offapc.file_update_protection => 2 => 2apc.filters => no value => no valueapc.gc_ttl => 3600 => 3600

Use `php –i` to show the PHPenvironment configuration

Page 36: Performance all teh things

Verifying APC is running$ php -i |grep –i apcAdditional .ini files parsed => /etc/php5/cli/conf.d/apc.ini,apcAPC Support => disabledAPC Debugging => Disabledapc.cache_by_default => On => Onapc.canonicalize => On => Onapc.coredump_unmap => Off => Offapc.enable_cli => Off => Offapc.enabled => On => Onapc.file_md5 => Off => Offapc.file_update_protection => 2 => 2apc.filters => no value => no valueapc.gc_ttl => 3600 => 3600

Use `grep` to filter for configrelated just to APC. The `-i`flag is to ignore case.

Page 37: Performance all teh things

Verifying APC is running$ php -i |grep –i apcAdditional .ini files parsed => /etc/php5/cli/conf.d/apc.ini,apcAPC Support => disabledAPC Debugging => Disabledapc.cache_by_default => On => Onapc.canonicalize => On => Onapc.coredump_unmap => Off => Offapc.enable_cli => Off => Offapc.enabled => On => Onapc.file_md5 => Off => Offapc.file_update_protection => 2 => 2apc.filters => no value => no valueapc.gc_ttl => 3600 => 3600

Hooray!

Page 38: Performance all teh things

Verifying via the browser

Link to PHPconfiguration

Admin » Reports » Status

Page 39: Performance all teh things

Verifying via the browser

Page 40: Performance all teh things

Benchmarking time!

Page 41: Performance all teh things

How fast?

Requests per second(mean, averaged over 3 runs of 1000 requests)

No APC 3.75

APC 3.44

Page 42: Performance all teh things

Wait, WHAT?!

Page 43: Performance all teh things

How fast?

Requests per second(mean, averaged over 3 runs of 1000 requests)

No APC 3.75

APC 3.44

I cheated.This is what happens whenyou don’t assign enoughmemory to APC.

Page 44: Performance all teh things

How fast?

Requests per second(mean, averaged over 3 runs of 1000 requests)

No APC 3.75

APC 3.44

APC 12.49

Page 45: Performance all teh things

MemcacheWhat does it do?

Page 46: Performance all teh things

cache_get

if ($cache = cache_get('image_styles', 'cache')) { $styles = $cache->data;}else { // Do a load of work // And look up all sorts of things in the SLOW db.}

Page 47: Performance all teh things

cache_get

if ($cache = cache_get('image_styles', 'cache')) { $styles = $cache->data;}else { // Do a load of work // And look up all sorts of things in the SLOW db.}

cache_get is pluggable.It’s a key-value store.By default it uses the DB.

Page 48: Performance all teh things

cache_get in the DB.

cache_get('image_styles', 'cache’);

SELECT * from cache WHERE cid='image_styles’;

Page 49: Performance all teh things

A quick summaryMemcache moves load from the DB to

memcache.Memcache stores data in memory (unlike DB

which stores it on disk). Memory is faster.Memcache is non-persistent. Don’t store data

you’re not prepared to lose.

Page 50: Performance all teh things

Installing Memcache$ sudo apt-get install memcached php5-memcacheReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following package was automatically installed and is no longer required: libmemcached6Use 'apt-get autoremove' to remove them.Suggested packages: libcache-memcached-perl libmemcachedThe following NEW packages will be installed: memcached php5-memcache0 upgraded, 2 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/121 kB of archives.After this operation, 375 kB of additional disk space will be used.Selecting previously unselected package memcached.(Reading database ... 68214 files and directories currently installed.)……Setting up memcached (1.4.13-0ubuntu2) ...Starting memcached: memcached.Setting up php5-memcache (3.0.6-1) ...$ sudo /etc/init.d/memcached startStarting memcached: memcached.

Page 51: Performance all teh things

Installing Memcache$ sudo apt-get install memcached php5-memcacheReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following package was automatically installed and is no longer required: libmemcached6Use 'apt-get autoremove' to remove them.Suggested packages: libcache-memcached-perl libmemcachedThe following NEW packages will be installed: memcached php5-memcache0 upgraded, 2 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/121 kB of archives.After this operation, 375 kB of additional disk space will be used.Selecting previously unselected package memcached.(Reading database ... 68214 files and directories currently installed.)……Setting up memcached (1.4.13-0ubuntu2) ...Starting memcached: memcached.Setting up php5-memcache (3.0.6-1) ...$ sudo /etc/init.d/memcached startStarting memcached: memcached.

memcached is the memcache server. It’s a standalone program, completely separate from PHP.

Page 52: Performance all teh things

Installing Memcache$ sudo apt-get install memcached php5-memcacheReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following package was automatically installed and is no longer required: libmemcached6Use 'apt-get autoremove' to remove them.Suggested packages: libcache-memcached-perl libmemcachedThe following NEW packages will be installed: memcached php5-memcache0 upgraded, 2 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/121 kB of archives.After this operation, 375 kB of additional disk space will be used.Selecting previously unselected package memcached.(Reading database ... 68214 files and directories currently installed.)……Setting up memcached (1.4.13-0ubuntu2) ...Starting memcached: memcached.Setting up php5-memcache (3.0.6-1) ...$ sudo /etc/init.d/memcached startStarting memcached: memcached.

php5-memcache is the PHP extension that provides functions for PHP to exchange data with a memcache server.

Page 53: Performance all teh things

Installing Memcache$ sudo apt-get install memcached php5-memcacheReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following package was automatically installed and is no longer required: libmemcached6Use 'apt-get autoremove' to remove them.Suggested packages: libcache-memcached-perl libmemcachedThe following NEW packages will be installed: memcached php5-memcache0 upgraded, 2 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/121 kB of archives.After this operation, 375 kB of additional disk space will be used.Selecting previously unselected package memcached.(Reading database ... 68214 files and directories currently installed.)……Setting up memcached (1.4.13-0ubuntu2) ...Starting memcached: memcached.Setting up php5-memcache (3.0.6-1) ...$ sudo /etc/init.d/memcached startStarting memcached: memcached.

There’s another PHP extension called php5-memcached, which does the same thing.PHP also has duplicate modules!

Page 54: Performance all teh things

Installing Memcache$ sudo apt-get install memcached php5-memcacheReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following package was automatically installed and is no longer required: libmemcached6Use 'apt-get autoremove' to remove them.Suggested packages: libcache-memcached-perl libmemcachedThe following NEW packages will be installed: memcached php5-memcache0 upgraded, 2 newly installed, 0 to remove and 8 not upgraded.Need to get 0 B/121 kB of archives.After this operation, 375 kB of additional disk space will be used.Selecting previously unselected package memcached.(Reading database ... 68214 files and directories currently installed.)……Setting up memcached (1.4.13-0ubuntu2) ...Starting memcached: memcached.Setting up php5-memcache (3.0.6-1) ...$ sudo /etc/init.d/memcached startStarting memcached: memcached.

Start the memcache server

Page 55: Performance all teh things

Drupal config for MemcacheDownload the memcache module.Add the configuration to settings.php.

Page 56: Performance all teh things

settings.php changes/** * Memcache configuration. */define('MEMCACHE_PATH', 'sites/all/modules/contrib/memcache/memcache.inc'); // Include the base cache class that memcache extends.include_once('./includes/cache.inc');// Include the memcache files.include_once(MEMCACHE_PATH); // Optionally configure a prefix; most sites won't need this.# $conf['memcache_key_prefix'] = 'foo';

// Declare memcache as a potential backend.$conf['cache_backends'][] = MEMCACHE_PATH; // Set the default cache to use memcache.$conf['cache_default_class'] = 'MemCacheDrupal';

// Form-cache must use non-volatile storage.$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

Page 57: Performance all teh things

How fast?

Requests per second(mean, averaged over 3 runs of 1000 requests)

APC 12.49

Memcache 29.06

Page 58: Performance all teh things

How fast?

Page cache 55

+ APC 190

+ APC +memcache 193

Requests per second(mean, averaged over 3 runs of 1000 requests)

APC 12.49

Page 59: Performance all teh things

Varnish…live demo!(uh oh…)

Page 60: Performance all teh things

A QUICK SUMMARY

Page 61: Performance all teh things

Speeding up the snail• APC and memcache are quick wins.• Turn on page-caching and block-caching• Use Varnish for static assets• Use Varnish + ESI to cache authenticated users

Page 62: Performance all teh things

Thanks for coming!

Blog: http://deglos.com Email: [email protected] Slides: http://slideshare.net/manarth Twitter: @manarth

Questions?

Page 63: Performance all teh things

Build websites Develop modules Offer technical consultancy

http://techito.co.uk/