jclaveau/phpunit-profile-asserts

为PHPUnit提供内存使用和执行时间的断言

1.1.0 2018-07-13 14:32 UTC

This package is auto-updated.

Last update: 2024-09-06 09:58:22 UTC


README

此库提供了对执行时间和内存使用的断言。它还基于Symfony的Stopwatch组件提供了StopwatchListener。

Build Status

安装

phpunit-profile-asserts可以通过Composer安装,并应作为require-dev依赖项添加

composer require --dev jclaveau/phpunit-profile-asserts

用法

通过将以下代码添加到项目的phpunit.xml文件中启用

<phpunit bootstrap="vendor/autoload.php">
...
    <listeners>
        <listener class="JClaveau\PHPUnit\Listener\StopwatchListener" />
    </listeners>
</phpunit>
class SomeTestCase extends \PHPUnit_Framework_TestCase
{
    use \JClaveau\PHPUnit\Framework\UsageConstraintTrait; // adds the asserts methods

    /**
     */
    public function test_usages()
    {
        // ...

        $this->assertExecutionTimeBelow(1.5); // seconds
        $this->assertMemoryUsageBelow('1M');

        // This trait also provides two methods to help knowing the current
        // memory usage and duration
        $this->getMemoryUsage();
        $this->getExecutionTime();
    }

}

待办事项

  • PHP 7实现(找到一个优雅的方式来同时支持PHP 5和7)
  • 集成SpeedTrap并添加MemoryTrap
  • 研究xhprof集成以及针对特定方法/函数的调用次数/执行时间的断言

灵感

许可协议

phpunit-profile-asserts在MIT许可下提供。