laracraft-tech/laravel-xhprof

为您的Laravel应用程序轻松设置XHProf以进行性能分析!

v1.1.0 2024-03-07 19:38 UTC

This package is auto-updated.

Last update: 2024-09-08 10:25:35 UTC


README

Logo Laravel XHProf

Total Downloads Latest Stable Version License License

介绍

Laravel XHProf为您提供了一个简单的设置,以使用Facebook最初开发的知名XHProf PHP扩展对您的Laravel应用程序进行性能分析。它还引导您完成安装XHProf UI的步骤,这是一个用于可视化、保存和分析性能分析结果的UI。

安装

首先,您需要安装PHP扩展。强烈推荐使用ondrejs ppa。它维护良好,并提供几乎所有PHP版本。

普通环境

如果您有一个普通PHP环境,只需安装XHProf扩展

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php php-xhprof Graphviz
# You can now check if the extension was successfully installed
php -i | grep xhprof
# maybe restart your webserver or php-fpm...

注意:我们需要Graphviz来生成调用图。

Laravel Sail环境

如果您使用laravel sail,这里有一个为您准备的设置

sail up -d
sail artisan sail:publish
# in docker-compose.yml check wich php version is used under build->context (eg. ./docker/8.1)
# If you know the php-version you can type:
nano docker/<php-version>/Dockerfile
# find the block where all php extensions are installed and add "php<php-version>-xhprof graphviz \"
# Now you need to rebuild the sail
sail down ; sail build --no-cache ; sail up -d # this may take a while...
# You can now check if the extension was successfully installed
sail php -i | grep xhprof

注意:提供的Laravel Sail Dockerfile已经使用了ondrejs ppa。

安装包

composer require laracraft-tech/laravel-xhprof --dev
php artisan vendor:publish --provider="LaracraftTech\LaravelXhprof\XHProfServiceProvider" --tag="config"

安装UI

我们正在使用php.net推荐的由"preinheimer"提供的分支:https://php.ac.cn/manual/en/xhprof.requirements.php

mkdir public/vendor ; git clone git@github.com:preinheimer/xhprof.git ./public/vendor/xhprof
# If you haven't already, I recommend adding public/vendor to your .gitignore
echo "/public/vendor" >> .gitignore

数据库

由于UI包背后使用的数据库表名是硬编码为details,并且您可能已经有一个类似名称的表,因此您可能需要采取一些额外步骤。如果没有,这里首先是最简单的方法


假设您数据库中没有自己的details

php artisan vendor:publish --provider="LaracraftTech\LaravelXhprof\XHProfServiceProvider" --tag="migrations"
php artisan migrate

假设您数据库中已经有自己的details

我建议只是使用不同的数据库。

CREATE DATABASE xhprof;
USE xhprof;
CREATE TABLE IF NOT EXISTS `details` (
  `id` char(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `url` char(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `c_url` char(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `server name` char(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `perfdata` longblob,
  `type` tinyint DEFAULT NULL,
  `cookie` longblob,
  `post` longblob,
  `get` blob,
  `pmu` int DEFAULT NULL,
  `wt` int DEFAULT NULL,
  `cpu` int DEFAULT NULL,
  `server_id` char(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `aggregateCalls_include` char(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `details_url_index` (`url`),
  KEY `details_c_url_index` (`c_url`),
  KEY `details_cpu_index` (`cpu`),
  KEY `details_wt_index` (`wt`),
  KEY `details_pmu_index` (`pmu`),
  KEY `details_timestamp_index` (`timestamp`),
  KEY `details_server name_timestamp_index` (`server name`,`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

注意:您还需要创建一个具有该新数据库权限的用户!

配置

现在让我们配置一些设置!

cp public/vendor/xhprof/xhprof_lib/config.sample.php public/vendor/xhprof/xhprof_lib/config.php
# 1. Change the DB credentials to your needs
# 2. enable dot_binary section
# 3. If you're local, set $controlIPs to false
nano public/vendor/xhprof/xhprof_lib/config.php

使用方法

只需在您的.env文件中将XHPROF_ENABLED=true设置,现在您对应用程序的每个请求都会进行性能分析。
访问<your-host>/vendor/xhprof/xhprof_html/以查看您的性能分析结果。

祝您分析愉快!

安全

如果您发现任何与安全相关的问题,请通过电子邮件zacharias.creutznacher@gmail.com报告,而不是使用问题跟踪器。

鸣谢

许可证

MIT。请参阅许可证文件以获取更多信息。