PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

38
PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com

Transcript of PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Page 1: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

PHP Performance w/APC + Memcached

@FordAntiTrustthaicyberpoint.comthaithinkpad.comthaihi5.com

Page 2: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Cache Theory ?

Page 3: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Cache Theory ?

Req

Output

Cache

ComputeMiss

HitStore

Check1

2

34

Miss - [Store] - Hit

Page 4: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Type ?

http://ralphlosey.files.wordpress.com/2007/06/ram.jpg

http://www.pctechguide.com/images/31HardDrive.jpg

Fast Slow

Page 5: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

APC ?

Alternative PHP Cache

Page 6: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Code not changes request

Opcode Cache and Variables Cache

Automatic update w/code changes

Page 7: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Free & Open Source

Page 8: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Simply Installation

Page 9: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

PHP Flow

• Scanning and Lexing (Plain Text to Tokens)

• Parsing (Tokens to Expressions)

• Compilation (Translated Expressions to Opcode)

• Execution (Opcode stacks are processed)

Req

Output

• Scanning • Lexing • Parsing • Compilation

Execution

Page 10: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Normal PHP vs APC

Page 11: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Normal PHP vs APC

Req

Output

• Scanning • Lexing • Parsing • Compilation

Execution

Page 12: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Normal PHP vs APC

Req

Output

Opcodecache

• Scanning • Lexing • Parsing • Compilation

APC hook

APC hook

Execution

Miss

Hit

Store

Page 13: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Installation

• Basic PECL (http://pecl.php.net)– pecl install apc

• Ubuntu– apt-get install php5-apc

• Windows (http://pecl4win.php.net)– extension=php_apc.dll

Confextension=apc.soapc.shm_segments=1apc.shm_size=256apc.optimization=0apc.num_files_hint=2048apc.ttl=3600apc.user_ttl=3600apc.enable_cli=1apc.max_file_size=1M

Page 14: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.
Page 15: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

BenchmarkPHP Native PHP w/APC

Concurrency Level 10Time taken for tests 60 secondsComplete requests 298 914

Total transferred 643,149 bytes 1,962,675 bytes

HTML transferred 516,971 bytes 1,582,035 bytes

Requests per second 4.91 [#/sec] (mean) 15.21 [#/sec] (mean)

Time per request 2035.405 [ms] (mean) 657.623 [ms] (mean)

Time per request(mean, across all concurrent requests)

203.541 [ms] 65.762 [ms]

Transfer rate 10.35 [KB/s] received 31.88 [KB/s] received

Page 16: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Memcached

Page 17: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

History

“Developed by Danga Interactive for LiveJournal.com”

Page 18: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Memcached ?

• a distributed memory object caching system with hash table

Page 19: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Memcached ?

• a distributed memory object caching system with hash table

Page 20: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Memcached ?

• a distributed memory object caching system with hash table

• a generic two stage hashing system (clients and servers). Clients are written for specific application languages. Server is written in C

Page 21: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Memcached ?

• a distributed memory object caching system with hash table

• a generic two stage hashing system (clients and servers). Clients are written for specific application languages. Server is written in C

Page 22: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Memcached ?

• a distributed memory object caching system with hash table

• a generic two stage hashing system (clients and servers). Clients are written for specific application languages. Server is written in C

• 85% of top 20 sites use memcached; 50%+ of top 5k sites uses Memcached

Page 23: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Memcached ?

• a distributed memory object caching system with hash table

• a generic two stage hashing system (clients and servers). Clients are written for specific application languages. Server is written in C

• 85% of top 20 sites use memcached; 50%+ of top 5k sites uses Memcached

Page 24: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Why Memcached ?

• Scale-out– Machine– Process (Use different ports)

• Memory faster than Disk• Allows for efficient use of your database • Can utilize existing network

– Uses non-blocking network I/O (TCP/IP)• Very flexible• Client libraries in all major languages

Page 25: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Limits (c/Hash Table)

• Key : max 250 chars.• Values: max 1MB

Page 26: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.
Page 27: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

“We use more than 800 servers supplying over 28 terabytes of memory to our users.”

“total throughput achieved is 300,000 UDP req/s”

by Paul Saab Saturday, December 13, 2008 at 2:43am

• 8-core machines• 20-30% CPU usage.• scale memcached to 8 threads on an 8-core system• moved to UDP for get operations to reduce network traffic

http://www.facebook.com/note.php?note_id=39391378919

Page 28: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Architecture practical ?

http://barry.files.wordpress.com/2007/04/new-servers-back-04-2007.jpg

Page 29: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Architecture practical ?

http://blog.thinkdiff.net/?p=419

“designing & implementing scalable applications with memcached and mysql”

Page 30: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.
Page 31: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Architecture practical ?

http://blog.thinkdiff.net/wp-content/uploads/2009/02/memcache_22-02-2009.png

Page 32: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Coding ? (in PHP)

/* OO API */$memcacheObj = new Memcache;$memcacheObj->connect('memcache_host', 11211);

/* set */$memcacheObj->set('key', 'data', MEMCACHE_COMPRESSED, 50);/ * get w/single key */$var = $memcacheObj->get('key');/ * get w/multiple key */$var = $memcacheObj->get(array('key1', 'key2'));

$memcacheObj->close();

Page 33: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

CMS ?

• Drupal has Memcache API and Integrationhttp://drupal.org/project/memcache

• Wordpress has object cachehttp://mohanjith.net/blog/2008/10/using-memcached-with-wordpress-object-cache.html

Page 34: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

code optimization is best

Page 35: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

DiscussionQ&A

Page 36: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Ref.• Improving PHP Application Performance with APC

http://www.slideshare.net/vortexau/improving-php-application-performance-with-apc-presentation

• PHP Accelerators : APC vs Zend vs XCache with Zend Framework

http://blog.digitalstruct.com/2007/12/23/php-accelerators-apc-vs-zend-vs-xcache-with-zend-framework/

• Scaling with memcachedhttp://www.slideshare.net/acme/scaling-with-memcached

• Gear6 and Scaling Website Performance: Caching Session and Profile Data with Memcached

http://www.slideshare.net/gear6memcached/gear6-and-scaling-website-performance-caching-session-and-profile-data-with-memcached

Page 37: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Ref. (2)

• PHP: Memcache - Manualhttp://th.php.net/manual/en/book.memcache.php

• http://danga.com/memcached/• http://pecl.php.net/package/memcache• http://www.socialtext.net/memcached/

Page 38: PHP Performance w/APC + Memcached @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com.

Thank you@FordAntiTrust

http://www.slideshare.net/FordAntiTrust