zdenekdrahos/profiler-tools

此包已被废弃且不再维护。没有建议的替代包。

辅助函数 - 停表计时器、记录器和时间转换器(秒转换为可读字符串)

v1.2.0 2015-02-15 09:01 UTC

This package is auto-updated.

Last update: 2022-05-24 00:03:50 UTC


README

停表计时器、CSV记录器和时间转换器(秒转换为可读字符串)。

安装

composer.json 中添加以下内容

{
    "require": {
        "zdenekdrahos/profiler-tools": "*"
    }
}

使用方法

// start stopwatch
$stopwatch = ProfilerTools\stopwatch();

// execute something

// stop timer
list($start, $end, $elapsedSeconds) = $stopwatch();

// log execution time
appendCsvLine(
    'log.csv'
    array(
        $start->format('c'),
        $end->format('c'),
        ProfilerTools\secondsToDays($elapsedSeconds)
    )
);

记录器将以下行追加到 log.csv

2015-02-01T09:15:17+01:00,2015-02-01T09:15:18+01:00,1.1s

无时间耦合

execute something 是上一个示例中的难点。你可以轻易地耦合到开始和停止计时器。你可以使用传递闭包到 monitorExecution,它将返回 执行报告。查看带有 隐藏 计时器的示例

$report = ProfilerTools\monitorExecution(function() {
    // execute something
});
ProfilerTools\appendCsvLine('log.csv', array(
    $report->dateStart->format('c'),
    $report->dateFinish->format('c'),
    $report->convertSecondsToReadableString(),
    $report->elapsedSeconds,
    $report->hasFailed() ? $report->exception->getMessage() : ''
));

停表计时器

  • $stopwatch = ProfilerTools\stopwatch() - 开始计时器并返回停止计时器的函数
  • $stopwatch() - 返回开始/结束日期和经过的秒数
  • $report = ProfilerTools\monitorExecution(closure) - 监控函数调用并返回报告

记录器

  • ProfilerTools\appendCsvLine($file, array $row) - 将数组转换为行并追加该行
  • ProfilerTools\appendCsvLines($file, array $rows) - 在一次写操作中追加N行
  • ProfilerTools\clearLog($file) - 删除文件中的现有内容

时间转换器

  • ProfilerTools\secondsToDays($elapsedSeconds, $precision) - 将秒转换为可读格式,可选的毫秒精度

示例

秒数 .00s .0s
0.1546456 0.15s 0.2s
9 9s 9s
19.7878 19.79s 19.8s
65 1m 5s 1m 5s
374 6m 14s 6m 14s
12805.9 3h 33m 25.9s 3h 33m 25.9s
86922.298 1d 8m 42.3s 1d 8m 42.3s

许可

版权所有 (c) 2015 Zdeněk Drahoš。MIT 许可,有关详细信息请参阅 LICENSE