Improving Mouf performance on Windows

The short story

Don"t have 10 minutes to read the whole article? Here is the short story:

You should an opcode cache, like APC or Zend Optimizer+ (until PHP 5.5) or the native PHP 5.5 opcode cache afterwards. You should also be sure that the cache has enough memory.

Mouf will let you know if APC is short on memory. If you are using Zend Server on Windows (it has great performances), you should edit these parameters:

zend_optimizerplus.max_accelerated_files=20000
zend_optimizerplus.memory_consumption=256

Finally, you should optimize your application's autoloader, using Composer:

php composer.phar dumpautoload -o

Windows performance is particularly poor when it comes to running file_exists and optimizing the autoloader will drastically reduce the number of calls to file_exists by generating a classmap.

These simple actions will help you get the best performance and make your experience with Mouf really enjoyable.

The long story

A lot has been done in Mouf to get the highest possible performance.

And we achieved to have a quite responsive web UI, even with massive projects... or at least, we did that in Linux! If you are running Mouf with Windows, you might have a slow (or very slow) web UI, especially when you are accessing your project's status screen. In this article we will dive into the details of PHP on Windows configuration and see how we can improve Mouf's performances in Windows.

Mouf has a lot of heavy lifting to do. In particular, it has to:

  • scan all PHP files
  • find PHP files containing classes
  • analyze classes and methods annotations

These tasks can become quite long when you start having a large project.

We have performed a real-life benchmark on an existing project: weroom.com

Weroom, it's:

  • 3551 PHP files
  • 200 instances

When Mouf is initialized, the first thing it does is scan all the PHP files and retrieve the validators list.

Here is a result out-of-the-box in different environments:

Mouf's initialization
Linux (Ubuntu 13.04, PHP 5.4.9, APC 3.1.13, Xdebug 2.2.1)5.2 s
WAMPServer 2.4, PHP 5.4.12 no APC, Xdebug 2.2.328.6 s
Zend Server 6.1.0, PHP 5.4.16, Zend Optimizer + (default settings)21.9 s
Zend Server 6.1.0, PHP 5.4.16, Zend Optimizer + (optimized, see below)12.8 s

(Results are measured after purging cache)

A few comments

I had high hopes when testing Zend Server. Indeed, it is one of the only servers that comes with a version of PHP compiled in non-thread safe (faster), with FastCGI (compared to the Apache module used by Wamp that requires a thread-safe implementation of PHP in Windows).

In fact, the FastCGI vs Apache module approach has little to no impact when it comes to Mouf performance.

Customizing settings for Zend Server

By default, Zend Server comes with a PHP Opcode cache that is really small. Therefore, the newly read file will role over the old files and remove them from the cache. You must increase the size of the cache and the number of cachable files.

To do this, edit the optimizerplus.ini file (by default on Windows in C:\Program Files (x86)\Zend\ZendServer\etc\cfg)

Replace those 2 config settings:

zend_optimizerplus.max_accelerated_files=20000
zend_optimizerplus.memory_consumption=256

After this optimisation, we get a response time of 12.8 seconds, still far from the 5.2 seconds I have in Ubuntu.

Profiling Mouf in Windows

Using a profiler, we can see that on some calls, almost 30% of the execution time is spent in calls to file_exist. No, this is not an error. It seems the calls to file_exist are really slow on Windows. And most of these calls are performed in the Composer autoloader (it checks whether class files exists). Hopefully, we can optimize the autoloader by generating a class-map:

php composer.phar dumpautoload -o

Let's see if we manage to go faster:

Mouf's initialization with optimized dumpautoload
Linux (Ubuntu 13.04, PHP 5.4.9, APC 3.1.13, Xdebug 2.2.1)4.2 s
WAMPServer 2.4, PHP 5.4.12 no APC, Xdebug 2.2.321 s
Zend Server 6.1.0, PHP 5.4.16, Zend Optimizer + (optimized, see below)7.2 s

Ha! The optimized autoloader clearly has a positive impact on the performance, especially on Windows machines!

Keeping optimizing Mouf

After this study, we kept working on performance. Especially, a profiling using XDebug showed that most time was spent analyzing PHP files to find classes. We spent some time optimizing this particular function and managed to gain 30% performance on startup.

So by updating Mouf2 to beta6 or better and applying the advices above, you should definitely have a nicer experience. And finally, even after many days spent optimizing the Windows platform, Linux performance still stays better so you really want the best of Mouf, switch to your favorite distribution :)

Hope this helps!