APC – Alternative PHP Cache

A PHP-accelerator is a PHP extension designed to improve the performance of software applications written in the PHP language.

Most PHP accelerators work by caching the compiled p-code (portable code) of PHP-Scripts to avoid the overhead of reparsing and recompiling source code on each request made for example by a stupid bot.

APC is free, open source framework that optimizes PHP intermediate code and caches data and compiled code from the PHP bytecode compiler in shared memory. APC is quickly becoming the de-facto standard PHP caching mechanism as it will be included built-in to the core of PHP starting with PHP 6 <- Great news!

I wrote a long post about my current nginx setup and now I extend it by adding APC-Accelerator

My php5-fpm package comes from Dotdeb, its a good idea to use APC from them too;)
Debian way:

1
aptitude update && aptitude install php5-apc

Fix Cache size for your Accelerator

1
2
3
4
5
cat /etc/php5/conf.d/apc.ini
; configuration for php apc module
extension=apc.so
; cache size
apc.shm_size=256

Restart php5-fpm:

1
/etc/init.d/php5-fpm restart

Now your cache is active! The package provide a apc.php to check the current status and memory allocation.

Cache usage:

General Information (click to resize):

Pos/NegativeHits:

And a little Hit-Bar for your cache – it looks great! – 99.6% of positive hits by a running duration of 9 hours and 25 minutes.

Thats not all, you will get detailed information too!

Let’s prove it!
I start a benchmark test before I enable APC:

1
ab -c5 -n3000 http://zeldor.biz/

Result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Server Software:        nginx
Server Hostname:        zeldor.biz
Server Port:            80
Document Path:          /
Document Length:        33586 bytes
Concurrency Level:      5
Time taken for tests:   9.331 seconds
Complete requests:      3000
Failed requests:        0
Write errors:           0
Total transferred:      101481000 bytes
HTML transferred:       100758000 bytes
Requests per second:    321.52 [#/sec] (mean)
Time per request:       15.551 [ms] (mean)
Time per request:       3.110 [ms] (mean, across all concurrent requests)
Transfer rate:          10621.03 [Kbytes/sec] received

Now enable APC and run the test again – My result was positive!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Server Software:        nginx
Server Hostname:        zeldor.biz
Server Port:            80
Document Path:          /
Document Length:        33586 bytes
Concurrency Level:      5
Time taken for tests:   7.364 seconds
Complete requests:      3000
Failed requests:        0
Write errors:           0
Total transferred:      101481000 bytes
HTML transferred:       100758000 bytes
Requests per second:    407.40 [#/sec] (mean)
Time per request:       12.273 [ms] (mean)
Time per request:       2.455 [ms] (mean, across all concurrent requests)
Transfer rate:          13458.27 [Kbytes/sec] received

My conclusion will be short: Nginx+php5-fpm+APC =Awesome.

0 Responses to “APC – Alternative PHP Cache”


  • No Comments

Leave a Reply