noack/php-spa

一个简单的性能调试/分析工具

v1.0.8 2022-03-09 12:06 UTC

This package is auto-updated.

Last update: 2024-09-06 14:14:20 UTC


README

简单的性能分析

使用方法

Profiler 需要设置 PROFILER_ACTIVE 环境变量为 true1 才能运行。然后,您需要将以下函数调用添加到您想要分析代码中

Profiler::start($timerName, $context = []);

使用给定的名称启动计时器并记录时间和内存使用情况。您可以在每个计时器中启动多个计时器以显示结果中的层次结构。您可以将上下文添加到计时器,以便在 HTML 结果中显示哪些数据可能导致代码缓慢。

Profiler::stop();

停止最后启动的计时器并将其放入 ProfilerStatistics

ProfilerStatistics::save();

将所有停止的计时器保存到 /tmp/php-pm/profiling/。您也可以通过 PROFILER_DIR 环境变量更改计时器保存的目录。

ResultGenerator::generate($saveDir);

生成包含在 PROFILER_DIR 中的所有已保存计时器的 HTML 结果,并将结果默认保存到 /tmp/php-pm/results/。您也可以通过 $saveDir 参数或 PROFILER_RESULTS_DIR 环境变量更改结果保存的目录。如果同时设置了 PROFILER_RESULTS_DIR 环境变量和 $saveDir 参数,则使用 $saveDir 参数。

示例

use php_spa\Profiler;
use php_spa\Generators\Html\ResultGenerator;
use php_spa\ProfilerStatistics;

require_once __DIR__ . "/vendor/autoload.php";

putenv('PROFILER_ACTIVE=true');
Profiler::start('doing something');
for ($index = 0;$index < 1000; $index++) {
    usleep(100);
}
Profiler::stop();

Profiler::start('im a parent timer');
for ($index = 0;$index < 100; $index++) {
    Profiler::start('im a child timer', ['index' => $index]);
    usleep(100);
    Profiler::stop();
}
Profiler::stop();

ProfilerStatistics::save();
ResultGenerator::generate();