Writing Benchmarks

Benchmark classes have the following characteristics:

  • The class and filename must be the same.

  • Class methods that start with bench will be executed by the benchrunner and timed.

PHPBench does not require that the benchmark class be aware of PHPBench library - it does not need to extend a parent class or implement an interface.

The following is a simple benchmark class:

// HashBench.php
class HashBench
{
    public function benchMd5()
    {
        hash('md5', 'Hello World!');
    }

    public function benchSha1()
    {
        hash('sha1', 'Hello World!');
    }
}

And it can be executed as follows:

$ phpbench run examples/HashBench.php --progress=dots
Running benchmarks.

...

3 subjects, 30 iterations, 30000 revs, 0 rejects
⅀T: 30543μs μSD/r 0.05μs μRSD/r: 4.83%
min mean max: 0.78 1.02 1.47 (μs/r)

Note

The above command does not generate a report, add --report=default to view something useful.

PHPBench reads docblock annotations in the benchmark class. Annotations can be placed in the class docblock, or on individual methods docblocks.

Note

Instead of prefixing a method with bench you can use the @Subject annotation or specify a custom pattern.

Benchmark Configuration

See the Annotations and Attributes reference to see how you can configure your benchmarks.