承接 petrknap/profiler 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

petrknap/profiler

最新稳定版本:v2.2.0

Composer 安装命令:

composer require petrknap/profiler

包简介

PHP profiler for short-term & long-term profiling

README 文档

README

This tool allows you to monitor performance and detect memory leaks as well as inconsistent performance behavior of your application over time.

Basic profiling

For basic profiling you can use a profiling helper. The Profiling will allow you to profile between start and finish methods calls.

namespace PetrKnap\Profiler;

$profiling = Profiling::start();
// do something
$profile = $profiling->finish();

printf('It took %.1f s to do something.', $profile->getDuration());

The Profiling is simple and cannot be turned on and off easily. So a profiler was created for the purpose of hard-coded more complex profiling.

Complex profiling

Request a profiler as a dependency and call a profile method on it.

namespace PetrKnap\Profiler;

function doSomething(ProfilerInterface $profiler): string {
    return $profiler->profile(function (): string {
        return 'something';
    })->process(fn (ProfileInterface $profile) => printf(
        'It took %.1f s to do something.',
        $profile->getDuration(),
    ));
}

How to enable / disable it

It can be easily enabled, or disabled through the DI, which provides either the Profiler or the NullProfiler.

namespace PetrKnap\Profiler;

echo doSomething(new Profiler());
echo doSomething(new NullProfiler());

Useful features

Take snapshot

If you need to measure the current values, just call the takeSnapshot method on the Profiling, or a profiler.

namespace PetrKnap\Profiler;

$profiling = Profiling::start();
// do something
$profiling->takeSnapshot();
// do something more
$profile = $profiling->finish();

printf('There are %d memory usage records.', count($profile->getMemoryUsages()));

If you want to automate it then take snapshot on tick. Or you can use a more practical cascade profiling.

Take snapshot on tick

For greater precision, you can take snapshot on each N tick.

declare(ticks=2); // this declaration is important (N=2)

namespace PetrKnap\Profiler;

$profiling = Profiling::start(takeSnapshotOnTick: true);
(fn () => 'something')();
$profile = $profiling->finish();

printf('There are %d memory usage records.', count($profile->getMemoryUsages()));

This will result in very detailed code tracking, which can degrade the performance of the monitored application.

Cascade profiling

The profile method provides you a nested profiler that you can use for more detailed cascade profiling.

namespace PetrKnap\Profiler;

$profile = (new Profiler())->profile(function (ProfilerInterface $profiler): void {
    // do something
    $profiler->profile(function (): void {
        // do something more
    });
});

printf('There are %d memory usage records.', count($profile->getMemoryUsages()));

Run composer require petrknap/profiler to install it. You can support this project via donation. The project is licensed under the terms of the LGPL-3.0-or-later.

统计信息

  • 总下载量: 846
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2024-10-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固