承接 teqneers/phpunit-stopwatch 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

teqneers/phpunit-stopwatch

Composer 安装命令:

composer require teqneers/phpunit-stopwatch

包简介

Extension to measure and analyse any part of your code in order to detect performance issues using phpunit/phpunit.

README 文档

README

Latest Stable Version CI

codecov Type Coverage

Project information: License GitHub code size in bytes

Stopwatch is an essential phpunit/phpunit extension for performance analysis!

Gain invaluable insights into your code's execution time and call frequency during test runs. The extension is designed to elevate your code performance analysis. Track the execution time and frequency of any marked code segments to address performance bottlenecks with precision. You can identify performance bottlenecks (e.g., database cleanups or setup) or just monitor them over time. The extension will generate stopwatch reports for each test as well as a summary report at the end of the test run.

This project provides a composer package and a Phar archive.

The extension is compatible with the following versions of phpunit/phpunit:

Once you've added some measurement points to your code, the extension will stop watch and count them. The results are displayed for each test and as a total report at the end of the test run.

Here is an example of how the output of a single test class might look:

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)

And at the end of the test run, you will get a summary of all stopwatches used, and it is going to look like this:

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)

Usage

Stopwatch is very easy to use. A single line of code wrapped around the code you want to measure is all it takes.

For instance, let's say your tests are pretty slow, but you don't know who's the culprit? You suspect that it might be the database setup that has to be done for each and every test. Simply wrap the suspected code block as follows:

        Stopwatch::start(__METHOD__);

        self::initializeDatabase(
            $db->getConnection(),
            ...static::createData()
        );

        Stopwatch::stop(__METHOD__);

Should the test, setup, teardown, or other relevant methods execute this code, Stopwatch will seamlessly measure its execution time and count, and will present the results within the test output.

Installation

Installation with composer

Run

composer require --dev teqneers/phpunit-stopwatch

to install teqneers/phpunit-stopwatch as a composer package.

Installation as Phar

Download phpunit-stopwatch.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:^11.0.0
  • phpunit/phpunit:^12.0.0
  • phpunit/phpunit:^13.0.0

adjust your phpunit.xml configuration file and configure the extensions element.

 <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>

Bootstrapping the extension as a PHAR

To bootstrap the extension as a PHAR when using

  • phpunit/phpunit:^11.0.0
  • phpunit/phpunit:^12.0.0
  • phpunit/phpunit:^13.0.0

adjust your phpunit.xml configuration file and configure the extensionsDirectory attribute and the extensions element.

 <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>

Configuring the extension

So far, there are no configuration settings for this extension.

Running tests

When you have bootstrapped the extension, you can run your tests as usually. E.g.:

vendor/bin/phpunit

When the extension is used somewhere in your code, it will give you a report:

License

This project uses the MIT license.

Credits

This package is inspired by ergebnis/phpunit-slow-test-detector.

teqneers/phpunit-stopwatch 适用场景与选型建议

teqneers/phpunit-stopwatch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 978 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「performance」 「phpunit」 「test」 「time」 「extension」 「measure」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 teqneers/phpunit-stopwatch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 teqneers/phpunit-stopwatch 我们能提供哪些服务?
定制开发 / 二次开发

基于 teqneers/phpunit-stopwatch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 978
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-15