Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick benchmark from the command line

I'd like to benchmark a PHP script, but this would equally apply to anything that can be run from the command line.

Using bash is there a simple way to benchmark a script, i.e. run a command multiple times and time how long it takes?

like image 685
rgvcorley Avatar asked Jul 18 '12 22:07

rgvcorley


2 Answers

In command line, you can:

$ time php foobar.php

Here time is a bash built-in.

For multiple runs:

$ time for a in {1..10}; do php foobar.php; done
real    0m13.042s
user    0m0.021s
sys     0m0.044s

However, you need to calculate the average time by hand.

like image 99
Xiè Jìléi Avatar answered Nov 20 '22 20:11

Xiè Jìléi


If all you want is to benchmark a PHP script why not just write a unit test for it. Like:

<?php

function test() {
    require 'my_script_to_test.php';
}

for ($i = 0; $i < 1000; $i++) {
    $time = microtime(true);
    test();
    $time = microtime(true) - $time;
    echo 'test '.$i.': '.$time;
    // and then you can also average time and w/e 
}

?>
like image 1
Anthony Hatzopoulos Avatar answered Nov 20 '22 18:11

Anthony Hatzopoulos