In computer terminology, benchmark is the process of measuring the performance of computer resources, particularly the hardware, against any task performed by the computer.
Likewise testing the performance of software is called software benchmark. It basically gives a measure of CPU time and memory chunk, the software has consumed for its working. This is performed by running the computer programs within the software and thereby taking a collective measure of CPU and memory usage.
PHP Benchmark Performance Code
Software benchmark holds significant importance for real time software. Though it is done on a very intense ground with the help of very powerful benchmarking tools, but the programmers at their end can perform some benchmark using The Ubench PHP library available at:
https://github.com/devster/ubench
You just need to install this repository into your system and start benchmarking.
Installing via composer is preferably recommended.
Just need to add the following to your composer.json
1 2 3 4 5 |
{ "require": { "devster/ubench": "1.1.*-dev" } } |
Run composer and get it installed
The repository with the basic code as well as other dependencies will be automatically installed on your system under the folder “Ubench”.
Check This Also:
How to Benchmark PHP Code For Speed
Changing the Format of Date and Time in PHP
How to Create a Downloadable CSV File in PHP
Here is the Sample Code to use This Library
Write this code inside a php file and place it on the root path inside ubench directory.
sample.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php require_once 'src/Ubench.php'; $bench = new Ubench; $bench->start(); // the code for testing starts here $x = 1; for($i=0;$i<=100000;$i++) { $x = $x+$i; } echo 'The value of x is '.$x; echo '<br>'; // the code for testing ends here $bench->end(); echo 'CPU time taken '.$bench->getTime(false, '%d%s'); // 156ms or 1s echo '<br>'; echo 'memory used '.$bench->getMemoryUsage(); // 152B or 90.00Kb or 15.23Mb echo '<br>'; ?> |
Output:
output will look like:
The value of x is 5000050001
CPU time taken 7ms
memory used 256.00Kb
Please notice to place the code, you need to test between $bench->start() and $bench->end() statements.
You can use other benchmark methods available in the ubench class files. Also many other tools are available on web to banchmark you PHP code.
Hope this article may give some benefit to you. Thanks.