2011-07-20 38 views
1

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

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

PHPUnit 3.5中是否有这个功能的等价物,我该如何使用它?

回答

1

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

而不是建立另一个回路或采取$开始和$结束时间读数 - 我露出TIMED_OUT PARAM,这里详细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); 
7

嗯,你可以简单地做这样的事情

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

当然,这意味着你的命令将不被完成之前被中断。

而国际海事组织单位测试并不意味着测试性能。