php profiler
Profiler can run in the following modes
- sampling (stops program every n microseconds and analyzes current stack)
- func (hooks into every function call)
- opcode (hooks into every opcode)
mode can be set through prof.mode ini setting or PROF_MODE env var
// a.php
function foo()
{
for ($i = 0; $i < 10_000_000; $i++) {
}
}
foo();php -d prof.mode=sampling a.phpoutput
total time: 0.076188s
total samples: 70
╭──────────┬───────────╮
│ function │ hits │
├──────────┼───────────┤
│ foo │ 70 (100%) │
╰──────────┴───────────╯
- console
- callgrind (will generate
callgrind.outfile) - pprof (will generate
cpu.pproffile)
can be set throgh prof.output_mode ini setting or PROF_OUTPUT_MODE env var
- sampling interval (
prof.sampling_intervalini setting orPROF_SAMPLING_INTERVALenv var, default value is 1000 microseconds) - sampling limit (
prof.sampling_limitini setting orPROF_SAMPLING_LIMITenv var, default value is 20, only in console output mode), show only top n functions - sampling threshold (
prof.sampling_thresholdini setting orPROF_SAMPLING_THRESHOLDenv var, default value is 1), show only functions with > n calls
// a.php
function foo()
{
usleep(1000);
}
foo();php -d prof.mode=func a.phpoutput
total time: 0.002287s
╭──────────┬───────────┬───────────┬────────┬───────╮
│ function │ wall │ cpu │ memory │ calls │
├──────────┼───────────┼───────────┼────────┼───────┤
│ foo │ 0.001129s │ 0.000043s │ 448 b │ 1 │
╰──────────┴───────────┴───────────┴────────┴───────╯
- func limit (
prof.func_limitini setting orPROF_FUNC_LIMITenv var, default value is 50), show only top n functions by wall time - func threshold (
prof.func_thresholdini setting orPROF_FUNC_THRESHOLDenv var, default value is 0.000001 seconds), show only functions with wall time > n
// a.php
function foo()
{
usleep(1000);
}
foo();php -d prof.mode=opcode a.phpoutput
total time: 0.002542s
╭──────────────┬───────────╮
│ opcode │ time │
├──────────────┼───────────┤
│ /app/t.php:4 │ 0.001115s │
│ /app/t.php:5 │ 0.000009s │
│ /app/t.php:7 │ 0.000003s │
╰──────────────┴───────────╯
- opcode threshold (
prof.opcode_thresholdini setting orPROF_OPCODE_THRESHOLDenv var, default value is 0), show only opcodes with time > n
- for now profiler works only in cli mode
- zlib
phpize
./configure
make
make installmake test