oscmarb/phpunit-speedgun
Composer 安装命令:
composer require oscmarb/phpunit-speedgun
包简介
Search for slow tests in your PHPUnit test suite
README 文档
README
SpeedGun detects slow tests executed with PHPUnit and displays them in the console.
The execution times of tests can vary for various reasons, such as system load, disk access, etc. This tool can identify tests that are considered slow but may not provide an explanation for why they are slow.
Installation
SpeedGun is installed using Composer. Add it as a require-dev dependency:
composer require --dev oscmarb/phpunit-speedgun
Usage
Enable with all defaults by adding the following code to your project's phpunit.xml file:
<phpunit bootstrap="vendor/autoload.php"> ... <extensions> <extension class="Oscmarb\SpeedGun\SpeedGunPHPUnitExtension"/> </extensions> </phpunit>
Now run the test suite. If one or more test executions exceed the slowness threshold (100ms by default), SpeedGun will report on those tests in the console after all tests have completed.
Configuring the extension
SpeedGun supports these configuration parameters:
slow_threshold- Number of milliseconds when a test is considered "slow" (Default: 100ms)min_report_length- Minimum number of slow tests. If the number of slow tests does not exceed the value ofmin_report_length, no results will be displayed. (Default: 0 tests)max_report_length- Max number of slow tests included in the report. Must be greater thanmin_report_length(Default: all tests)pattern_thresholds- Regex expression to apply special conditions to a set of tests.
Each parameter is set in 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>
Custom slowness threshold per-test case
There are some tests that, due to their implementation, are slower (or faster) than the average of the tests in our suite. For this reason, there are different ways to customize the threshold.
Annotation
The @slowThreshold annotation can set a threshold. This value can be higher or lower than the general threshold. The
set threshold will only be used for the test in which we are using it.
class SlowTestCase extends PHPUnit\Framework\TestCase { /** * @slowThreshold 5000 */ public function testSlow() { // Code that takes a longer time to execute } }
Setting @slowThreshold 0 will never report that test as slow.
Regex expression
Another alternative is to use regular expressions to set a threshold for a set of tests. Tests are identified
by class::method. For example: App\SlowTestCase::test_slow.
Suppose we have integration tests with Doctrine, and we want to increase the threshold for these tests as they are slower than the unit tests. We can go test by test and update the threshold using annotations.
This process is slow, and furthermore, we could end up with tests lacking the correct annotation. If we know that all
tests with Doctrine contain the word Doctrine, we can configure a different threshold as follows:
<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>
Disable slowness profiling using an environment variable
SpeedGun profiles for slow tests when enabled in phpunit.xml. But using an environment variable
named PHPUNIT_SPEED_GUN
can enable or disable the extension:
PHPUNIT_SPEED_GUN="0" ./vendor/bin/phpunit****
or
PHPUNIT_SPEED_GUN="disabled" ./vendor/bin/phpunit****
The value of the environment variable can also be set in the phpunit.xml file:
<phpunit bootstrap="vendor/autoload.php"> <php> <env name="PHPUNIT_SPEED_GUN" value="0"/> </php> <extensions> <extension class="Oscmarb\SpeedGun\SpeedGunPHPUnitExtension"/> </extensions> </phpunit>
Setup
Requirements
- Docker
Instructions
Follow the steps below to set up the environment that allows you to modify and test 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 file contains all available commands.
PHP Version Support Policy
This package supports PHP versions with active support.
The maintainers of this package add support for a PHP version following its initial release and drop support for a PHP version when it has reached its end of active support.
License
phpunit-speedgun is available under the MIT License.
Credits
This package is inspired by johnkary/phpunit-speedtrap, originally
licensed under MIT by John Kary
Social
Follow @oscmarb on Twitter.
oscmarb/phpunit-speedgun 适用场景与选型建议
oscmarb/phpunit-speedgun 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phpunit」 「extension」 「listener」 「HOOK」 「speed」 「profile」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oscmarb/phpunit-speedgun 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oscmarb/phpunit-speedgun 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oscmarb/phpunit-speedgun 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
A custom URL rule class for Yii 2 which allows to create translated URL rules
Timestamp parameter in public file uri
The Listener component of the RabbitEvents library.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-02