Theory vs. Practice
Diagnosis is not the end, but the beginning of practice.
› How high can PHP "fly"? 88 times higher than HipHop!
PHP was not really on my agenda in 2009 when I decided to create G-WAN. Our applications are C programs, so G-WAN has first supported C scripts. But since the very beginning, people came and asked for other languages, including PHP scripts.

I have had difficulties to integrate Java or C# but never in the extend of PHP – despite the fact that PHP was supporting direct interfaces with C (making it easier, at least in theory, to use from C).
Friends were bogging me about PHP, and the frustration was not calmed by the official PHP distribution which cannot be both compiled and used in thread-safe mode (that's either one or the other).
Finally, that's the email of Tak, a long-time G-WAN friend, which gave me the idea that made it possible for G-WAN to support PHP.
The down-to-earth idea (the prehistoric CGI interface)
Tak asked me how to run an extern process from a G-WAN servlet and how to have G-WAN serve the output to remote clients.
Answering Tak's question, I started to wonder why this strategy could not be used to run PHP. After all, many PHP servers run an instance of PHP per request in order to achieve the concurrency that PHP does not support natively (not being thread-safe, it crashes).
At first glance, at least if you don't check the facts yourself, CGI sounds like a poor alternative to FastCGI – but in the real-life, with a multicore G-WAN, involving IPC (inter-process communication) via sockets (like FasCGI does it to query a persistent PHP process), is often a waste of time (CGI runs much faster, despite having to continuously reload the PHP process for each request!).
Why, how?
- In normal times: because the overheads of the PHP script to run might be smaller than the fastCGI overhead.
- In a multithread environment: because the PHP script will crash the thread-unsafe PHP runtime.
The prospect of such a primitive test, as compared to the many many many hours invested (and lost) so far to find how to directly interface PHP with a multi-threaded program without making the PHP runtime crash, was considerably more attractive than anything I had seen in the past.
After all, G-WAN could very well support PHP better than other servers, even with the overhead of the PHP runtime reloaded continuously if its runtime is a mere reference to a pre-loaded shared library.
Compare this to PHP-FPM, the interface officially recommended for PHP servers. As the PHP FastCGI server is extremely slow you won't have a chance to perform and you can only scale horizontally (using hundreds of servers instead of one).
That started to look almost good: the design is atrocious, the means is crude, but, hey, this could work – and for the first time, knowing for sure if a PHP interface works would take very little of my time.
Animated by the pleasure of anticipating a likely result (G-WAN is serving scripts faster than others because it's a faster server), I prepared the stub code and an ab.c test, to see how well this very basic idea would work with the appropriate amount of threading.
Using PHP as a script engine
I have read that PHP runs best in Apache. I remember that on the (now defunct) G-WAN forum, it was tested with Nginx + PHP-FPM at 17k requests per second. In contrast, my attempts to run G-WAN with the PHP Embed library, using one single PHP thread (to avoid crashing the PHP runtime), were running at 170k requests per second.
I was ready to consider as a success anything (a) scaling with the number of worker threads and (b) surviving the [1-1,000] concurrency range with ab.c using weighttp and the 6 CPU Cores of my test machine.
But this unsophisticated strategy proved to run much better than expected (with a default Debian 6.0 64-bit PHP 5.3.3 installation):

G-WAN hello.php
First (good) surprise, PHP did not die despite being reloaded and re-initialized continuously during this 36 minutes and 57 seconds test.
The reported CPU & RAM usage were very low because the (many) PHP extern processes are not measured.
Third, the performance and the scalability were extraordinarily good, with PHP reaching 630k requests per second at concurrency 620.
Apache or Nginx "hello world" C modules can't even to do that!
The three much-slower requests at 151k requests per second are most likely to be due to an internal lock in the PHP runtime (the code is reloaded and recompiled for every request). This would be waved at the same time performance would be boosted at around 800k requests per second... if the PHP runtime was used properly as a thread-safe library.
Just keep in mind that I have never heard of any server able to make PHP fly at G-WAN's 151k RPS lowest (see this PHP frameworks comparison which tops at 7.3k RPS with Nginx and the APC cache, and a skinny 1.4k RPS without caching).
And this G-WAN lowest is accidental – G-WAN's PHP average flies at 613k RPS, a whooping 438x faster than the best-performing PHP framework (~580x faster if G-WAN was using PHP properly). As a simple hello world would need five hundred times less servers, guess what it would be with more substancial jobs!
If I was a PHP user then I would start to question the reasons of my loyalty towards people who provide so much inferior solutions: are they really working in your interest? Or is it that they are not competent? Either case, does it make sense to work with them since it is clear that they refuse to consider what would let them do better?
G-WAN C, C++, C#, D, Java and Scala scripts do better (in the 800k range), but remember that here G-WAN is using PHP in the worse possible way (by invoking it as an extern process, and therefore not caching compiled code). In contrast, the other programming languages offer direct interfaces that allow to cache the pre-compiled code and to greatly limit the overhead involved in the transfer of their output – both things that are not allowed to PHP here.
Does Facebook's HipHop do any better?
Someone asked me the question. HipHop translates PHP into C++ which can then be compiled natively (instead of running byte-code in an interpreter). And the embedded HipHop server relies on libev which is probably faster than Apache. Out of curiosity, I installed the Ubuntu 12.04 pre-built package available from GitHub.
Unfortunately, after installing it, hphp is not available in the PATH. Reading further, I found this HipHop Wiki link saying that you have to define environment variables with the HipHop installation PATH.
But I could not find where the debian package was installed: looking for hphp, hphpi or program (the commands used in the "Running HipHop" section) results in no matching files found.
Is there any way to actually use the animal? Is this Wiki really aimed at helping people use HipHop? I don't know.
Other than that, I have found benchmarks claiming that HipHop provides a 5x boost as compared to plain old PHP:
Framework Req/sec Speed
------------ ------- ------
Regular PHP 15.10 100%
PHP + APC 46.90 310%
HipHop 77.01 510%
As we have seen that G-WAN boosts PHP by a factor 438, 88x more than HipHop, and doing so with the 'regular PHP' framework, I am not sure how or why HipHop, a subset of the regular PHP, should be used.
A PHP app. server which Scales?
The PHP interface used by G-WAN is 10 lines of code long. The request parameters (and any POST/PUT entity) are properly passed to PHP. But there's still code to write to have G-WAN pass its 'environment' variables to PHP. While trivial to implement, this will take time and there are surely other things that should be done that I ignore as I am not a PHP developer. This means that the current state of things, while fully-compliant since it does run the original runtime, is probably not (yet) a fully-featured PHP framework.
But, hey, never in history PHP has been seen flying so high... and for the first time PHP was given a chance to scale on multicore.
It means that if some among the PHP community (at least its users since Zend is not responding) collaborate with G-WAN then this PHP support will quickly surpass today's G-WAN crude PHP draft interfacing – in addition to already surpassing every server in existence in the (not-so-open) PHP ecosystem.
I am not a PHP user. I will not be hurt if G-WAN makes Asm, C, C++, C#, D, Java and Scala scripts run at full-speed and not PHP (after all, PHP ranks only #6 on TIOBE's index and it is loosing ground – probably for a reason). But given my sincere efforts since 2009 to help the PHP community (some G-WAN registered users are PHP developers), this would be quite a waste of resources to bypass G-WAN now it has demonstrated that the savings for PHP users are without equivalent.
That's your money guys that you are wasting in poorly performing Web servers unable to use 99.9% of the hardware resources that you have paid at a premium... while burning 100% of the electricity that they consume doing almost nothing.
You have a chance to stop the current haemorrhage that will only be growing in the future as multicore grows, or to continue losing your blood – your choice. Early adopters will be immediately more profitable, making it impossible to compete for those still in denial of the single most important problem facing the global IT industry.
History will choose for you anyway: economic forecasts are rather unrosy – and this will not change any time soon.
Would you use a car that both wastes 99.9% of the fuel you buy and runs at 0.1% of the allowed speed?
Not me. And that's why I wrote G-WAN.