Profiling PHP Code: Finding Bottlenecks in Practice
\nThe 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\nWhat is profiling and why is it needed?
\nProfiling 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.
\nMain 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
Built-in PHP Profiling Tools
\nPHP provides several built-in functions for initial performance analysis. Let's look at the most useful ones.
\n\nThe microtime(true) function
\nThe simplest way to measure the execution time of a code block is to use microtime(true):
<?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\nThe memory_get_usage() function
\nTo track memory consumption, use memory_get_usage() and memory_get_peak_usage():
<?php\necho "Memory used: " . memory_get_usage() . " bytes\\n";\necho "Peak usage: " . memory_get_peak_usage() . " bytes";\n?>\n\nThe DateTime and DateInterval classes
\nFor 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\nProfessional Profiling Tools
\nFor serious optimization, built-in tools are not enough. Let's look at three popular tools.
\n\nXdebug — the classic profiling tool
\nXdebug is a PHP extension that generates detailed profiles in cachegrind format. Installation via Composer:
\npecl install xdebug\nAfter installation, enable profiling in php.ini:
\nxdebug.mode = profile\nxdebug.output_dir = /tmp/profiler\nTo 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
Blackfire.io — cloud profiler
\nBlackfire.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);\nBlackfire automatically shows:
\n- \n
- Optimization recommendations \n
- Comparison with previous profiles \n
- SQL query analysis \n
Tideways — for production environments
\nTideways is a lightweight profiler that works in real-time:
\ncomposer 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();\