irap/profiling

适用于PHP 5.6+的性能分析库

1.0.1 2018-06-12 11:10 UTC

This package is auto-updated.

Last update: 2024-09-24 02:39:13 UTC


README

这是一个PHP包,可以更轻松地找到占用最多时间的函数/区域。

安装

使用composer安装

composer require irap/profiling

用法

以下是一个示例脚本,展示了如何使用此工具

<?php

# Include the autoloader for packages.
require_once(__DIR__ . '/vendor/autoload.php');

function Bar()
{
    \iRAP\Profiling\FunctionAnalyzer::start();
    sleep(3);
    \iRAP\Profiling\FunctionAnalyzer::stop();
}

function Foo()
{
    \iRAP\Profiling\FunctionAnalyzer::start();
    sleep(1);
    Bar();
    \iRAP\Profiling\FunctionAnalyzer::stop();
}

Foo();
print \iRAP\Profiling\FunctionAnalyzer::getResults();

它应该输出类似以下内容

Foo: 1.0001013278961 seconds
Bar: 3.0002498626709 seconds

请注意,尽管 BarFoo 内部被调用,因此 Foo 执行总耗时4秒,但 Foo 的结果仅为1秒,因为工具显示的是在 Foo 中进行逻辑操作所花费的时间,而不是在 Bar 中,因为 Bar 已经被单独分析。如果您想要包括 Bar 在内的 Foo 的总耗时,只需将分析器调用从 Bar 方法中移除即可。

<?php

# Include the autoloader for packages.
require_once(__DIR__ . '/vendor/autoload.php');

function Bar()
{
    sleep(3);
}

function Foo()
{
    \iRAP\Profiling\FunctionAnalyzer::start();
    sleep(1);
    Bar();
    \iRAP\Profiling\FunctionAnalyzer::stop();
}

Foo();
print \iRAP\Profiling\FunctionAnalyzer::getResults();

输出

Foo: 4.0003681182861 seconds

对小块代码进行性能分析 - 自定义名称

如果您有一个非常长的函数,并且想要分析它的各个部分,那么您可以为 startstop 方法提供自定义名称,如下所示

require_once(__DIR__ . '/vendor/autoload.php');

function Bar() { sleep(3); }
function Foo() { sleep(1); }

function main()
{
    \iRAP\Profiling\FunctionAnalyzer::start('part1');
    Foo();
    \iRAP\Profiling\FunctionAnalyzer::stop('part1');

    // Profiling part 2
    \iRAP\Profiling\FunctionAnalyzer::start('part2');
    Bar();
    Foo();
    \iRAP\Profiling\FunctionAnalyzer::stop('part2');
}

main();
print \iRAP\Profiling\FunctionAnalyzer::getResults();

... 它将输出

part1: 1.0000782012939 seconds
part2: 4.0001401901245 seconds