PHPUnit性能/测试超时

时间:2011-07-20 06:22:53

标签: phpunit

我正在构建一个测试脚本,它正在检查一组命令的性能。在测试失败之前,测试脚本需要让它运行一段特定的时间。

我在PHPUnit文档网站上找到了PerformanceTestCase,但是当我尝试使用它时,我意识到它的旧功能尚未包含在新版本中。 (该文档是PHPUnit 3.0,我的版本是3.5)。

PHPUnit 3.5中是否存在此功能的等效物,我该如何使用它?

2 个答案:

答案 0 :(得分:7)

嗯,你可以简单地做一些像

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

当然这意味着您的命令在完成之前不会被中断。

IMO单元测试并非用于测试性能。

答案 1 :(得分:1)

我今天遇到了一个熟悉的问题 - 我需要测试在分配的时间内发生的外部HTTP调用。

而不是构建另一个循环或接受$ start和$ end time读数 - 我公开了timed_out参数,详见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);
相关问题