The only productive way to load test an Apache or WAMP (such as WampDeveloper Pro) web-server is to test a real-world page that itself performs -
- Loading and processing of multiple PHP files.
- Establishment of multiple MySQL connections, and performing multiple table reads.
This is the minimum, because the test of an almost empty and static page (used by most examples) tells us nothing about how the different parts of a web-server hold up under stress, nor how that web-server setup will handle real-world concurrent connections to websites running on web-apps such as WordPress.
* Ideally, this test would also a) perform GETs of all page assets (css, js, images) and b) simulate traffic of which 10% is DB writes (we’ll skip this because its more complicated to set up).
Luckily, this type of test is very easy to do in a quick (and somewhat dirty) way by using Apache’s
ab (Apache Bench) application (that’s included with each Apache version in its \bin directory).
ab test won’t be the most extensive test, and it comes with its own caveats, but it will quickly show you -
- If there is an immediate problem with the setup (this problem will manifest itself in Apache crashing).
- How far you can push the Apache, PHP, and MySQL web-server (with concurrent connections and page request load).
- And what Apache and PHP settings you should modify to get better performance and eliminate the crashes.
There are some problems with
ab to be aware of -
abwill not parse HTML to get the additional assets of each page (css, images, etc).
abcan start to error out, breaking the test, as the number of requests to perform is increased, more connections are established but not returned, and as the load increases and more time passes (see
ab -hfor explanation of
abis an HTTP/1.0 client, not a HTTP/1.1 client, and “
Connection: KeepAlive” (
ab-k switch) requests of dynamic pages will not work (dynamic pages don’t have a predetermined “
Content-Length: value“, and using “
Transfer-Encoding: chunked” is not possible with HTTP/1.0 clients).
AB and the
KeepAlive issue -
A Keep-Alive connection with an HTTP/1.0 client can only be used when the length of the content is known in advance. This implies that dynamic content will generally not use Keep-Alive connections to HTTP/1.0 clients.
A persistent connection with an HTTP/1.0 client cannot make use of the chunked transfer-coding, and therefore MUST use a Content-Length for marking the ending boundary of each message.
Chunked transfer encoding allows a server to maintain an HTTP persistent connection for dynamically generated content. In this case the HTTP Content-Length header cannot be used to delimit the content and the next HTTP request/response, as the content size is as yet unknown.
Before Performing The Load Test
Make sure that -
- You’ve rebooted the system and don’t have anything extra open/running (i.e., YouTube videos playing in your Browser).
- These extra PHP extensions are not loaded: Zend OPcache, APC, nor XDebug.
- You wait 4 minutes before performing another
abtest to avoiding TCP/IP Port Exhaustion (also known as ephemeral port exhaustion).
- And in a test where
KeepAliveworks (it doesn’t in
abtests getting dynamic pages), the number of Apache Worker Threads are set to be greater than the number of concurrent users/visitors/connections.
- If Apache or PHP crashes, you’ve rebooted the computer or VM before performing another test (some things get stuck and continue to persist after Apache and/or mod_fcgid’s PHP processes are restarted).
Start The AB Test
1. Install WordPress as http://www.example.com/blog
2. Open the command-line (cmd.exe).
3. Restart Apache and MySQL, and prime the web-server (with 1 request):
ab -n 1 -c 1 -k -H "Accept-Encoding: gzip, deflate" http://www.example.com/blog/
4. Run the Apache Bench program to simulate -
1 concurrent user doing 100 page hits
This is 100 sequential page loads by a single user:
ab -n 100 -c 1 -k -H "Accept-Encoding: gzip, deflate" http://www.example.com/blog/
This shows you how well the web-server will handle a simple load of 1 user doing a number of page loads.
5 concurrent users each doing 10 page hits
This is 100 page loads by 5 different concurrent users, each user is doing 10 sequential pages loads.
ab -n 50 -c 10 -k -H "Accept-Encoding: gzip, deflate" http://www.example.com/blog/
This represents a peak load of a website that gets about 50,000+ hits a month. Congratulations, your website / business / idea has made it (and no doubt is on its way up).
10 concurrent users each doing 10 page hits
This is 100 page loads by 10 different concurrent users, each user is doing 10 sequential pages loads.
ab -n 100 -c 10 -k -H "Accept-Encoding: gzip, deflate" http://www.example.com/blog/
This is where the load starts to really stress test the web-server, as 10 concurrent (simultaneous) users is a lot of traffic. Most websites will be lucky to see 1 or 2 users (visitors) a minute… So let me say it again, 10 users per second is a lot of traffic!
30 concurrent users each doing 20 page hits
This is 600 page loads by 30 different concurrent users, each user is doing 20 sequential pages loads.
ab -n 600 -c 30 -k -H "Accept-Encoding: gzip, deflate" http://www.example.com/blog/
This is the edge of what a non-cached WordPress setup will be able to handle without crashing or timing-out the web-server (and/or
ab itself). This type of load represents an extremely active website or forum, the top 1%.
90 concurrent users each doing 30 page hits
This is 2700 page loads by 90 different concurrent users, each user is doing 30 sequential pages loads.
ab -n 2700 -c 90 -k -H "Accept-Encoding: gzip, deflate" http://www.example.com/blog/
Only a fully cached (using mod_cache) Apache setup will be able to handle this type of a load. This represents some of the busiest sites on the net, and there is no hope of this not maxing out and crashing (if your settings are not just right) the web-server with a non-cached WordPress setup.
Analyze the AB Results
We only care about 3 things:
1. How many Requests Per Second are we seeing? The other metrics are not really useful, as they are not representative of anything real in this
ab context. * This value will remain somewhat the same regardless of the concurrency level used.
2. Are there any errors in the website’s or Apache’s (general) error and php logs? * When things stat to choke, PHP memory issues will start coming up. A lot of PHP scripts also begin to crash (and take out Apache + PHP processes) if they are not written with concurrency in mind.
3. At what concurrency level does Apache crash and/or time-out? * If this is happening at a lower concurrency level, something is wrong and you need to adjust these settings either lower of higher…
Adjust Settings to Gain Stability Under Load
# The number of Apache threads (workers) to deploy. Each worker can handle a separate concurrent connection or request. # This should never be set more than the expected traffic can bring in, otherwise it will waste server resources. # Setting this value DOWN from the default 64-128 is a good idea. ThreadsPerChild 32 ThreadLimit 32 # The number of requests (or connections if KeepAlive is On) after wich to recycle all Apache threads (to help control memory leaks and process bloat). # Renamed to MaxConnectionsPerChild under Apache 2.4 ("MaxRequestsPerChild" is still valid). MaxRequestsPerChild 16384 # The default stack size on Windows is less than or equal to 1MB, and 8MB on Linux. # Increase size to help crashes / segmentation faults / stack overflows due to PHP scripts needing more stack size. # Can decrease value to lower memory consumption by Apache when PHP is ran via mod_php (PHP as an Apache thread). # Can decrease value even more when PHP is ran via mod_fcgid (PHP as a process outside of Apache). # 8MB*1024*1024 is 8388608, 4MB*1024*1024 is 4194304, 1MB*1024*1024 is 1048576, 0.5MB*1024*1024 is 524288 <IfVersion >= 2.2> ThreadStackSize 4194304 </IfVersion> # The maximum number of free Kbytes that every Apache thread is allowed to hold without attempting to give it back to the OS. # Apache 2.0 and 2.2 default to 0 / unlimited (bad), Apache 2.4 to 2MB (better). # This might prevent the Apache process from growing too large as this will typically restrict its process max size to threads * MaxMemFree. MaxMemFree 2048 # Backlog queue when all threads/workers are taken up. # Only useful to increase during TCP SYN flood attacks. ListenBacklog 511
PHP (for crashes):
; Determines the size of the realpath cache to be used by PHP. This value should ; be increased on systems where PHP opens many files to reflect the quantity of ; the file operations performed. ; http://php.net/realpath-cache-size ;realpath_cache_size = 16k realpath_cache_size = 1M
For stability, also make sure to test both PHP and PHP-FCGI. The difference is PHP (mod_php) runs inside of Apache, PHP-FCGI (separate process via mod_fcgid) runs outside of Apache. PHP-FCGI might be more stable under some circumstances, and more fickle under others.
For top performance gains use -
1. Apache’s mod_cache module to cache page requests/results. This will produce 5-10x the performance gains over all other methods combined.
2. PHP’s Zend OPcache extension to cache PHP scripts as compiled objects. This will produce a 3-5x Requests Per Second speed up.
3. memcached + php_memcache setup to cache PHP script’s or web-app’s internal data and results. This can produce a good 50%-100% performance gain.
5. mod_expires to make the client’s (visitor’s) Browser cache pages and page assets for a given time, instead of re-getting those pages and assets on each page load.
* Some of these are more difficult to configure and set up than others.
Also, in my experience, the switch from 32 bit to 64 bit Apache, PHP, and MySQL versions only provides limited/marginal performance gains (and in some cases it’s even negative).
To sum everything up, 99% of all performance gains will come from utilizing Apache’s caching mechanisms (via mod_cache), using PHP Zend OPcache (extension), and afterwards (once the bottleneck is moved from Apache with PHP to MySQL), improving MySQL performance by tuning my.ini settings, and optimizing/restructuring MySQL queries by utilizing MySQL’s Slow Query log (to see what the problem is).
Having said that, there are also performance robing issues that can exist on the OS, in the Apache/MySQL/PHP settings, and even the client’s Browser, that are covered here -