Performance8 min read

Where you start the clock decides which function looks slow

One workload, two profiling mechanisms. Instrumentation cost 13% and missed the function that was 55% of the request; sampling cost nothing measurable and found it.

Alden PikeBackend engineer and technical writer

The same workload, measured three ways. The truth, taken with two clock reads per batch: 30.1% of the time in one function, 69.8% in the other. An instrumenting profile of the same run: 18.3% and 81.7%. A sampling profile: neither of those functions is the largest thing in the request.

All three are measuring the same 349 milliseconds.

How these numbers were taken

PHP 8.5.10, NTS, arm64, Homebrew build, on an Apple M4 Pro laptop with 24 GB and 12 logical cores, running macOS and not quiesced. OPcache and the JIT are off.

Xdebug and SPX are not installed on this build. Installing either would have changed the rig the rest of this archive is measured on, so the two mechanisms were reproduced directly instead: a hand-instrumented copy of the workload, and a sampling profiler built in userland out of setitimer, SIGPROF and debug_backtrace(). Published figures for the real extensions appear at the end, quoted and attributed rather than measured here.

Timings are the median of 10 interleaved runs with three warmup rounds discarded, taken with hrtime() inside the process, with the range given. Sample counts are single runs and are reported individually, because their variation is part of the argument. Read timings relative to each other, for the reasons the method post sets out.

The workload, and what is actually true about it

Two functions with opposite shapes — one called constantly and cheap, one called rarely and expensive:

<?php

declare(strict_types=1);

function normalizeRow(array $row): array
{
    $row['sku'] = strtoupper(trim((string) $row['sku']));
    $row['qty'] = (int) $row['qty'];

    return $row;
}

function compressBatch(array $rows): string
{
    $blob = '';
    foreach ($rows as $row) { $blob .= $row['sku'] . '|' . $row['qty'] . "\n"; }

    return gzcompress($blob, 9);
}

Two hundred batches of five thousand rows: 1,000,000 calls to normalizeRow and 200 to compressBatch. Timing the two phases with one clock read each per batch — 400 reads in total, which is not enough instrumentation to disturb anything — gives the ground truth this post measures the profilers against:

total 348.948 ms | normalize 105.207 ms (30.1%) | compress 243.720 ms (69.8%)

Instrumentation moves the thing it measures

The instrumented version records entry and exit inside each function and accumulates per-function totals — the same bookkeeping an instrumenting profiler does, done by hand:

function normalizeRow(array $row): array
{
    global $PROF;
    $enter = hrtime(true);

    $row['sku'] = strtoupper(trim((string) $row['sku']));
    $row['qty'] = (int) $row['qty'];

    $PROF['normalizeRow'][0] += hrtime(true) - $enter;
    $PROF['normalizeRow'][1]++;

    return $row;
}
instrumented total 385.962 ms
  normalizeRow      53.689 ms    18.3% of attributed  1000000 calls
  compressBatch    240.057 ms    81.7% of attributed      200 calls

Two things went wrong at once, and they are the two failures the mechanism has.

The first is the total: 388.590 ms against 343.695 unprofiled, both medians of ten runs with ranges of 386.544–391.086 and 341.185–346.498. That is 13.1%, bought by instrumenting exactly two functions. An extension-level profiler hooks every function call in the request.

The second is the split. The instrumented run gives normalizeRow 18.3% where the phase truth is 30.1%. Nothing is broken; the clock simply starts after the function has been entered. Binding the argument, building the frame and returning the value are all outside the measured window, and for a function called a million times those are most of what it costs. The profiler is answering “how long does the body take”, and the question was “how much of the request is this”.

That is the general shape of the trap, and it is not specific to my hand-rolled version: every instrumenting profiler has to choose where its hook fires, and the choice decides whether call overhead is attributed to the callee, to the caller, or to nothing.

A sampling profiler in twenty lines

The other mechanism does not touch the program. It arms a timer, and when the signal arrives it asks what the program is doing:

<?php

declare(strict_types=1);

$samples = [];
$ffi = FFI::cdef('
    struct timeval { long tv_sec; int tv_usec; };
    struct itimerval { struct timeval it_interval; struct timeval it_value; };
    int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
', 'libc.dylib');

$it = $ffi->new('struct itimerval');
$it->it_interval->tv_sec = 0; $it->it_interval->tv_usec = 1000;
$it->it_value->tv_sec = 0;    $it->it_value->tv_usec = 1000;

pcntl_async_signals(true);
pcntl_signal(SIGPROF, static function () use (&$samples): void {
    foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
        if (isset($frame['function']) && !str_starts_with($frame['function'], '{closure')) {
            $samples[$frame['function']] = ($samples[$frame['function']] ?? 0) + 1;
            return;
        }
    }
});

$ffi->setitimer(2, FFI::addr($it), null);   // 2 is ITIMER_PROF

ITIMER_PROF counts CPU time rather than wall time, pcntl_async_signals(true) lets the handler run between VM instructions, and debug_backtrace() names the function on top of the stack at that moment. Nothing in the workload changes.

sampled total 343.694 ms, 159 samples
  gzcompress          88   55.3%
  compressBatch       28   17.6%
  normalizeRow        17   10.7%
  strtoupper          14    8.8%
  runWorkload         12    7.5%

The overhead is 345.059 ms against 343.695, ranges 342.067–350.508 and 341.185–346.498. The two overlap, so on this workload the sampling profiler’s cost is not separable from run-to-run variation.

What the sample list contains that the other one cannot

