Stephen RC
Stephen RC

Reputation: 1504

PHPUnit Performance / Test Timeout

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?

Upvotes: 4

Views: 4448

Answers (2)

cloakedninjas
cloakedninjas

Reputation: 4176

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);

Upvotes: 1

wonk0
wonk0

Reputation: 13962

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.

Upvotes: 10

Related Questions