Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit Performance / Test Timeout

Tags:

phpunit

I am building a testing script which is checking the performance of a set of commands. The test script needs to let it run for a specific amount of time before failing the test.

I found the PerformanceTestCase in the PHPUnit documentation website, but when I tried to use it I realised that it's old functionality which hasn't been included within the new version. (That doc is PHPUnit 3.0, and my version is 3.5).

Is there an equivalent for this functionality within PHPUnit 3.5, and how do I use it?

like image 988
Stephen RC Avatar asked Oct 26 '25 12:10

Stephen RC


2 Answers

Well, you could simply do something like

public function testFoo() {
 $tStart = microtime( true );
 // your time critical commands
 $tDiff = microtime( true ) - $tStart;
 $this->assertLessThan( $maxTime, $tDiff, 'Took too long' );
}

Of course this means that your commands will not be interrupted before being finished.

And IMO unittests are not meant for testing performance.

like image 186
wonk0 Avatar answered Oct 29 '25 00:10

wonk0


I ran into a smiliar issue today - I needed to test an external HTTP call was occuring within the allocated time.

Rather than build another loop or take $start and $end time readings - I exposed the timed_out param, detailed here http://www.php.net/manual/en/function.stream-get-meta-data.php

while (!feof($fp)) {
    $info = stream_get_meta_data($fp);
    if ($info['timed_out']) {
        $this->timed_out = true;
        fclose($fp);
        throw new Exception("Request timed out");
    }
    else {
        $response .= fgets($fp, 128);
    }
}
fclose($fp);
like image 37
cloakedninjas Avatar answered Oct 29 '25 02:10

cloakedninjas