Tally
Benchmarks PHP code by counting instructions, CPU cycles, and memory instead of measuring time.
final class SortBench
{
#[Expected(instructions: '59.00K', cycles: '94.43K', memory: '10.53KiB')]
public function benchSortHundred(): void
{
$numbers = range(100, 1, -1);
sort($numbers);
}
}
Why
Timing benchmarks don't work in CI. Runners differ from each other in CPU model, clock speed, and cache sizes, and share the hardware with other jobs, so the same code takes a different time on every run. The noise is bigger than most regressions worth catching.
Tally runs each benchmark once under CodSpeed's CPU simulation, which is built on Valgrind. The simulation counts executed instructions and estimates cycles from simulated cache misses. With the same PHP build, these numbers barely move between runs: in two CI runs of Tally's own benchmarks, instructions differed by at most 0.4 % and cycles by at most 0.8 %, and memory not at all. So the numbers are committed next to the benchmark and checked in every pull request, like the expected output of a test.
Contents
Installation
composer require --dev phpstan/tally
Tally needs PHP 8.2 or later with the POSIX extension, on Linux on x86_64 or aarch64. Nothing else to set up: each release includes, in codspeed/ for both platforms,
- a build of CodSpeed's Valgrind, with its license and where its source is. Tally starts it with the simulated caches of
codspeed run --mode=simulation, so it counts the same instructions as CodSpeed, but needs neither the codspeed CLI nor a CodSpeed account, and uploads nothing.
- builds of Tally's
tally_hooks extension (ext/tally_hooks) for PHP 8.2 to 8.6, thread safe or not. It tells Callgrind when to count, with the client requests of Valgrind's callgrind.h. tally run loads the build for your PHP into the process that counts a case with -d extension=, so it needs no php.ini change and nothing else in your other processes.
A debug build of PHP, or another PHP whose extension API differs from the official one, needs its own build of tally_hooks. Build it with phpize and load it in php.ini, then tally run uses it instead:
build/build-tally-hooks.sh /usr/local/lib/php/tally_hooks.so # in vendor/phpstan/tally
echo 'extension=/usr/local/lib/php/tally_hooks.so' > /usr/local/etc/php/conf.d/tally_hooks.ini
Releases are tagged on the release/1.x branch, which CI builds from 1.x with the builds committed. A clone of 1.x, or dev-1.x from Composer, has no builds: run make codspeed, which builds Valgrind and tally_hooks for your PHP and needs gcc, make, autotools, and phpize.
The counts depend on the PHP build and the system libraries. Numbers measured on a laptop differ from those of a CI runner, so take the expected numbers from CI.
Writing benchmarks
A benchmark is a public, non-static method whose name starts with bench, in a file whose name ends with Bench.php, in the class named like the file, such as StrBench in StrBench.php. Other classes in the file, such as helpers, are not benchmarks. Tally creates the class with the constructor arguments described below. A bench* method that is static or not public is an error, so is a path without any benchmarks.
use Tally\Attribute\DataProvider;
use Tally\Attribute\ExpectedFor;
final class SortBench
{
/**
* @return iterable<string, array{list<int>}>
*/
public static function shuffled(): iterable
{
foreach ([10, 1000] as $size) {
$numbers = range(1, $size);
mt_srand($size);
shuffle($numbers);
yield 'size-' . $size => [$numbers];
}
}
/**
* @param list<int> $numbers
*/
#[DataProvider('shuffled')]
#[ExpectedFor('size-10', instructions: '4.563K', cycles: '20.96K', memory: '1.023KiB')]
#[ExpectedFor('size-1000', instructions: '955.3K', cycles: '1.119M', memory: '59.88KiB')]
public function benchSort(array $numbers): void
{
sort($numbers);
}
}
Each data set of a data provider is a case with the ID Class::method@name. A case without a data provider has the ID Class::method. The data provider runs before counting, so building the input is not counted. Keep the input the same on every run: a random order must come from a fixed seed.
Numbers are written with units: K, M, B, and T for counts, B, KiB, MiB, and GiB for memory, with four significant digits.