PHP: APC Configuration and Usage Tips and Tricks

This PHP APC guide is divided on four different section:
1. PHP APC Configuration
2. Enable PHP APC Statistics
3. Howto Use PHP APC User Cache
4. PHP APC Performance Testing

4. PHP APC Performance Testing

This is very simple performance test setup just show that how powerful and important APC or any other Opcode Cache is. This test was carried out on CentOS 6.2 box, using APC 3.1.10, PHP 5.3.14 and Nginx 1.2.2.

4.1 Test Files on Web Server

Two files test.php and test_include.php, basic idea is just include test_include.php file 50 000 times and check how long it takes.
test.php

<?php
 
     	$start = microtime(true);
 
        for ($i = 0; $i < 50000; $i++) {
                include('test_include.php');
        }
 
	$end = microtime(true);
 
        echo "Start: " . $start . "<br />";
        echo "End: " . $end . "<br />";
        echo "Diff: ". ($end-$start) . "<br />";
 
?>

test_include.php

<?php
 
     	$t =    "Lorem ipsum dolor sit amet, consectetur
                adipiscing elit. Cras non odio dolor,
                eget fermentum ligula. Nunc et risus at
                augue sollicitudin varius. Suspendisse
                vel mauris lacus, ac egestas mauris.
                Suspendisse dignissim massa id ligula
                scelerisque luctus. Etiam lobortis nisl
                lorem. Integer non velit ante. Nulla
                molestie consequat varius. Proin feugiat,
                tortor nec feugiat vestibulum, nisl odio
                aliquet est, in mollis sapien massa vitae
                ipsum. Cras volutpat nisi at metus
                volutpat at adipiscing ante pulvinar.
                Curabitur semper mauris risus. Aliquam non
                nunc eu nibh tincidunt condimentum aliquet
                eu magna. Sed lobortis consequat lorem a
                pellentesque. Ut vel odio sit amet elit
                porta pellentesque. Aliquam erat volutpat.";
 
?>

4.2 Performance Test Results

APC Cache Disabled:
Start: 1326635645.6191
End: 1326635669.8046
Diff: 24.185463905334

APC Cache Enabled:
Start: 1326635808.3951
End: 1326635810.2877
Diff: 1.8925409317017

Difference is huge. With APC Cache disabled same script took 24.19 seconds and with APC Cache enabled it took only 1.89 seconds.

This PHP APC guide is divided on four different section:
1. PHP APC Configuration
2. Enable PHP APC Statistics
3. Howto Use PHP APC User Cache
4. PHP APC Performance Testing

Pages: 1 2 3 4

Follow If Not True Then False Updates!

Pages: 1 2 3 4

11 Comments

  1. The best thing with learning PHP programming is having a lot of people who are always willing to share their knowledge. One of the best tips on APC configuration is this one that you have made.

  2. I disagree with this:
    “Normally it’s wise cache only “small” files and this value is good to set example to default 1 Mb.”
    Why did you say that? Caching large files is also good because you are avoiding parsing these files.
    Anyway thanks for the tips :)

    • Hi Luis,

      Some cases it might be good increase apc.max_file_size value. But…

      Let’s think very normal example, VPS servers (512Mb, 1Gb, 2Gb) where you have to limit amount of APC memory (e.g. 32Mb, 64Mb 128Mb, 256Mb), and you have hundreds of files, a lot of small files (less than 1Mb) and a few large files (like 25Mb).

      Okay, you cache all files ignoring the size (set very high apc.max_file_size value), then the cache will reach maximum capacity almost constantly and it clean forcefully any entries that haven’t been accessed in the last apc.ttl seconds. This is situation where you use APC, but finally your scripts are loading much slower than without any cache, because you end up the situation where load files from disk, add files to cache and delete files from cache. Which is much heavier than just load files from disk.

      The optimal situation is a situation where your cache is almost fully used, but newer reach maximum capacity (Cache full count number is always zero) and almost all incoming requests are hits (not misses).

      But, if you have enough memory to get all your files cached, then it’s okay to set very high apc.max_file_size and cache everything. :)

  3. I installed APC on a Debian server using apt-get.

    I can’t find the apc.php file in the location you mentioned where else is this file located ?

    • Hi A.Jesin,

      On Debian check /usr/share/doc/php-apc/ path or try following (as root):

      updatedb
       
      locate apc.php
  4. Hello JR,

    I tried everything and still I couldn’t find apc.php. I even tried

    find / -name apc.php

    So finally I downloaded the APC archive from PECL’s website and used the apc.php from it.

    I have uploaded a copy of it to my blog at http://jesin.tk/apc-php-download/ for people who have installed it from the repository.

    • Hi again A.Jesin,

      Strange that Debian doesn’t include apc.php at all, but nice to hear that you got it working and even write helpful post to other Debian users who can’t get apc.php file from Debian’s on pakcages. Thanks!

      • It’s in /etc/php5/conf.d/20-apc.ini

        • Thanks Henrik!

  5. Hi. I am coming from windows and am on fedora 18. What do I use for domain-name here? domainname command states there is no domain name set. i.e. none

    • Hi Chandan Pednekar,

      Do you mean APC statistics url?

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks/Pingbacks

  1. Install Apache/PHP 5.4.6 on Fedora 17/16, CentOS/RHEL 6.3/5.8 - [...] More info about PHP APC from PHP APC Configuration and Usage Tips and Tricks. [...]
  2. Install Nginx/PHP-FPM on Fedora 17/16, CentOS/RHEL 6.3/5.8 - [...] More info about PHP APC from PHP APC Configuration and Usage Tips and Tricks [...]

Pages: 1 2 3 4