teqneers / phpunit-stopwatch
该扩展用于测量和分析代码的任何部分,以使用 phpunit/phpunit 检测性能问题。
Requires
- php: >=8.1.0
- phpunit/phpunit: ^10.1.0 || ^11.0.0
- psr/clock: ^1.0.0
- symfony/clock: ^6.4.0 || ^7.0.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.42.0
- ergebnis/license: ^2.0.0
- ergebnis/php-cs-fixer-config: ^6.23.0
- fakerphp/faker: ^1.23.1
- psalm/plugin-phpunit: ~0.18.4
- rector/rector: ^1.0.2
- roave/security-advisories: dev-master
- scrutinizer/ocular: dev-master
- vimeo/psalm: ^5.23.1
This package is auto-updated.
Last update: 2024-08-25 14:47:11 UTC
README
Stopwatch 是一个用于性能分析的 phpunit/phpunit
扩展,至关重要!
在测试运行期间,您可以获得代码执行时间和调用频率的宝贵见解。该扩展旨在提升您的代码性能分析。跟踪任何标记的代码段的执行时间和频率,以精确地解决性能瓶颈。您可以识别性能瓶颈(例如,数据库清理或设置)或只是随着时间的推移进行监控。该扩展将为每个测试生成计时器报告,并在测试运行结束时生成总结报告。
该项目提供了一个 composer
包和一个 Phar 归档。
该扩展与以下版本的 phpunit/phpunit
兼容
一旦您在代码中添加了一些测量点,扩展程序将停止计时并计数。结果将在每个测试中显示,并在测试运行结束时作为一个总报告显示。
以下是一个单个测试类输出的示例
Stopwatch for TQ\Tests\Example\SingleTest::testDataCalculation: - TQ\Testing\Database::deleteData 0.117secs ( 3x, Ø 0.04) TOTAL 327.026secs ( 184x, Ø 1.78) - ...onment\Testing::cleanupInstance 0.259secs ( 1x, Ø 0.26) TOTAL 6.159secs ( 60x, Ø 0.10) - TQ\Testing\Database::import 7.889secs ( 11x, Ø 0.72) TOTAL 250.958secs ( 352x, Ø 0.71) - Test 1.428secs ( 1x, Ø 1.43) TOTAL 1041.228secs ( 70x, Ø 14.87) . Stopwatch for TQ\Tests\Example\SingleTest::testDataTransfer: - TQ\Testing\Database::deleteData 0.116secs ( 3x, Ø 0.04) TOTAL 327.142secs ( 187x, Ø 1.75) - ...onment\Testing::cleanupInstance 0.256secs ( 1x, Ø 0.26) TOTAL 6.415secs ( 61x, Ø 0.11) - TQ\Testing\Database::import 7.573secs ( 11x, Ø 0.69) TOTAL 258.531secs ( 363x, Ø 0.71) - Test 5.998secs ( 1x, Ø 6.00) TOTAL 1047.226secs ( 71x, Ø 14.75) . Stopwatch for TQ\Tests\Example\SingleTest TearDown: - TQ\Testing\Database::deleteData 38.486secs ( 6x, Ø 6.41) TOTAL 365.511secs ( 190x, Ø 1.92) - ...onment\Testing::cleanupInstance 0.256secs ( 1x, Ø 0.26) TOTAL 6.415secs ( 61x, Ø 0.11) - TQ\Testing\Database::import 7.573secs ( 11x, Ø 0.69) TOTAL 258.531secs ( 363x, Ø 0.71) - Test 5.998secs ( 1x, Ø 6.00) TOTAL 1047.226secs ( 71x, Ø 14.75)
在测试运行结束时,您将获得所有使用的计时器的总结,它将如下所示
Stopwatch TOTALS: - Test TOTAL 1047.246secs ( 78x, Ø 13.43) - TQ\Testing\Database::deleteData TOTAL 365.511secs ( 190x, Ø 1.92) - TQ\Testing\Database::import TOTAL 258.531secs ( 363x, Ø 0.71) - ...onment\Testing::cleanupInstance TOTAL 6.416secs ( 62x, Ø 0.10) - TQ\Production\Monitoring::ping TOTAL 17.967secs ( 7x, Ø 2.57)
用法
Stopwatch 非常易于使用。只需要围绕您想要测量的代码的一行代码即可。
例如,假设您的测试运行速度很慢,但您不知道罪魁祸首是谁?您怀疑可能是每个测试都必须进行的数据库设置。只需将怀疑的代码块包裹如下
Stopwatch::start(__METHOD__); self::initializeDatabase( $db->getConnection(), ...static::createData() ); Stopwatch::stop(__METHOD__);
如果测试、设置、拆卸或其他相关方法执行此代码,Stopwatch 将无缝测量其执行时间和计数,并将结果显示在测试输出中。
安装
使用 composer
安装
运行
composer require --dev teqneers/phpunit-stopwatch
安装 teqneers/phpunit-stopwatch
作为 composer
包。
作为 Phar 安装
从 最新发布版 下载 phpunit-stopwatch.phar
。
用法
引导扩展
在扩展程序可以检测到 phpunit/phpunit
中的慢速测试之前,您需要引导它。引导机制取决于您使用的 phpunit/phpunit
版本。
将扩展作为 composer
包引导
要使用
phpunit/phpunit:^10.0.0
phpunit/phpunit:^11.0.0
调整您的 phpunit.xml
配置文件并配置
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" > + <extensions> + <bootstrap class="TQ\Testing\Extension\Stopwatch\Extension"/> + </extensions> <testsuites> <testsuite name="unit"> <directory>test/Unit/</directory> </testsuite> </testsuites> </phpunit>
将扩展作为 PHAR 启动
在以下情况下,将扩展作为 PHAR 启动:
phpunit/phpunit:^10.1.0
phpunit/phpunit:^11.0.0
调整您的 phpunit.xml
配置文件并配置
- “extensionsDirectory” 属性 和 “extensions” 元素 在 “phpunit/phpunit:^10.0.0”
- “extensionsDirectory” 属性 和 “extensions” 元素 在 “phpunit/phpunit:^11.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="TQ\Testing\Extension\Stopwatch\Extension"/> + </extensions> <testsuites> <testsuite name="unit"> <directory>test/Unit/</directory> </testsuite> </testsuites> </phpunit>
配置扩展
到目前为止,此扩展没有配置设置。
运行测试
在启动扩展后,您可以像往常一样运行测试。例如:
vendor/bin/phpunit
当扩展在您的代码中使用时,它将为您提供报告
许可协议
本项目使用 MIT 许可协议。
鸣谢
本包受到 ergebnis/phpunit-slow-test-detector
的启发。