Home

Awesome

Sampling profiler for PHP

This project implements a basic sampling profiler for PHP.

Most (all?) other profilers for PHP work by hooking into function execution (zend_execute_ex to be more precise). This means that

A sampling profiler on the other hand provides line-level resolution and, depending on the chosen sampling interval, either doesn't affect performance at all or slows down everything symmetrically.

Installation

In the directory of the extension run:

phpize
./configure
make
sudo make install

Usage

API

void sample_prof_start($interval_usec = 1, $num_entries_alloc = 1<<20)

Starts the profiler. Parameters:

bool sample_prof_end()

Ends collection of samples. Returns true is the profiler was running at the time of the call or false otherwise. This information is useful if you want to find out whether the number of samples exceeded $num_entries_alloc and the profiler was automatically disabled because of that.

array sample_prof_get_data()

Retrieves the profiling data as an array with format [$file => [$line => $hits]], which specifies how often a certain line in a certain file was hit during sampling. The hits are not cumulative, i.e. hits that happen in a function call will not be added to the line of the function call.

Example usage

<?php

sample_prof_start(50);          // start profiler with 50 usec interval
require $script;                // run script here
sample_prof_end();              // disable profiler
$data = sample_prof_get_data(); // retrieve profiling data

foreach ($data as $file => $lines) {
    echo "In file $file:\n";
    foreach ($lines as $line => $hits) {
        echo "Line $line hit $hits times.\n";
    }
}

Script

A sample_prof.php script is provided for convenience. It is invoked as follows:

php sample_prof.php [--html | --callgrind] [--interval=usec] script.php [...args] > out

sample_prof.php will run the script script.php with the passed ...args and profile the execution. The output format is specified using --html (default) or --callgrind. The former will output HTML which can be viewed in a browser of your choice, while the latter produces a callgrind output which you can view in a tool like KCacheGrind. You can adjust the sampling interval using the --interval option.

It is recommended to let script.php run multiple seconds to get good statistical coverage. The longer the script runs the better the results will be.

Sample profiling output: The numbers in the second column specify how often a line has been hit. Lines that were hit more often are displayed in red.

Note that the hits are not cumulative, i.e. if you perform a function the hits within the call will not be added to the hits on the function call line.

Todo