2017 © Pedro Peláez
 

library php-benchmark

PHP library that allows you benchmark and compare the performance of functions

image

mookofe/php-benchmark

PHP library that allows you benchmark and compare the performance of functions

  • Wednesday, September 7, 2016
  • by mookofe
  • Repository
  • 1 Watchers
  • 3 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 40 % Grown

The README.md

mookofe/php-benchmark

PHP library that allows you benchmark and compare the performance of functions., (*1)

Latest Stable Version Latest Unstable Version License, (*2)

Features

  • Optimize for PHP7
  • The library accepts an arbitrary number of user defined functions to test against each other
  • Accept functions under test as callable type.
  • Accept functions under test with a canonical name
  • The library accept an arbitrary number of argument sets to be passed to each function under test.
  • Functions can be tested N number of times
  • Includes Summary report
  • Summary report can be streamed can be sent to an i/o stream in human readable format
  • Summary report can be order by (Min, Max, Avg and Median) execution time (Ascending and Descending)
  • Report can be filtered by function name and parameters set

Version

0.0.1, (*3)

Installation

To get started, use Composer to add the package to your project's dependencies:, (*4)

$ composer require mookofe/php-benchmark

Basic Usage:

function bubbleSort(array $array): void
{
    if (!$length = count($array)) {
        return $array;
    }      

    for ($outer = 0; $outer < $length; $outer++) {
        for ($inner = 0; $inner < $length; $inner++) {
            if ($array[$outer] < $array[$inner]) {
                $tmp = $array[$outer];
                $array[$outer] = $array[$inner];
                $array[$inner] = $tmp;
            }
        }
    }
}

function quickSort(array $array): void
{
    if (!$length = count($array)) {
        return $array;
    }

    $k = $array[0];
    $x = $y = array();

    for ($i=1;$i<$length;$i++) {
        if ($array[$i] <= $k) {
            $x[] = $array[$i];
        } else {
           $y[] = $array[$i];
        }
    }

    return array_merge(quickSort($x),array($k),quickSort($y));
}

Given these two functions, let's benchmark them, (*5)

use Mookofe\Benchmark\Method;
use Mookofe\Benchmark\Sorters\Min;
use Mookofe\Benchmark\Orchestrator;
use Mookofe\Benchmark\Sorters\Order\Asc;
use Mookofe\Benchmark\Repositories\IOReporter;

...

//Define orchestrator
$orchestrator = Orchestrator();

/** Add paraters to test */
$orchestrator->addParameters([5, 4, 3, 2, 1]);
$orchestrator->addParameters([100, 5, 300]);
$orchestrator->addParameters([20, 10, 9, 25]);

/** Add methods */
$orchestrator->addMethod(new Method('bubbleSort'));
$orchestrator->addMethod(new Method('quickSort'));

/** Run tests 10 times */
$results = $orchestrator->run(10);

/** Run reporter */
$reporter = new IOReporter($results);
$reporter->setPath('results.txt');

//Sorter
$asc = new Asc();
$sorter = Min($asc);

/** Generate report */
$reporter->generate($sorter);

Results:, (*6)


******************************************************************************** Benchmark Report ******************************************************************************** Running times: 10 Number of functions: 2 Number of parameters set: 3 SUMMARY: Function Parameters Min Max Avg Median -------------------------------------------------------------------------------- bubbleSort ([100, 5, 300]) 3.0994 5.0068 3.0994 3.0994 bubbleSort ([20, 10, 9, 25]) 5.0068 28.8486 5.0068 5.0068 bubbleSort ([5, 4, 3, 2, 1]) 6.9141 22.8882 6.9141 6.9141 quickSort ([100, 5, 300]) 10.0136 11.9209 10.0136 10.0136 quickSort ([5, 4, 3, 2, 1]) 10.9673 377.8934 10.9673 10.9673 quickSort ([20, 10, 9, 25]) 13.113 19.0735 13.113 13.113 * Times in microsecond (µs)

Sorting:

Sort by minimum descending

Sort the summary report by the min field, (*7)

use Mookofe\Benchmark\Sorters\Min;
use Mookofe\Benchmark\Sorters\Order\Desc;

...

/** Sorter */
$desc = new Desc();
$sorter = new Min($desc);

/** Generate report */
$reporter->generate($sorter);

Sort by maximum

Sort the summary report by the max field, (*8)

use Mookofe\Benchmark\Sorters\Max;
use Mookofe\Benchmark\Sorters\Order\Asc;

...

/** Sorter */
$asc = new Asc();
$sorter = new Max($asc);

Sort by average

Sort the summary report by the avg field, (*9)

use Mookofe\Benchmark\Sorters\Max;
use Mookofe\Benchmark\Sorters\Order\Avg;

...

$asc = Asc();
$sorter = new Avg($asc);

Sort by median

Sort the summary report by the median field, (*10)

use Mookofe\Benchmark\Sorters\Max;
use Mookofe\Benchmark\Sorters\Order\Median;

...

$asc = new Asc();
$sorter = new Median($asc);

Filtering

Filter by method name

use Mookofe\Benchmark\Sorters\Median;
use Mookofe\Benchmark\Sorters\Order\Asc;
use Mookofe\Benchmark\Filters\FunctionName;

...

$asc = new Asc();
$sorter = new Median($asc);

/** Filters */
$functionNames = [
    'bubbleSort'
];

$functionNameFilter = new FunctionName($functionNames);
$reporter->addFilter($functionNameFilter);

/** Generate report */
$reporter->generate($sorter);

Filter by parameters set

use Mookofe\Benchmark\Sorters\Median;
use Mookofe\Benchmark\Sorters\Order\Asc;
use Mookofe\Benchmark\Filters\Parameter;

...

$asc = new Asc();
$sorter = Median($asc);

/** Filters */
$parametersFilter = new Parameter();
$parametersFilter->addSet([5, 4, 3, 2, 1]);
$parametersFilter->addSet([100, 5, 300]);
$reporter->addFilter($parametersFilter);

/** Generate report */
$reporter->generate($sorter);

TODO

  • Accept arguments by canonical name
  • Create WebReporterRepository to allow benchmark on controllers and other places
  • Return exception when argument and signature does not match
  • Finish package tests
  • try/catch validations
  • Enable Continuous Integration

License

This package is open-sourced software licensed under the MIT license, (*11)

The Versions

07/09 2016

dev-master

9999999-dev

PHP library that allows you benchmark and compare the performance of functions

  Sources   Download

MIT

The Requires

  • php >=5.4.7

 

The Development Requires

by Victor Cruz

functions package benchmark performance victor cruz mookofe