phpunit --path-coverage(分支覆盖)花费了 100 倍的时间

Ole*_*kin 2 phpunit code-coverage xdebug

我有一个带有大量测试的 Laravel 项目。我使用 pcov 来计算代码覆盖率,大约需要 4 分钟。但 pcov 不支持分支覆盖,所以我决定使用 xdebug。

使用 xdebug 测试执行,使用代码覆盖率但不使用 --path-coverage(分支覆盖率)大约需要 8 分钟。

但是使用 xdebug、代码覆盖率和 --path-coverage(分支覆盖率)测试执行需要超过 2 个小时,甚至不能等到结束:

INFO[2021-09-14 21:33:24] Executing runtests with coverage xdebug
XDEBUG_MODE=coverage
php artisan test --parallel --processes=8 --verbose --passthru=--path-coverage tests/Feature --coverage-text

Warming cache for static analysis ... done [00:00.071]

............S................................................   61 / 1180 (  5%)
.............................................................  122 / 1180 ( 10%)
.............................................................  183 / 1180 ( 15%)
.............................................................  244 / 1180 ( 20%)
....... 

INFO[2021-09-15 00:00:05] finished in 2h 26m 40.458565176s   
Run Code Online (Sandbox Code Playgroud)

所以我的问题是 --path-coverage 执行超过 100 倍是正常的吗?

Der*_*ick 5

总而言之:是的,这是预期的。

\n

Xdebug 需要做很多事情才能确定这些信息。

\n
    \n
  1. 它需要更详细地分析存在哪些路径和分支,并存储这些信息。即使没有打开路径覆盖,Xdebug 也会进行一些分析来查找死代码 \xe2\x80\x94 这就是它比 pcov 慢的原因,而 pcov 也不会这样做。
  2. \n
  3. Xdebug 需要重载每个内部 PHP 指令(操作码),以便它可以确定它是新分支的开始,如果是,则记录该信息。
  4. \n
  5. Xdebug 需要收集在函数执行期间看到的所有分支,并对其进行整理,以便找出看到的路径,并存储该信息。
  6. \n
\n

All these checks, and the extra storage of data, drastically reduces performance. But that's not because Xdebug is slow, but rather because it does a lot of work.

\n

It would be nice if it was possible to only enable path/branch coverage for specific high-impact functions and methods, but I don't think that PHPUnit has a way for that (yet).

\n