The largest thing in the request is gzcompress, at 55.3%. It is an internal function. No instrumenting profile of the two userland functions would ever have listed it, because the instrumentation was placed by someone who already had a theory about where the time was.

strtoupper at 8.8% is the same story from the other side: a third of normalizeRow’s cost is one internal call inside it, which the function-level instrumentation folded into a single number. Internal functions are where a surprising share of a PHP request lives, and they are reached through the same handler dispatch every other opcode goes through — which is to say they are ordinary work, not a black box, and a profile that cannot name them is hiding the majority of this one.

The two accounts do agree once you add the frames up. Compression is 55.3 + 17.6 = 72.9% against a truth of 69.8%; the normalizing side is 10.7 + 8.8 = 19.5%, plus the 7.5% attributed to the loop in runWorkload that builds the row arrays, against a truth of 30.1%. Sampling reconstructed the split to within about three points without being told what to look at.

The same samples, ranked two ways

One more decision changes the answer, and it is made by the tool rather than by the mechanism. The listing above counted only the top frame of each stack. Count every frame instead — a function is credited if it appears anywhere in the stack — and the same profiler produces a different ranking of the same run:

Function Top frame only Anywhere on the stack
runWorkload 7.5% 100.0%
compressBatch 17.6% 72.6%
gzcompress 55.3% 54.0%
normalizeRow 10.7% 18.6%
strtoupper 8.8% 7.1%

The two columns answer different questions. The first is which code was executing, and it puts an internal compression routine at the top. The second is which of your functions the time belongs to, and it puts the outermost one at 100% — true, and useless, which is why a flame graph is drawn as a shape rather than read as a list.

The useful row is compressBatch: 17.6% of the executing time and 72.6% of the owning time, and the gap between those two figures is gzcompress running inside it. A tool that shows one column and not the other will produce one of those two beliefs and not the other.

And what sampling cannot do

Run the identical workload three times and the profile is not identical:

Run Samples gzcompress
1 159 55.3%
2 141 49.6%
3 118 57.6%

Eight points of movement on the top entry, and a sample count that varies by a third. Any ranking difference smaller than that is noise, and a profile with a few hundred samples cannot distinguish two functions that are close.

The timer is also not the timer you asked for. The interval above is one millisecond, and 159 samples over 343 ms of CPU is one every 2.2 ms. Signal delivery is coalesced by the operating system, and this is a macOS figure; the point that transfers is that the requested rate is an upper bound rather than a guarantee.

Then there is the number sampling structurally cannot produce. The instrumented run reported 1,000,000 and 200 calls. No amount of sampling gives a call count, which is why “this function is called 40,000 times per request” is an instrumentation finding and always will be.

The published overhead figures, and what is missing from them

Xdebug’s own profiler documentation does not quantify its overhead anywhere. The page warns about disk space and says nothing about time, which is worth knowing given how often “Xdebug doubles your request time” is repeated as though it came from there.

The measured figures in circulation come from Tideways’ comparison of profiling overhead, published 2015-10-27: on PHP 5.6 at full sampling, Xdebug 39.42%, XHProf 42.22%, Tideways Timeline 22.93%; on PHP 7, Tideways XHProf 59.42%. The article’s own framing is the part that has aged best — the same absolute overhead is a larger percentage of a faster request, and “WordPress is almost 3 times faster on PHP 7 than on 5.6”.

Eleven years and several major versions later, that argument only gets stronger, and it is why 13.1% for instrumenting two functions on 8.5 is not a comforting number.

Which tool for which question

Sample when the question is where the request’s time goes. It costs almost nothing, it attributes to whatever is genuinely on the stack including internal functions, and its blind spot — statistical noise — is one you can see and quantify by taking more samples.

Instrument when the question is how often. Call counts, and the discovery that a function you thought ran once runs four thousand times, are what the mechanism is for, and the request whose time was not in the code at all is the case where the count is the finding.

Never quote an absolute timing from an instrumented run. The run took 13% longer than the one you are trying to describe, the distortion is not spread evenly, and it is concentrated in exactly the functions with the highest call counts — which are the ones the profile is about to blame.

And check where the clock starts before acting on a ranking. A function whose cost is in being called cannot be made faster by making its body faster, and the two profiles above disagree about that function by twelve points.

Frequently asked

Which profiler should I use?

Sampling for where a request spends its time, because it costs almost nothing and attributes to whatever is actually on the stack, including internal functions. Instrumentation for how many times something was called - it reported 1,000,000 and 200 here - which is the one question sampling cannot answer.

How much does profiling slow a request down?

On this workload, sampling at 1 ms was inside the noise - 345.059 ms against 343.695 unprofiled, with overlapping ranges. Instrumenting two functions cost 13.1%. Extension-level instrumenting profilers hook every function, and the published figures for those are much larger.

Why does my profiler blame a function that I cannot speed up?

Often because the cost is in calling it rather than in its body, and where a profiler starts its clock decides which of those it reports. A function called a million times can be most of a request without any single call being slow.

Can I build a sampling profiler without an extension?

Yes, in about twenty lines. setitimer through FFI arms ITIMER_PROF, pcntl_signal handles SIGPROF, and debug_backtrace names the function on top of the stack when the signal arrives. The post has the code.

Written by

Alden Pike

Alden Pike writes about PHP internals, performance, architecture and production behavior — the layer beneath the frameworks. Measurements over assumptions, trade-offs over universal rules.

More about the author

Arrow keys to move, Enter to open.