Giter Club home page Giter Club logo

laravel-xhprof's Introduction

Logo Laravel XHProf

Total Downloads Latest Stable Version License 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 to use 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 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

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 haven't 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 take some additional steps. If not, here at first the simple way:


In case you DON'T already HAVE your 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 just using a different database.

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;

Note: you also need to create a user who 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 analyzing!

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.

laravel-xhprof's People

Contributors

a-bashtannik avatar imanghafoori1 avatar sairahcaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-xhprof's Issues

Fix the migration

While working with postgresql, I faced a small problem running the migration,

   INFO  Running migrations.

  2022_10_25_094737_create_xhprof_table .................................................................................................. 42ms FAIL

   Illuminate\Database\QueryException

  SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "ON"
LINE 1: ...hout time zone not null default CURRENT_TIMESTAMP ON UPDATE ...
                                                             ^ (SQL: create table "details" ("idcount" bigserial primary key not null, "id" char(64) not null, "url" char(255) null, "c_url" char(255) null, "timestamp" timestamp(0) without time zone not null default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, "server name" char(64) null, "perfdata" bytea null, "type" smallint null, "cookie" bytea null, "post" bytea null, "get" bytea null, "pmu" integer null, "wt" integer null, "cpu" integer null, "server_id" char(64) null, "aggregateCalls_include" char(255) null))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +9 vendor frames
  10  database/migrations/2022_10_25_094737_create_xhprof_table.php:41
      Illuminate\Support\Facades\Facade::__callStatic("create")

      +26 vendor frames
  37  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

so I guess it is better to change

$table->timestamp('timestamp')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

to

$table->timestamp('timestamp')->useCurrent()->useCurrentOnUpdate();

which in my case fixed the problem :D

Suggest adding a UI for XHProf reports to your library

Dear developers of laravel-xhprof,

I am a user of your library and I appreciate the work you have put into creating this valuable tool for profiling Laravel applications. I have noticed that the library currently does not include a user interface for viewing XHProf reports. Instead, users need to download and configure the UI separately.

I would like to suggest that you consider adding a UI for XHProf reports to your library. One possible UI that could be included is the one available at [email protected]:preinheimer/xhprof.git.

Including a UI in your library would make it much easier for users to get up and running with XHProf. It would also streamline the configuration process, as users would not need to manually add configuration files to their Laravel installation.

Thank you for considering my suggestion. I look forward to seeing how your library continues to evolve and improve in the future.

Best regards,

[fh32000]

Different Database

Need to add in config to select Different connection of database and add to migrations Schema::connection(config('connection))->create

sudo: add-apt-repository: command not found

When trying to install the extension, I get the following error on Mac OS Monterey

➜  ~ sudo add-apt-repository ppa:ondrej/php

Password:
sudo: add-apt-repository: command not found
➜  ~ 


[1.x] id column has not index!

Version

v1.0.9

Database Driver

MySQL

Description

I created details table using the following command:
php artisan vendor:publish --provider="LaracraftTech\LaravelXhprof\XHProfServiceProvider" --tag="migrations"

I was able to view the xhprof page the first time, but after a while, the xhprof page timed out. I found the following slow query in the MySQL slow query log:

`SELECT * FROM `details` WHERE `id` = '651e3280bba53';`

OMG, I discovered that the id column in the details table does not have an index!

The id column requires an index, perhaps even a unique key instead of just an index.

How about encapsulating the frontend part?

Hey

I have some ideas on how to make the package better considering you depend on https://github.com/preinheimer/xhprof - one of the ugliest things I have ever seen lol.

How about making 2 things:

  • Encapsulate the https://github.com/preinheimer/xhprof into a resource package to avoid repository clone operation.
  • Make everything configurable in a standard laravel way - today we have to keep 2 copies of the configuration, one for the application and one for the cloned ignored directory.

We can start on these changes and move forward step by step making the profiling process great.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.