Profiling PHP Code: Finding Bottlenecks in Practice

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Profiling PHP Code: Finding Bottlenecks in Practice

\n

The performance of PHP applications directly affects user experience and server hardware costs. Developers often encounter situations where a site runs slowly, but the cause is not obvious. In this article, we will explore practical methods for profiling PHP code that will help identify bottlenecks and speed up your application.

\n\n

What is profiling and why is it needed?

\n

Profiling is the process of collecting data about program execution: function execution time, number of calls, memory usage, and I/O. Unlike simple logging, a profiler builds a complete picture of script execution.

\n

Main goals of PHP profiling:

\n
    \n
  • Identifying slow functions and methods
  • \n
  • Detecting memory leaks
  • \n
  • Analyzing the number of database queries
  • \n
  • Optimizing page load times
  • \n
\n\n

Built-in PHP Profiling Tools

\n

PHP provides several built-in functions for initial performance analysis. Let's look at the most useful ones.

\n\n

The microtime(true) function

\n

The simplest way to measure the execution time of a code block is to use microtime(true):

\n
<?php\n$start = microtime(true);\n\n// Your code\n$data = [];\nfor ($i = 0; $i < 10000; $i++) {\n    $data[] = $i * $i;\n}\n\n$end = microtime(true);\n$executionTime = ($end - $start) * 1000;\necho "Execution time: " . round($executionTime, 2) . " ms";\n?>
\n\n

The memory_get_usage() function

\n

To track memory consumption, use memory_get_usage() and memory_get_peak_usage():

\n
<?php\necho "Memory used: " . memory_get_usage() . " bytes\\n";\necho "Peak usage: " . memory_get_peak_usage() . " bytes";\n?>
\n\n

The DateTime and DateInterval classes

\n

For more accurate measurements of long-running operations, use DateTime objects:

\n
<?php\n$start = new DateTime();\n\n// Long operation\nsleep(2);\n\n$end = new DateTime();\n$interval = $start->diff($end);\necho "Elapsed: " . $interval->format('%s seconds');\n?>
\n\n

Professional Profiling Tools

\n

For serious optimization, built-in tools are not enough. Let's look at three popular tools.

\n\n

Xdebug — the classic profiling tool

\n

Xdebug is a PHP extension that generates detailed profiles in cachegrind format. Installation via Composer:

\n
pecl install xdebug
\n

After installation, enable profiling in php.ini:

\n
xdebug.mode = profile\nxdebug.output_dir = /tmp/profiler
\n

To analyze the generated files, use QCacheGrind (Linux) or WinCacheGrind (Windows). Example profile output:

\n
    \n
  • Execution time of each function call
  • \n
  • Number of calls
  • \n
  • Call hierarchy
  • \n
  • Memory consumption
  • \n
\n\n

Blackfire.io — cloud profiler

\n

Blackfire.io from SensioLabs (creators of Symfony) offers advanced analysis with visualization:

\n
# Agent installation\ncurl -s https://blackfire.io/install.sh | bash\n\n# Profiling via API\n$blackfire = new \\Blackfire\\Client();\n$probe = $blackfire->createProbe();\n\n// Your code\n$users = User::findAll();\n\n$blackfire->endProbe($probe);
\n

Blackfire automatically shows:

\n
    \n
  • Optimization recommendations
  • \n
  • Comparison with previous profiles
  • \n
  • SQL query analysis
  • \n
\n\n

Tideways — for production environments

\n

Tideways is a lightweight profiler that works in real-time:

\n
composer require tideways/profiler\n\n// Enable profiling\ntideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_MEMORY | TIDEWAYS_XHPROF_FLAGS_CPU);\n\n// Your code\n$result = heavyFunction();\n\n$data = tideways_xhprof_disable();\                

Blogs

Book Recommendations