wdalmut/php-bench

以简单的方式基准测试您的应用程序方法

0.1.2 2015-10-24 10:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:58:34 UTC


README

Build Status

只是简单地基准测试您的类方法或函数。

./vendor/bin/bench run tests/

或者使用不同的路径

./vendor/bin/bench run tests/ benchs/

声明基准测试方法

只需在类中创建一个以 benchmark 关键字开头的方法,或在您的文档块中使用简单的 @benchmark 注解。

class MyClass
{
    ...

    public function benchmarkUsingTheMethodName($b)
    {
        for ($i=0; $i<$b->times(); $i++) {
            my_project_function("%s", "hello");
        }
    }

    /**
     * @benchmark
     */
    public function this_bench_instead_use_the_annotation($b) {
        for ($i=0; $i<$b->times(); $i++) {
            $myObj->slowMethod("stub");
        }
    }
}

benchmark 方法接收一个外部传入的 b 对象,其中包含您的基准函数应运行的迭代次数。

PHPUnit 集成

只需在测试用例中添加一个 benchmark 方法。

class MyTest extends \PHPUnit_Framework_TestCase
{
    // Executed only by php-bench
    public function benchmarkMyAppMethod($b)
    {
        for ($i<0; $i<$b->times(); $i++) {
            sprintf("%s", "hello");
        }
    }

    public function testMyAppMethod()
    {
        // ...
        $this->assertEquals(...);
    }
}

使用 composer 安装

您可以使用 composer 来获取本库的本地副本

{
    "require-dev": {
        "wdalmut/php-bench": "*"
    }
}

灵感来源于 Golang 基准测试库