PHP-benchmark

This a library containing classes used to compare algorithms and benchmark your application.

This project is maintained by victorjonsson

PHP-Benchmark

This a library containing classes used to compare algorithms and benchmark your application.

Benchmarking

Setup

  1. Either download the library to your server or install it in your project using composer
  2. Include the file init.php in the very beginning of the first file that receives the request to your application (this is usually index.php). After having done this you can load the address of your website in the browser with the query parameters php-benchmark-test=1&display-data=1 and the benchmark data will be displayed in the upper left corner of your website.

Becnhmark 1

Taking snapshots

If you want to take snapshots from the benchmark data during the request you can do so by adding the following code where you want a snapshot to be taken.

 \PHPBenchmark\Monitor::instance()->snapshot('Bootstrap finished');

Inserting some snapshots in the source code of WordPress may give the following benchmark data:

Becnhmark 1

Stress test using command line (nodejs)

By using the command line tool (nodejs) you'll get information about average memory peaks and time spent on generating the page. The test will do a number of requests to your application with a query parameter that tells the benchmark script to monitor the time and memory consumption during the request. When all requests is finished you will get the following information:

First of all you need to install node if you haven't already. After that you have installed node you have to download the nodejs script to your server (name the file php-benchmark). Then navigate to the directory where your nodejs script is located and run one of the following commands:

$ node php-benchmark http://mywebsite.com/ Will do a benchmark test with 50 requests that gives you average page generation time, memory peak, number of loaded classes and included files.

$ node php-benchmark http://mywebsite.com/ -n 500 Will do a benchmark test with 500 requests.

$ node php-benchmark http://mywebsite.com/ -s 20 Will start 20 requests per second until all requests is made. If you're doing this test to monitor the performance of your PHP code (rather then load testing your server infrastructure) and having limited server resources it might be good to set this option as low as 1-2 requests per second.

$ node php-benchmark http://mywebsite.com/ -nu Will prevent the use of unique URL:s when requesting the application.

$ node php-benchmark http://mywebsite.com/ -v If a lot of the request fails it might be good to add -v to get more information about the failing requests.

$ node php-benchmark http://mywebsite.com/ -f /var/log/ Will write ouput to log file in /var/log/.

$ node php-benchmark http://mywebsite.com/ -f test.log Will write ouput to log file test.log in current working directory.

All options can of course be used together as well.

Comparing algorithms

Setup (manually)

Download the library to your server. After that all you have to do is to include the file lib/autoload.php to be able to load the classes you want to use.

Setup (composer)

Add the dependency "phpbenchmark/phpbenchmark" to composer.json and you're set to go.

Example code

require 'vendor/autoload.php';

use \PHPBenchmark\FunctionComparison;

FunctionComparison::load()
    ->setFunctionA('stream_resolve_include_path', function() {
        $bool = stream_resolve_include_path(__FILE__) !== false;
    })
    ->setFunctionB('file_exists', function() {
        $bool = file_exists(__FILE__);
    })
    ->exec();

Load a file with this code in the browser (or run it via command line) and you will find out that stream_resolve_include_path wins the game, being about 30-35% faster.

Extending \PHPBenchmark\AbstractFunctionComparison

If you would want to manage a large collections of tests you can create a class (extending AbstractFunctionComparison) for each test. Put all classes in the same directory and call FunctionComparison::runTests( $path_to_class_directory ) and you'll be able to execute all tests in one request.