PHPBench is a single-file PHP benchmarking tool that helps you measure various aspects of your PHP environment's performance, including core PHP operations, I/O operations, randomness generation, and MySQL query handling.
Initially inspired by PHP Benchmark Script, this script has been completely refactored and rewritten. While the original project provided a great starting point, I found that having a single self-contained PHP file is often more convenient. Deploying one file simplifies the process of benchmarking different servers, especially when you have limited access, making maintenance and distribution much easier.
- Single-file approach: All tests and functionality in one PHP file.
- Variety of benchmarks: CPU operations, loops, string/array manipulation, MySQL queries, and more.
- Configurable multiplier: Quickly scale the complexity of benchmarks.
- MySQL integration (optional): Test query performance if
mysqli
is available. - Easy extensibility: Add your own benchmark sets by following the provided examples.
- PHP 5.6 or higher
- The
mysqli
extension for MySQL tests (optional).
-
Command line (CLI): From your SSH:
curl https://raw.githubusercontent.com/demartis/phpbench/main/phpbench.php | php
You can pass arguments as
--key=value
. For example, adjust themultiplier
to make the tests run longer or point to a different MySQL server. Don't forget the double--
when running with the pipe, eg:php -- --multiplier=2
:curl https://raw.githubusercontent.com/demartis/phpbench/main/phpbench.php | php -- --multiplier=2
or locally:
php phpbench.php --multiplier=2 --mysql_host=127.0.0.1 --mysql_user=root --mysql_password=secret
-
Via a web server: Place
phpbench.php
in a web-accessible folder and run:http://yourserver/phpbench.php?multiplier=2
Query string parameters will override default settings (see
$default_args params
). e.g:http://yourserver/phpbench.php?multiplier=2&mysql_host=127.0.0.1&mysql_user=root&mysql_password=secret
It's simple to add your own test functions. For example, to add a custom test set named mytest
:
-
Add it to the
$benchmarks
array like:'mytest' => get_benchmarks_mytest(),
so:
$benchmarks = [ 'core' => get_benchmarks_core(), 'io' => get_benchmarks_io(), 'rand' => get_benchmarks_rand(), 'mytest' => get_benchmarks_mytest(), // <--- your new test 'mysql' => get_benchmarks_mysql(), ];
-
Then define your function:
function get_benchmarks_mytest() { return [ 'mytest_1' => function ($multiplier = 1, $count = 1000) { $count = $count * $multiplier; for ($i = 0; $i < $count; $i++) { something(0, $i); } return $i; }, ]; }
Replace something(0, $i);
with your actual custom code to benchmark.
This project is released under the LGPL license. This means:
- You are free to use, modify, and distribute this software.
- If you modify and distribute the code, you must provide the modified source code under the same LGPL terms.
- You can integrate this code into larger projects, even proprietary ones, as long as you adhere to the LGPL requirements regarding modifications to this code.
For more details, see the LGPL License text.
Contributions are welcome!
- Feel free to open Pull Requests to add new benchmarks, enhance functionality, or improve the code structure.
- Contact me at [email protected] if you have any questions or need guidance.
Your input and improvements can help make PHPBench more useful for everyone.
Author: Riccardo De Martis ([email protected])
GitHub: https://github.com/demartis/phpbench