sandermuller/stopwatch

用于测量 Laravel 和 PHP 项目执行时间的计时器(性能分析代码)

v0.1.4 2024-08-23 23:10 UTC

This package is auto-updated.

Last update: 2024-09-23 23:18:57 UTC


README

轻松分析应用程序/代码的各个部分并测量性能以揭示瓶颈

安装

您可以通过 composer 安装此包

composer require sandermuller/stopwatch

使用方法

启动计时器

use SanderMuller\Stopwatch\Stopwatch;

$stopwatch = Stopwatch::start();

添加圈/检查点

use SanderMuller\Stopwatch\Stopwatch;

$stopwatch = Stopwatch::start();

$stopwatch->checkpoint('First checkpoint');
// Or
$stopwatch->lap('Second checkpoint');

显示总运行时间

use SanderMuller\Stopwatch\Stopwatch;

$stopwatch = Stopwatch::start();

// Do something

echo $stopwatch->toString();
// or
echo (string) $stopwatch;
// Echoes something like: 116ms

以 HTML 格式渲染

渲染整洁的 HTML 输出,显示总执行时间、每个检查点和每个检查点之间的时间。

耗时最多的检查点将被突出显示。

use SanderMuller\Stopwatch\Stopwatch;

$stopwatch = Stopwatch::start();

// Do something

$stopwatch->checkpoint('First checkpoint');

// Do something more

$stopwatch->checkpoint('Second checkpoint');

echo $stopwatch->toHtml();
// Or in Laravel
{{ $stopwatch }}

rendered-stopwatch.png

手动停止计时器

您可以手动停止计时器,但当使用 Stopwatch 输出时(例如,当您输出 Stopwatch 对象或调用 ->totalRunDuration())它也会自动停止。

use SanderMuller\Stopwatch\Stopwatch;

$stopwatch = Stopwatch::start();

// Do something

$stopwatch->stop();

// Do something you don't want to measure

// Finally render the output
echo $stopwatch->toHtml();