almasmurad / stopwatch
测量代码执行时间的组件
0.4.4
2024-04-29 14:36 UTC
Requires
- php: ^7.0
- ext-mbstring: *
Requires (Dev)
- infection/infection: *
- mikey179/vfsstream: ^1.6
- phpbench/phpbench: ^0.14.0
- phpunit/php-code-coverage: ^5.3
- phpunit/phpunit: ^6.5
README
PHP工具,用于测量代码执行时间。
用法
// 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){ //... }
您可以在“报告对象”部分中了解更多关于报告对象的信息