Tune a MediaWiki using Webpagetest and APC Cache

From Organic Design wiki
Procedure.svg Tune a MediaWiki using Webpagetest and APC Cache
Organic Design procedure

Why Cache?

Employing a PHP opcode cache like APC or XCache allows you to make a trade-off between memory use and load time.

In a MediaWiki with lots of queries, each query is executed once and forgotten, then executed again next time it is called. If you cache the result of that then you can just present the result immediately from memory.

Obviously the cache also has to check to see if anything has changed before it repeats the last result.

Every time a query is cached an extra amount of memory is used in every instance of apache2 on the server, hence there is a tradeoff.

The two main caches used in single server installs are APC and Xcache.

They are similar in speed. XCache is the newer variant. APC has official PHP support.

The downside of both apart from memory use is that both can cause segmentation faults in some cases.

Webpagetest

Webpagetest is a free site that give you a graphical representation of all the elements that are loading on the URL that you specify.

It can be specified for location and browser type.

You can test several pages on the site, before and after any changes, and report on that.

Using Webpagetest

It is best to do a test for say three pages on your site including the main page and pages that have big queries.

You can save the results by bookmarking or saving.

  1. First you can look for badly optimized graphics and fix those.
  2. Look then for JavaScript and css that is unnecessarily loading - maybe the extension is not being used and more, or two instances are being loaded. Calls to js or css files outside the file system (i.e. on the Web) are especially laggy. Scripts can be omitted or minified as appropriate.

Using APC Cache to Cache PHP Calls

Next you want to cache expensive PHP loads and queries

Set Up APC Cache

Firstly, install APC cache. You will also need to restart Apache every time you make a configuration change. In Debian or Ubuntu:

sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart


The config file is /etc/php5/conf.d/apc.ini, and its only default entry is extension=apc.so

Change it to read like this:

extension=apc.so
apc.enabled=1
apc.ttl=3600
apc.user_ttl=3600
apc.shm_size=32
apc.cache_by_default=1


Now find your apc.php file:

cd /
find -name apc.php | grep apc


Copy apc.php to the root of your webserver, or some other location where you can navigate to it, and navigate there.

Tune APC Cache

When you navigate to Apc.php having implemented the above cache settings, restarted Apache, and visited your website a couple of times, you will be greeted with a front page like this. It will show a 32MB cache (a useful starting size which you can change at any time), probably with fragmentation. This is because you have told the APC cache to be on by default, so absolutely everything is being cached.


Apc-fragmented.jpg


Click the second tab "System Cache Entries" and expand:


Apc-p1.jpg


Sort by Hits, descending. There will now be a list of files with the same number of hits, and another list below with a slightly smaller number. Try caching that first list, then the list with fewer hits as well, checking load times with Webpagetest all the while.


You do this by changing apc.cache_by_default to 0, thereby making files not cached by default, then adding apc.filters as below. This is the list of hits referred to above, it will be much larger than the example. Use the regular expression syntax shown, APC will cache only these files.

extension=apc.so
apc.enabled=1
apc.ttl=3600
apc.user_ttl=3600
apc.shm_size=32
apc.cache_by_default=0
apc.filters="+HTMLFileCache.php,+DT_XMLParser.php,+MessagesEn.php,+Names.php,+Language.php,+Parser.php"


Once you have restarted Apache, and hard-refreshed a couple of pages on your site and APC.php, your pie will look like this:


Apc-32.jpg


Finally you can set your apc.shm_size to slightly larger than the amount used by the cache.

apc.shm_size=8


If your site contains only static content and there are no dynamic queries you can speed the load time up even further by setting apc.stat to 0, thereby telling the cache not to check for updated entries.


Once you have completed this procedure you can go ahead and do a load test (see link below).

See Also