ergebnis/phpunit-slow-test-detector
Composer 安装命令:
composer require ergebnis/phpunit-slow-test-detector
包简介
Provides facilities for detecting slow tests in phpunit/phpunit.
README 文档
README
This project provides a composer package and a Phar archive with an extension for detecting slow tests in phpunit/phpunit.
Example
After installing, configuring, and bootstrapping the extension, when running your tests with phpunit/phpunit, the extension will report slow tests:
PHPUnit 10.0.0 by Sebastian Bergmann and contributors. Runtime: PHP 8.1.0 Configuration: test/EndToEnd/Default/phpunit.xml Random Seed: 1676103726 ............. 13 / 13 (100%) Detected 11 tests where the duration exceeded the global maximum duration (0.500). # Duration Test ---------------------------------------------------------------------------------------------------------------------------------------------------- 1 1.604 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#9 2 1.505 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#8 3 1.403 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#7 4 1.303 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#6 5 1.205 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#5 6 1.103 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#4 7 1.005 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#3 8 0.905 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#2 9 0.805 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#1 10 0.705 Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#0 ---------------------------------------------------------------------------------------------------------------------------------------------------- 0.000 └─── seconds There is 1 additional slow test that is not listed here. Time: 00:12.601, Memory: 8.00 MB OK (13 tests, 13 assertions)
Compatibility
The extension is compatible with the following versions of phpunit/phpunit:
phpunit/phpunit:^13.0.0phpunit/phpunit:^12.0.0phpunit/phpunit:^11.0.0phpunit/phpunit:^10.0.0phpunit/phpunit:^9.0.0phpunit/phpunit:^8.5.19phpunit/phpunit:^7.5.0phpunit/phpunit:^6.5.0
Installation
Installation with composer
Run
composer require --dev ergebnis/phpunit-slow-test-detector
to install ergebnis/phpunit-slow-test-detector as a composer package.
Installation as Phar
Download phpunit-slow-test-detector.phar from the latest release.
Usage
Bootstrapping the extension
Before the extension can detect slow tests in phpunit/phpunit, you need to bootstrap it. The bootstrapping mechanism depends on the version of phpunit/phpunit you are using.
Bootstrapping the extension as a composer package
To bootstrap the extension as a composer package when using
phpunit/phpunit:^13.0.0phpunit/phpunit:^12.0.0phpunit/phpunit:^11.0.0phpunit/phpunit:^10.0.0
adjust your phpunit.xml configuration file and configure the
extensionselement onphpunit/phpunit:^13.0.0extensionselement onphpunit/phpunit:^12.0.0extensionselement onphpunit/phpunit:^11.0.0extensionselement onphpunit/phpunit:^10.0.0
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
+ <extensions>
+ <bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
+ </extensions>
<testsuites>
<testsuite name="unit">
<directory>test/Unit/</directory>
</testsuite>
</testsuites>
</phpunit>
To bootstrap the extension as a composer package when using
phpunit/phpunit:^9.0.0phpunit/phpunit:^8.5.19phpunit/phpunit:^7.5.0
adjust your phpunit.xml configuration file and configure the
extensionselement onphpunit/phpunit:^9.0.0extensionselement onphpunit/phpunit:^8.5.19extensionselement onphpunit/phpunit:^7.5.0
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
+ <extensions>
+ <extension class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
+ </extensions>
<testsuites>
<testsuite name="unit">
<directory>test/Unit/</directory>
</testsuite>
</testsuites>
</phpunit>
To bootstrap the extension as a composer package when using
phpunit/phpunit:^6.5.0
adjust your phpunit.xml configuration file and configure the
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
+ <listeners>
+ <listener class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
+ </listeners>
<testsuites>
<testsuite name="unit">
<directory>test/Unit/</directory>
</testsuite>
</testsuites>
</phpunit>
Bootstrapping the extension as a PHAR
To bootstrap the extension as a PHAR when using
phpunit/phpunit:^13.0.0phpunit/phpunit:^12.0.0phpunit/phpunit:^11.0.0phpunit/phpunit:^10.0.0
adjust your phpunit.xml configuration file and configure the
extensionsDirectoryattribute and theextensionselement onphpunit/phpunit:^13.0.0extensionsDirectoryattribute and theextensionselement onphpunit/phpunit:^12.0.0extensionsDirectoryattribute and theextensionselement onphpunit/phpunit:^11.0.0extensionsDirectoryattribute and theextensionselement onphpunit/phpunit:^10.0.0
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
+ extensionsDirectory="directory/where/you/saved/the/extension/phars"
>
+ <extensions>
+ <extension class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
+ </extensions>
<testsuites>
<testsuite name="unit">
<directory>test/Unit/</directory>
</testsuite>
</testsuites>
</phpunit>
To bootstrap the extension as a PHAR when using
phpunit/phpunit:^9.0.0phpunit/phpunit:^8.5.19phpunit/phpunit:^7.5.0
adjust your phpunit.xml configuration file and configure the
extensionsDirectoryattribute and theextensionselement onphpunit/phpunit:^9.0.0extensionsDirectoryattribute and theextensionselement onphpunit/phpunit:^8.5.19extensionsDirectoryattribute and theextensionselement onphpunit/phpunit:^7.5.0
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
+ extensionsDirectory="directory/where/you/saved/the/extension/phars"
>
+ <extensions>
+ <bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
+ </extensions>
<testsuites>
<testsuite name="unit">
<directory>test/Unit/</directory>
</testsuite>
</testsuites>
</phpunit>
Configuring the extension
You can configure the extension with the following options in your phpunit.xml configuration file:
maximum-count, anint, the maximum count of slow test that should be reported, defaults to10maximum-duration, anint, the maximum duration in milliseconds for a test before the extension considers it as a slow test, defaults to500stderr, abool, directs the slow test report output tostderr, defaults tofalse, only available onphpunit/phpunit:^6.5.0,phpunit/phpunit:^7.5.0,phpunit/phpunit:^8.5.19, andphpunit/phpunit:^9.0.0(onphpunit/phpunit:^10.0.0and later, the extension automatically respects thestderrconfigurationphpunit/phpunit)
The configuration mechanism depends on the version of phpunit/phpunit you are using.
Configuring the extension
To configure the extension when using
phpunit/phpunit:^13.0.0phpunit/phpunit:^12.0.0phpunit/phpunit:^11.0.0phpunit/phpunit:^10.0.0
adjust your phpunit.xml configuration file and configure one or more
parameterelements onphpunit/phpunit:^13.0.0parameterelements onphpunit/phpunit:^12.0.0parameterelements onphpunit/phpunit:^11.0.0parameterelements onphpunit/phpunit:^10.0.0
The following example configures the maximum count of slow tests to three, and the maximum duration for all tests to 250 milliseconds:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
<extensions>
- <bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
+ <bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension">
+ <parameter name="maximum-count" value="3"/>
+ <parameter name="maximum-duration" value="250"/>
+ </bootstrap>
</extensions>
<testsuites>
<testsuite name="unit">
<directory>test/Unit/</directory>
</testsuite>
</testsuites>
</phpunit>
To configure the extension when using
phpunit/phpunit:^9.0.0phpunit/phpunit:^8.5.19phpunit/phpunit:^7.5.0
adjust your phpunit.xml configuration file and configure the
argumentselement onphpunit/phpunit:^9.0.0argumentselement onphpunit/phpunit:^8.5.19argumentselement onphpunit/phpunit:^7.5.0
The following example configures the maximum count of slow tests to three, the maximum duration for all tests to 250 milliseconds, and directs the slow test report output to stderr:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
<extensions>
- <extension class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
+ <extension class="Ergebnis\PHPUnit\SlowTestDetector\Extension">
+ <arguments>
+ <array>
+ <element key="maximum-count">
+ <integer>3</integer>
+ </element>
+ <element key="maximum-duration">
+ <integer>250</integer>
+ </element>
+ <element key="stderr">
+ <boolean>true</boolean>
+ </element>
+ </array>
+ </arguments>
+ </extension>
</extensions>
<testsuites>
<testsuite name="unit">
<directory>test/Unit/</directory>
</testsuite>
</testsuites>
</phpunit>
To configure the extension when using
phpunit/phpunit:^6.5.0
adjust your phpunit.xml configuration file and configure the
The following example configures the maximum count of slow tests to three, the maximum duration for all tests to 250 milliseconds, and directs the slow test report output to stderr:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
<listeners>
- <listener class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
+ <listener class="Ergebnis\PHPUnit\SlowTestDetector\Extension">
+ <arguments>
+ <array>
+ <element key="maximum-count">
+ <integer>3</integer>
+ </element>
+ <element key="maximum-duration">
+ <integer>250</integer>
+ </element>
+ <element key="stderr">
+ <boolean>true</boolean>
+ </element>
+ </array>
+ </arguments>
+ </listener>
</listeners>
<testsuites>
<testsuite name="unit">
<directory>test/Unit/</directory>
</testsuite>
</testsuites>
</phpunit>
Configuring the maximum duration per test case
You can configure the maximum duration for a single test case with
- an
Attribute\MaximumDurationattribute when usingphpunit/phpunit:^13.0.0phpunit/phpunit:^12.0.0phpunit/phpunit:^11.0.0phpunit/phpunit:^10.0.0
- a
@maximumDurationannotation in the DocBlock when usingphpunit/phpunit:^9.0.0phpunit/phpunit:^8.5.19phpunit/phpunit:^7.5.0phpunit/phpunit:^6.5.0
- a
@slowThresholdannotation in the DocBlock when usingphpunit/phpunit:^9.0.0phpunit/phpunit:^8.5.19phpunit/phpunit:^7.5.0phpunit/phpunit:^6.5.0
The following example configures the maximum durations for single test cases to 5,000 ms, 4,000 ms, and 3,000 ms:
<?php declare(strict_types=1); use PHPUnit\Framework; use Ergebnis\PHPUnit\SlowTestDetector; final class ExtraSlowTest extends Framework\TestCase { #[SlowTestDetector\Attribute\MaximumDuration(5000)] public function testExtraExtraSlow(): void { // ... } /** * @maximumDuration 4000 */ public function testAlsoQuiteSlow(): void { // ... } /** * @slowThreshold 3000 */ public function testQuiteSlow(): void { // ... } }
Note
Support for the @slowThreshold annotation exists only to help you move from johnkary/phpunit-speedtrap. It will be deprecated and removed in the near future.
Understanding measured test durations
Understanding measured test durations when using the new event system
When using
phpunit/phpunit:^13.0.0phpunit/phpunit:^12.0.0phpunit/phpunit:^11.0.0phpunit/phpunit:^10.0.0
the extension uses the new event system of phpunit/phpunit, and measures the duration between the points in time when the PHPUnit\Event\Test\PreparationStarted and PHPUnit\Event\Test\Finished are emitted.
Understanding measured test durations when using the hooks event system
When using
phpunit/phpunit:^9.0.0phpunit/phpunit:^8.5.19phpunit/phpunit:^7.5.0phpunit/phpunit:^6.5.0
the extension uses the hooks event system of phpunit/phpunit, and measures the duration that is passed to the PHPUnit\Runner\AfterTestHook. This is the duration of invoking PHPUnit\Framework\TestCase::runBare() and more.
Note
Because of this behavior, the measured test durations can and will vary depending on the order in which phpunit/phpunit executes tests.
Changelog
The maintainers of this project record notable changes to this project in a changelog.
Contributing
The maintainers of this project suggest following the contribution guide.
Code of Conduct
The maintainers of this project ask contributors to follow the code of conduct.
General Support Policy
The maintainers of this project provide limited support.
You can support the maintenance of this project by sponsoring @ergebnis.
PHP Version Support Policy
This project currently supports the following PHP versions:
- PHP 7.0 (has reached its end of life on January 10, 2019)
- PHP 7.1 (has reached its end of life on December 1, 2019)
- PHP 7.2 (has reached its end of life on November 30, 2020)
- PHP 7.3 (has reached its end of life on December 6, 2021)
- PHP 7.4 (has reached its end of life on November 28, 2022)
- PHP 8.0 (has reached its end of life on November 26, 2023)
- PHP 8.1 (has reached its end of life on December 31, 2025)
- PHP 8.2
- PHP 8.3
- PHP 8.4
- PHP 8.5
The maintainers of this project add support for a PHP version following its initial release and may drop support for a PHP version when it has reached its end of life.
Security Policy
This project has a security policy.
License
This project uses the MIT license.
Credits
This package is inspired by johnkary/phpunit-speedtrap, originally licensed under MIT by John Kary.
Social
Follow @localheinz and @ergebnis on Twitter.
ergebnis/phpunit-slow-test-detector 适用场景与选型建议
ergebnis/phpunit-slow-test-detector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.51M 次下载、GitHub Stars 达 148, 最近一次更新时间为 2021 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phpunit」 「test」 「extension」 「detector」 「slow」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ergebnis/phpunit-slow-test-detector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ergebnis/phpunit-slow-test-detector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ergebnis/phpunit-slow-test-detector 相关的其它包
同方向 / 同关键字的高下载量 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
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
统计信息
- 总下载量: 9.51M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 150
- 点击次数: 11
- 依赖项目数: 76
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-24