PhpDev.App
laracraft-tech/laravel-xhprof

laracraft-tech/laravel-xhprof

Stars: 158

Forks: 9

Pull Requests: 2

Issues: 12

Watchers: 3

Last Updated: 2023-04-05 11:17:45

Easy XHProf setup to profile your laravel application!

License: MIT License

Languages: PHP

Logo Laravel XHProf

Total Downloads Latest Stable Version License

Introduction

Laravel XHProf provides you with a simple setup to profile your laravel application with the well known XHProf PHP extension originally developed by facebook. It also leads you through the steps to install XHProf UI, a UI to visualize, save and analyze the results of the profiling.

Installation

First you'll need to install the PHP extension. It's highly recommended using ondrejs ppa. It's well maintained and provides quite all PHP versions.

Normal environment

If you have a normal PHP environment, just install the XHProf extension:

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...

Note: we need graphviz to generate callgraphs.

Laravel Sail environment

If you are using laravel sail, here's a setup for you:

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 extionsions are installed and add "php<php-version>-xhprof graphviz \"
# now you need to rebuild 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

Note: The provided Laravel Sail Dockerfile already uses ondrejs ppa.

Install the Package

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

Install the UI

We are using the recommended fork by php.net from "preinheimer": https://www.php.net/manual/en/xhprof.requirements.php

mkdir public/vendor ; git clone [email protected]:preinheimer/xhprof.git ./public/vendor/xhprof
# if you havent already, I recommend adding public/vendor to your .gitignore
echo "/public/vendor" >> .gitignore

Database

Since the database table name, which the UI package is using behind to store and read data from the database, is hard coded to details and you already may have a table named like that, you may need to make some additional steps. If not, here at first the simple way:


In case you DON'T already HAVE an own details table in your database:

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

In case you already HAVE an own details table in your database:

I recommend to just use a different database.

CREATE DATABASE xhprof;
USE xhprof;
CREATE TABLE IF NOT EXISTS `details` (
  `idcount` int(11) NOT NULL AUTO_INCREMENT,
  `id` char(64) NOT NULL,
  `url` varchar(255) DEFAULT NULL,
  `c_url` varchar(255) DEFAULT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `server name` varchar(64) DEFAULT NULL,
  `perfdata` mediumblob,
  `type` tinyint(4) DEFAULT NULL,
  `cookie` blob,
  `post` blob,
  `get` blob,
  `pmu` int(11) DEFAULT NULL,
  `wt` int(11) DEFAULT NULL,
  `cpu` int(11) DEFAULT NULL,
  `server_id` char(64) DEFAULT NULL,
  `aggregateCalls_include` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`idcount`),
  KEY `url` (`url`),
  KEY `c_url` (`c_url`),
  KEY `cpu` (`cpu`),
  KEY `wt` (`wt`),
  KEY `pmu` (`pmu`),
  KEY `timestamp` (`timestamp`),
  KEY `Aggregation of the last n entries by Servername` (`server name`(5),`timestamp`)
);

Note: you also need to create a user which has privileges on that new database!

Config

Now let's configure some settings!

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

Usage

Just set XHPROF_ENABLED=true in your .env file and now every request you make to your application gets profiled.
Visit <your-host>/vendor/xhprof/xhprof_html/ to see your profiling results.

Happy analysing!

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.