tavgear/benchmark

一种灵活的实用工具,用于使用可定制的计时器测量代码的执行时间。支持各种输出格式。用于初始调试和代码优化。

v1.0.1 2020-07-22 19:08 UTC

This package is auto-updated.

Last update: 2024-09-23 15:07:19 UTC


README

Latest Stable Version

一个轻量级、易于使用的基准工具,用于使用可定制的计时器测量代码部分的执行时间。支持不同格式的结果输出。

用于初始调试和代码优化。

安装

$ composer require tavgear/benchmark

用法

<?php

use Tvg\Bench,
    Tvg\Interval;

默认计时器。

Bench::start(); // start default timer

usleep(100000);

echo Bench::get(); // get time elapsed from start of default timer to this point in seconds and milliseconds
// 00:100

usleep(200000);

echo Bench::get()->format(); // same, but with an explicit call to convert to a formatted string
// 00:301

usleep(800000);

echo Bench::stop()->format(Interval::FORMAT_MICRO); // stop default timer and get time with microseconds
// 01s 101ms 234us

$timeInfo = Bench::get()->detail(); // get detailed time from stopped default timer
//Array
//(
//    [hours] => 0
//    [minutes] => 0
//    [seconds] => 1
//    [ms] => 101
//    [mk] => 234
//)

使用命名计时器来测量代码不同部分的运行速度。

// Measuring the execution time of two blocks of code using named timers
Bench::start('block1');
usleep(200000);
Bench::stop('block1');

Bench::start('block2');
usleep(500000);
Bench::stop('block2');

// Get sorted results for all timers
foreach (Bench::getAll(Bench::SORT_ASC) as $timerName => $interval) {
    echo $timerName . ': ' . $interval->format();
}
// block1: 00:200
// block2: 00:500
// _default: 01:101

快速测量函数和方法性能。

// Some function
function wait($seconds){
    sleep($seconds);
}

// Measuring the execution time of some function (any callable type)
echo Bench::measure('wait', $r, 1)->format();
// 01:000

附加功能

// Get time from start script (request) to current point
echo Bench::getFromRequest();
// 02:809

// Get the size of memory used by the system process. Including memory used by all resource types.
echo Bench::getProcessMemoryUsage();

许可证

Benchmark 在 MIT 许可证下授权