* pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

46
<?PHP Performance ?> w/APC + Memcached for Windows @FordAntiTrust thaicyberpoint.com thaithinkpad.com thaihi5.com

description

/* pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Transcript of * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Page 1: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

<?PHP Performance ?> w/APC + Memcached

for Windows

@FordAntiTrust

thaicyberpoint.com

thaithinkpad.com

thaihi5.com

Page 2: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Cache Theory ?

Page 3: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Cache Theory ?

Req

Output

Cache

Compute Miss

Hit Store

Check 1

2

3 4

Miss - [Store] - Hit

Page 4: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Type ?

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

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

Fast Slow

Page 5: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

APC ?

Alternative PHP Cache

Page 6: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Code not changes request and Simply Installation

Opcode Cache and Variables Cache

Automatic update w/code changes

Page 7: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Free & Open Source

Page 8: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

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 9: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Normal PHP vs APC

Page 10: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Normal PHP vs APC

Req

Output

• Scanning • Lexing • Parsing • Compilation

Execution

Page 11: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Normal PHP vs APC

Req

Output

Opcode cache

• Scanning • Lexing • Parsing • Compilation

APC hook

APC hook

Execution

Miss

Hit

Store

Page 12: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Why ?

Page 13: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Why ?

http://ralphschindler.com/

Page 14: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Why ?

http://ralphschindler.com/

Page 15: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Installation

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

Page 16: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Installation

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

• Basic PECL

(http://pecl.php.net)

– pecl install apc

Page 17: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Installation

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

• Basic PECL

(http://pecl.php.net)

– pecl install apc

Conf

Page 18: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows
Page 19: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Benchmark

PHP Native PHP w/APC

Concurrency Level 10

Time taken for tests 60 seconds

Complete 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 20: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Memcached

Page 21: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

History

“Developed by Danga Interactive for LiveJournal.com”

Page 22: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Memcached ?

• a distributed memory object caching system with hash table

Page 23: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Memcached ?

• a distributed memory object caching system with hash table

Page 24: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

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 25: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

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 26: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

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 27: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

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 28: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Limits (c/Hash Table)

• Key : max 250 chars.

• Values: max 1MB

Page 29: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

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 30: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows
Page 31: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

“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 32: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Architecture practical ?

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

Page 33: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

APC Memcached

Instance Memcached Instance Memcached

Instance Memcached Instance

Web Server

Master Slave

Cache Reverse

Proxy

I

n

t

e

r

n

e

t

DB

Session

Memcached Instance Memcached

Instance Memcached Instance Memcached

Instance

Files

Memcached Instance Memcached

Instance Memcached Instance CDN/DFS

PHP Web App

Page 34: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Installation & Used

memcached binary packages

• http://labs.northscale.com/memcached-packages/

1. Unzip into a directory of choice

2. Open the shell of your choice (i.e. cmd.exe); cd into the directory

3. Run memcached.exe passing standard arguments.

Sample C:\memcached\memcached.exe -m 128 -p 11211 -l 127.0.0.1 -d

Page 35: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

PHP Session w/Memcached

session.save_handler = files

session.save_path = “C:/Windows/temp”

TO

session.save_handler = memcache

session.save_path = “tcp://server:port “

OR

ini_set('session.save_handler', 'memcache'); ini_set('session.save_path', 'tcp://server:port,tcp://server:port');

Page 36: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

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 37: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

CMS ?

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

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

Page 38: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Tips

Page 39: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Two Level Caching with Zend_Cache

• Tagging System in Files Cache

• Memcached Key used Tagging System

Page 40: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Two Level Caching - Workflow

Req

Output

Memcached

Files Cache Compute

1. Check 2. Miss

3. Store 4. Hit

Page 41: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Implement with Zend_Cache

Page 42: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Used Two Level with Zend_Cache

Page 43: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

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 memcached http://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 44: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Ref. (2)

• PHP: Memcache - Manual

http://th.php.net/manual/en/book.memcache.php

• http://danga.com/memcached/

• http://pecl.php.net/package/memcache

• http://www.socialtext.net/memcached/

Page 45: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Discussion Q&A

Page 46: * pOrt80BKK */ - PHP Day - PHP Performance with APC + Memcached for Windows

Thank you

@FordAntiTrust

http://www.slideshare.net/FordAntiTrust