best-served-cold/benchmark

基准测试 - PHP 独立基准测试类。

1.0.4 2017-03-12 15:15 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:03:49 UTC


README

Build Status Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight Latest Stable Version

基准测试

安装

$ composer require best-served-cold/benchmark

使用方法

调用基准测试和输出类

use BestServedCold\Benchmark\Benchmark;
use BestServedCold\Benchmark\Output;

然后测试一些代码

Benchmark::start();
for ($i = 0; $i <= 100; $i++ ) {
    mt_rand(1, 1000);
}
Benchmark::stop();
Output::output(Benchmark::get())->render();

返回类似这样的结果

+---------------+-------------------+--------------------+
| Name          | Metric            | Value              |
+---------------+-------------------+--------------------+
| 58c4302514614 | MicroTime         | 0.0022561550140381 |
| 58c4302514614 | MemoryUsage       | 160.05 KB          |
| 58c4302514614 | DeclaredInterface | 0                  |
| 58c4302514614 | DeclaredTrait     | 2                  |
| 58c4302514614 | DefinedFunction   | 0                  |
| 58c4302514614 | DefinedConstant   | 0                  |
| 58c4302514614 | IncludedFile      | 1                  |
| 58c4302514614 | DeclaredClass     | 0                  |
| 58c4302514614 | PeakMemoryUsage   | 1.54 MB            |
+---------------+-------------------+--------------------+

由于未指定 $name 参数,将提供一个哈希名称作为标识。

命名基准测试

Benchmark::reset(); // Clear existing benchmarks
Benchmark::start('bob');
$a = str_repeat('Hello', 24000);
define('FOO', 'bar');
Benchmark::stop('bob');
Benchmark::start('mary');
require_once('./SomeClass.php');
sleep(1);
Benchmark::stop('mary');

返回类似这样的结果

+------+-------------------+--------------------+
| Name | Metric            | Value              |
+------+-------------------+--------------------+
| bob  | MicroTime         | 0.0014297962188721 |
| bob  | MemoryUsage       | 191.77 KB          |
| bob  | DeclaredInterface | 0                  |
| bob  | DeclaredTrait     | 0                  |
| bob  | DefinedFunction   | 0                  |
| bob  | DefinedConstant   | 1                  |
| bob  | IncludedFile      | 0                  |
| bob  | DeclaredClass     | 0                  |
| bob  | PeakMemoryUsage   | 2.18 MB            |
+------+-------------------+--------------------+
| mary | MicroTime         | 1.0012910366058    |
| mary | MemoryUsage       | 75.48 KB           |
| mary | DeclaredInterface | 0                  |
| mary | DeclaredTrait     | 0                  |
| mary | DefinedFunction   | 0                  |
| mary | DefinedConstant   | 0                  |
| mary | IncludedFile      | 1                  |
| mary | DeclaredClass     | 1                  |
| mary | PeakMemoryUsage   | 2.18 MB            |
+------+-------------------+--------------------+

指定输出接口

Benchmark::reset();
Benchmark::start('susan');
require_once('./SomeTrait.php');
Benchmark::stop('susan');
Output::output(Benchmark::get(), 'apache')->render();

返回类似这样的结果