Answer the question
In order to leave comments, you need to log in
How to find out what part of the PHP code loads the CPU on the server?
I have a VDS with centos 7
, I want to optimize a php script, with a large number of calls it heavily loads the CPU
, how can I find those lines of code that affect this?
Answer the question
In order to leave comments, you need to log in
Google "PHP profiling"
XDebug, XHprof, Blackfire...
As well as "load testing"
jmeter...
For a quick test of short scenarios: take measurements, cut a piece, repeat measurements.
Typical places with problems:
- session based access to the file system (by default);
- non-optimal access to the database;
- long cycles (often with database accesses).
Look for such places in your script, and then, as Yan-s advised : take measurements, cut out a piece, repeat measurements.
1) Install php extension xdebug
Enable this extension, as well as profiling in xdebug settings
xdebug.profiler_enable = 1
Pay attention to these 2 options (where to put profile files):
xdebug.profiler_output_dir
xdebug.profiler_output_name
2) Download profiling files to your development machine, further you feed to such utility as QCacheGrind. It will quickly analyze them and give you which methods, how many times they were called, and how much CPU time it took to process. The results can be sorted.
3) It is better to disable xdebug in production.
I offer a super elemental option.
place at the beginning of the script $start = microtime(true);
and scatter over the codeprintf(microtime(true) - $start);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question