A
A
Anton none2018-05-05 09:38:20
PHP
Anton none, 2018-05-05 09:38:20

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

5 answer(s)
Y
Yan-s, 2018-05-05
@Yan-s

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.

C
Codebaker, 2018-05-05
@Codebaker

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.

O
oxemelot, 2018-05-05
@oxemelot

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.

A
asd111, 2018-05-05
@asd111

PHP profiling with XHprof

A
Andrey Fedorov, 2018-05-06
@aliencash

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 question

Ask a Question

731 491 924 answers to any question