Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track down slow code in PHP

I installed an open source PHP project on my Ubuntu server and got it running. However, pages load extremely slow. A phpinfo() page and a basic test page connecting to the mysql db all load very fast. There are tons of include files, log files and some curl calls in this open source code. What strategies/tools can I use to track down the source of the slowness?

like image 214
Thread7 Avatar asked Mar 06 '14 06:03

Thread7


People also ask

How can I get PHP code faster?

Enable OPcache on PHP server Thanks to OPcache which will cache the pre-compiled code and does not let it compile on later requests. Conducting the PHP performance test after integrating Opcache, some experts claim that PHP performance will be 3x faster and the load time will reduce much impressively.

What is PHP profiler?

PHP Code Performance Profiling A profiler is a tool that measures how some code consumes resources at run-time. It enables to find performance bottlenecks and understand the code's behavior. Blackfire Profiler for PHP enables to profile applications during development, test, staging, and production environments.


1 Answers

You need to profile pages, that have performance problems. It can be done with php-extension xdebug and kcachegrind.

Follow this steps:

  1. Install php5-xdebug package on your Ubuntu server via: sudo apt-get install php5-xdebug.
  2. Configure xdebug.profiler_enable_trigger parameter. Look at official manual.
  3. Install on your local computer kcachegrind package: sudo apt-get install kcachegrind.
  4. Start slow page with GET parameter XDEBUG_PROFILE=1. See related question.
  5. Copy generated profile log to your local comp and open it in kcachegrind.

Some notes:

  • After php5-xdebug package installation you'll need to restart http-server (or php-fpm daemon. Depends on installed software on your server).
  • Right place for profiling procedure is a test environment, not production.
like image 97
Alexander Yancharuk Avatar answered Oct 18 '22 13:10

Alexander Yancharuk