PHP APC Performance Tests with Apache ab

Today I ran some quick tests with PHP APC using Apache's ab benchmarking tool. The goal was to see how much of a performance gain I could see between PHP APC disabled, then enabled.

So what is op-code caching?

From wikipedia.org: A PHP accelerator is an extension designed to boost the performance of software applications written using the PHP programming language. Most PHP accelerators work by caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request (some or all of which may never even be executed). For best performance, caching is to shared memory with direct execution from the shared memory and the minimum of memory copying at runtime. A PHP accelerator typically reduces server load and increases the speed of PHP code anywhere from 2-10 times, depending on factors such as the inherent execution time of the PHP application and the percentage of source code actually executed on a given request. While a code optimizer may even slow down overall performance when used in isolation, it can provide an additional performance boost when coupled with a code cache as the optimization effort is performed just once.

The Testing System
The system is just dev box I have here in the office. These are the stats:

Processor
P4 3Ghz
Memory
1 Gb
OS
Ubuntu Feisty Fawn
Apache
2.2.3
PHP
5.2.1
Target Web App
Drual 5, fresh home page


Without PHP APC: 7.86 Requests per second
Here is the output of ab command with PHP APC disabled.

# ab -n 50 http://localhost/drupal5/
This is ApacheBench, Version 2.0.40-dev  apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        Apache/2.2.3
Server Hostname:        localhost
Server Port:            80

Document Path:          /drupal5/
Document Length:        5144 bytes

Concurrency Level:      1
Time taken for tests:   6.362471 seconds
Complete requests:      50
Failed requests:        0
Write errors:           0
Total transferred:      284150 bytes
HTML transferred:       257200 bytes
Requests per second:    7.86 [#/sec] (mean)
Time per request:       127.249 [ms] (mean)
Time per request:       127.249 [ms] (mean, across all concurrent requests)
Transfer rate:          43.54 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:   125  126   3.5    125     143
Waiting:      125  126   3.5    125     143
Total:        125  126   3.5    125     143

Percentage of the requests served within a certain time (ms)
  50%    125
  66%    126
  75%    126
  80%    126
  90%    131
  95%    134
  98%    143
  99%    143
 100%    143 (longest request)

With PHP APC: 52.92 Requests per second
Here is the output of the Apache ab command with PHP APC enabled.

# ab -n 50 http://localhost/drupal5/
This is ApacheBench, Version 2.0.40-dev  apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        Apache/2.2.3
Server Hostname:        localhost
Server Port:            80


Document Path:          /drupal5/
Document Length:        5144 bytes

Concurrency Level:      1
Time taken for tests:   0.944904 seconds
Complete requests:      50
Failed requests:        0
Write errors:           0
Total transferred:      284150 bytes
HTML transferred:       257200 bytes
Requests per second:    52.92 [#/sec] (mean)
Time per request:       18.898 [ms] (mean)
Time per request:       18.898 [ms] (mean, across all concurrent requests)
Transfer rate:          293.15 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:    18   18   0.3     18      19
Waiting:       18   18   0.0     18      18
Total:         18   18   0.3     18      19

Percentage of the requests served within a certain time (ms)
  50%     18
  66%     18
  75%     18
  80%     18
  90%     19
  95%     19
  98%     19
  99%     19
 100%     19 (longest request)

In Conclusion
As you can see the performance gain in terms of requests per second went up over 600% due to op-code caching that PHP-APC provides.

The downside to PHP APC, and other op-code apps, is that they will cause segmentation faults, although not often. I came across this article from 2bits.com. They have posted a script which will monitor the Apache logs and restart the service when a segmentation fault happens.

In my opinion, that's a small price to pay for such high performance gains.