oscmarb / phpunit-speedgun
在您的PHPUnit测试套件中搜索缓慢的测试
Requires
- php: >=8.0
- phpunit/phpunit: ^8.0 || ^9.0
README
SpeedGun 可以检测使用PHPUnit执行的缓慢测试,并在控制台显示它们。
测试的执行时间可能会因多种原因而有所不同,例如系统负载、磁盘访问等。此工具可以识别被认为是缓慢的测试,但可能无法解释为什么它们是缓慢的。
安装
SpeedGun 使用 Composer 进行安装。将其添加为 require-dev
依赖项
composer require --dev oscmarb/phpunit-speedgun
用法
通过将以下代码添加到项目的 phpunit.xml
文件中启用所有默认设置
<phpunit bootstrap="vendor/autoload.php"> ... <extensions> <extension class="Oscmarb\SpeedGun\SpeedGunPHPUnitExtension"/> </extensions> </phpunit>
现在运行测试套件。如果某个或多个测试执行超过了缓慢阈值(默认为100毫秒),则在所有测试完成后,SpeedGun 将在控制台报告这些测试。
配置扩展
SpeedGun 支持以下配置参数
slow_threshold
- 当测试被认为是“缓慢”时的毫秒数(默认:100ms)min_report_length
- 最小缓慢测试数量。如果缓慢测试的数量不超过min_report_length
的值,则不会显示结果。(默认:0个测试)max_report_length
- 报告中包含的最大缓慢测试数量。必须大于min_report_length
(默认:所有测试)pattern_thresholds
- 应用到一组测试的特殊条件的正则表达式。
每个参数都在 phpunit.xml
中设置。
<phpunit bootstrap="vendor/autoload.php"> <!-- ... other suite configuration here ... --> <extensions> <extension class="Oscmarb\SpeedGun\SpeedGunPHPUnitExtension"> <arguments> <array> <element key="slow_threshold"> <integer>100</integer> </element> <element key="min_report_length"> <integer>3</integer> </element> <element key="max_report_length"> <integer>5</integer> </element> <element key="pattern_thresholds"> <array> <element key="(.*)PatternSlow(.*)"> <integer>500</integer> </element> </array> </element> </array> </arguments> </extension> </extensions> </phpunit>
每个测试用例的定制缓慢阈值
有些测试由于其实现方式,比我们套件中的测试平均慢(或快)。因此,有几种方法可以自定义阈值。
注解
使用 @slowThreshold
注解可以设置阈值。此值可以高于或低于一般阈值。设置的阈值仅用于我们使用它的测试。
class SlowTestCase extends PHPUnit\Framework\TestCase { /** * @slowThreshold 5000 */ public function testSlow() { // Code that takes a longer time to execute } }
设置 @slowThreshold 0
将永远不会将该测试报告为缓慢。
正则表达式
另一种选择是使用正则表达式为一系列测试设置阈值。测试通过 class::method
识别。例如: App\SlowTestCase::test_slow
。
假设我们有一些与Doctrine集成的集成测试,并且我们希望提高这些测试的阈值,因为它们比单元测试慢。我们可以一个一个地更新阈值,使用注解。
这个过程很慢,而且我们可能最终会有缺少正确注解的测试。如果我们知道所有包含Doctrine的测试都包含单词 Doctrine
,我们可以按如下方式配置不同的阈值
<phpunit bootstrap="vendor/autoload.php"> <!-- ... other suite configuration here ... --> <extensions> <extension class="Oscmarb\SpeedGun\SpeedGunPHPUnitExtension"> <arguments> <array> <element key="pattern_thresholds"> <array> <element key="(.*)Doctrine(.*)"> <integer>500</integer> </element> </array> </element> </array> </arguments> </extension> </extensions> </phpunit>
使用环境变量禁用缓慢性分析
SpeedGun 在 phpunit.xml 中启用时会对缓慢测试进行分析。但使用名为 PHPUNIT_SPEED_GUN
的环境变量可以启用或禁用此扩展
PHPUNIT_SPEED_GUN="0" ./vendor/bin/phpunit****
或
PHPUNIT_SPEED_GUN="disabled" ./vendor/bin/phpunit****
环境变量的值也可以在 phpunit.xml
文件中设置
<phpunit bootstrap="vendor/autoload.php"> <php> <env name="PHPUNIT_SPEED_GUN" value="0"/> </php> <extensions> <extension class="Oscmarb\SpeedGun\SpeedGunPHPUnitExtension"/> </extensions> </phpunit>
设置
要求
- Docker
说明
按照以下步骤设置环境,以便您可以修改和测试 SpeedGun
# Clone the project (or fork it)
$ git clone git@github.com:oscmarb/phpunit-speedgun.git
# Build docker image and install dependencies
$ make
# Run test suite to verify code runs as expected
$ make phpunit
Makefile 文件包含所有可用的命令。
PHP版本支持策略
此包支持具有 活跃支持 的PHP版本。
本软件包的维护者在其初始发布后添加对PHP版本的支持,并在其达到活跃支持结束期时停止对PHP版本的支持。
许可协议
phpunit-speedgun 在MIT许可协议下可用。
致谢
本软件包受到 johnkary/phpunit-speedtrap
的启发,该软件最初由 John Kary 以MIT许可协议发布。
社交媒体
在Twitter上关注 @oscmarb。