imkingdavid/load-timer

1.0.1 2018-11-30 20:23 UTC

This package is auto-updated.

Last update: 2024-08-29 03:06:16 UTC


README

这是一个简单的PHP加载时间测量类。这个类旨在遵循PHP FIG标准,所以如果你发现任何错误,请告知我,或者提交一个Pull Request来修复它(见下文)。

版权(c)2012 - David King (imkingdavid@gmail.com)

需求

PHP 5.4

许可

此产品可按需用于任何目的,无论是免费还是商业用途,没有任何限制。此产品按原样提供,不附带任何保证。

贡献

如果你有要添加的内容或看到有问题,请随意创建一个工单和/或提交一个Pull Request。我没有关于提交信息的具体指南,除了它们应该是简短并准确说明更改内容。

安装与使用

这是一个Composer启用的包。有关安装和使用Composer的信息,请参阅https://getcomposer.org.cn

将以下代码放在您页面的开头。

use imkingdavid\LoadTimer;

// If you are using Composer, you should include the composer autoload.php
// file; otherwise, include the following line:
// include('LoadTimer.php');

// Next, instantiate the class
$timer = new LoadTimer();

提示:类默认不会自动开始计时,除非构造函数的第一个参数设置为true,如下所示


$timer = new LoadTimer(true);


If you choose not to make the timer auto-start, you will need to start it
yourself. Go to the point in your file at which you wish to begin timing, and
add the following line:
```php
$timer->start();

提示:$timer->start()会覆盖计时器开始的时间。如果你希望它在满足某些条件时从脚本中的不同点开始,可以利用这个功能。

如果你想在代码中标记显著的点而不完全结束计时器,可以使用$timer->lap()方法。它接受一个字符串参数,仅描述此圈。默认为:Lap #,其中#是当前圈数加1。

// Important code section:
$timer->lap('Before execute');
$script->execute();
$timer->lap('After execute');

稍后,你可以使用$timer->laps()方法访问所有圈。它返回一个数组,如下所示

(
	[0]	=> Array
		(
			'Before execute', // Lap message
			'01234567', // Lap timestamp
		),
	[1]	=> Array
		(
			'After execute', // Lap message
			'12345678', // Lap timestamp
		)
);

最后,当你想结束计时器时,放置最后一行代码。

$timer->end();

注意,默认情况下,end()方法会返回加载时间。你也可以通过将end()的第一个参数设置为true来让它输出一个字符串。你可以通过将end()的第二个参数设置为包含%f的字符串来修改默认打印的字符串(当字符串打印时运行sprintf())。

$load_time = $timer->end(true, 'Page loaded in %f seconds.');