almasmurad/stopwatch

测量代码执行时间的组件

0.4.4 2024-04-29 14:36 UTC

This package is auto-updated.

Last update: 2024-09-29 15:27:43 UTC


README

PHP工具,用于测量代码执行时间。

俄语README

用法

// create Stopwatch
$stopwatch = new Almasmurad\Stopwatch\Stopwatch();

// start measuring the first step
$stopwatch->start('code section #1');
//...

// start measuring the second step
$stopwatch->step('code section #2');
//...

// start measuring the third step
$stopwatch->step('code section #3');
//...

// finish measuring
$stopwatch->finish();

// output the report
$stopwatch->report();

因此,以下报告会输出到标准输出

 Start           | 06:55:14.123 
-----------------|--------------
 code section #1 |      0.198 s 
 code section #2 |      0.209 s 
 code section #3 |      1.200 s 
-----------------|--------------
 Summary         |      1.607 s 

最小使用方法

$stopwatch = new Almasmurad\Stopwatch\Stopwatch();

//... (measured code)

$stopwatch->report();

因此,以下报告会输出到标准输出

 Start   | 06:55:14.123 
---------|--------------
 Summary |      0.198 s 

将报告输出到文件

$stopwatch->reportToFile(__DIR__.'/report.txt');

报告将被写入文件,且不会输出到标准输出。如果文件已存在,报告将替换其内容。若要使报告追加文件内容,请使用其他方法

$stopwatch->reportToFileInAppendMode(__DIR__.'/report.txt');

访问报告组件

除了将报告输出到标准流或文件,您还可以访问其组件

// getting the report object
$report = $stopwatch->getReport();

// general indicators
$start   = $report->getStart()->getTimestamp();   // stopwatch start time
$finish  = $report->getFinish()->getTimestamp();  // stopwatch finish time
$elapsed = $report->getElapsed()->getSeconds();   // time of the entire stopwatch operation

// indicators of first code section 
$step1_start   = $report->getStepByIndex(0)->getStart()->getTimestamp();   
$step1_finish  = $report->getStepByIndex(0)->getFinish()->getTimestamp();  
$step1_elapsed = $report->getStep('code section #1')->getElapsed()->getSeconds();

// all of code sections
foreach ($report->getSteps() as $step){
    //...
}

您可以在“报告对象”部分中了解更多关于报告对象的信息

了解更多