Main Content

A simple server benchmark script

Archive - Originally posted on "The Horse's Mouth" - 2010-04-27 05:44:59 - Graham Ellis

Here's one of those examples where I found myself illustrating so many interesting features of PHP in one piece of code that I almost lost count!

<?php
# Web Site performance test page
 
if ($_REQUEST[pub] == "magpies") {
$wowser = `ab -n100 -c5 http://www.example.net/robots.txt`;
header("Content-type: text/plain");
print ($wowser);
exit();
}
 
?>
<html>
<body>
<h1>Under Construction</h1>
You wanted it WHEN?
</body>
</html>


1. The use of back quotes to run an operating system command.

2. The use of the ab program to benchmark another system / web2 techniques.

3. The ability to output different content types dependent on inputs.

4. The use of a "sequence diagram" to show what's actually going on in a piece of code.

5. exit to leave a PHP script in the middle of the code.

6. A case of "security through obscurity" - the way that the script will only run the real application it contains if the user adds a certain parameter to the command line.

Downloadable copy of the source - [here].

On the main subject line of this being a simple benchmark script ... it's a VERY simple one; it just prods a server with lots of requests for the same page (100 requests, 5 concurrent) and should be run with care and the results should be taken with a large pinch of salt. If you're going to run something like this, run it on a different machine to the one you're testing, but one that has plenty of bandwidth, and do it at a quiet time of day initially.

If you're looking for a more realistic load test mechanism, try Jmeter. Although it's written in Java, it can test any server. See [here] for an article I wrote on that a while back.