kehikko/profiler

kehikko 框架的性能分析

1.0.2 2018-12-16 09:35 UTC

This package is auto-updated.

Last update: 2024-09-16 22:40:17 UTC


README

Header

PHP 通用性能分析实现。

具有基于浏览器的 UI,用于查看性能分析调用。

已在 Ubuntu 18.04 上与 tidewaysPHP 7.2 测试过。

所有代码都基于一个使用 xhprofPHP 5.x 的较旧实现(已在 Ubuntu 14.04CentOS/cPanel v??? 上测试和使用过)。从 kehikko v1 框架开始时,将其代码移植为一个独立的版本。

即使没有对核心功能进行更改,也应继续在 PHP 5.x 上运行,但我在从 Composer 安装时将 PHP 7.0 作为要求。

要求

PHP 7.0、PHP 性能分析扩展(如下所示)和命令 dot(来自 GraphViz)。Twig 是可选的,因为它可以通过 Composer 安装。

以下 PHP 扩展之一是必需的

  • tideways
  • uprofiler
  • xhprof

Ubuntu 18.04 上,您可以通过以下方式安装所需的依赖项

apt install php-tideways graphviz
phpenmod tideways

简单的测试示例

在安装了 tideways 和 graphviz 之后执行以下操作

git clone https://github.com/kehikko/profiler.git
cd profiler
composer install
php example/example.php
php -S localhost:8080 web/index.php

如果您在本地计算机上执行了此操作,则应能够通过浏览到网址 https://:8080 来查看一些结果。

安装

要将此性能分析器安装到现有项目中,请运行

composer require kehikko/profiler

如果您打算在不使用 Composer 的情况下使用此性能分析器,则需要手动安装 Twig,以便在运行此性能分析器代码时自动加载。

写入性能分析数据的部分不需要 Twig。只有用于查看性能分析调用的部分需要 Twig

注意:通常您可能不想将性能分析器安装到项目中,它应像在 简单测试示例 中一样安装在其他位置,并且仅从那里将 profiler.php 文件包含到项目中,仅用于本地测试安装。

设置

生成性能分析数据

从您的代码中调用 profiler_start

/* optional profiler.php include, done automatically when using composer autoloader */
//require_once '/profiler/install/dir/profiler.php';

/* do this somewhere in your startup code,
 * path to profiler data directory is optional, default shown here
 */
profiler_start('/tmp/kehikko-php-profiler');

稍后调用 profiler_stop(或者不调用,并在 PHP 执行停止时自动调用)

profiler_stop();

您还可以调用 profiler_running 来查看性能分析器是否已启动

if (profiler_running() && environment_is_in_production()) {
    error_log('running profiler in production environment, stop now!');
    die;
}

Apache

在 Apache 中启用 mod_rewrite 并将以下内容添加/设置为您的公共 Web 目录中的 .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^_profiler(/.*)?$ profiler.php [NC,L,NE]

在同一个目录中创建一个名为 profiler.php 的 PHP 文件,并包含以下内容

<?php

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

/* show errors in browser, easier this way, this is not production stuff anyways */
error_reporting(E_ALL);
ini_set('display_errors', 1);

/* this should be given to later functions so that they are able to generate links correctly */
$root_url = '/_profiler/';
/* parse profiler "route" url */
$request_url = trim(substr($_SERVER['REQUEST_URI'], strlen($root_url)), '/');

/* path where profiling data is saved (same as given to profiler_start()), following is default */
$datapath = '/tmp/kehikko-php-profiler';
/* limit shown profiling entries to this number, default is 20 */
$limit = 20;

/* "route" */
if ($request_url == '') {
    /* to index */
    profiler_html_index($root_url, $datapath, $limit);
} else {
    /* to single call profile */
    $parts = explode('/', $request_url);
    $id    = array_pop($parts);
    if (strpos($request_url, 'graph/') === 0) {
        /* generate svg call graph */
        profiler_svg_graph_generate($id, $datapath);
    } else if (strpos($request_url, 'callgraph/') === 0) {
        /* view call graph */
        profiler_html_profile_call_graph($id, $root_url, $datapath);
    } else {
        /* view call profile */
        profiler_html_profile($id, $root_url, $datapath);
    }
}

现在,当您浏览到 http://your.web.server/_profiler 时,您可以看到您的性能分析调用。

注意:此脚本假设 Composer 安装包含库的 vendor 目录位于其所在目录的下一级。

注意:至少在 Ubuntu 18.04 中,systemd 强制将 PHP 中的 /tmp/ 设置为 Apache 的不同位置,通常是 /tmp/systemd-private-*-apache2.service-*/。当从命令行运行 PHP 时并非如此,因此在命令行中进行性能分析时,如果您的 Apache 侧没有显示结果,请不要感到困惑。

屏幕截图

配置文件

Profiles

配置文件

Profile

调用图

Call graph