PHPUnit 性能/测试超时

发布于 2024-11-24 21:19:39 字数 368 浏览 1 评论 0原文

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

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

PHPUnit 3.5 中是否有与此功能等效的功能?如何使用它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2024-12-01 21:19:39

好吧,您可以简单地执行类似的操作

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

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

IMO 单元测试并不是为了测试性能。

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.

2024-12-01 21:19:39

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

我没有构建另一个循环或读取 $start 和 $end 时间读数 - 我公开了 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文