public function testAdjustmentOfFastTests() { $timer = new Timer(100); $method = new Method(self::$fastClosure, ''); $result = $timer->time($method); $this->assertGreaterThan($timer->getMinDuration(), $result->getDuration()); $this->assertLessThan($timer->getMinDuration() * 1.2, $result->getDuration(), 'Minimum duration must not be exceeded by 20% for fast tests.'); }
public function testReport() { Timer::$defaultMinDuration = 100; $report = new Report('Test report', 'Example benchmark.'); $unit = new Unit('String concatenation'); $unit->addClosure(function ($n) { while ($n--) { $x = "foobar {$n}"; } }, '$x = "foobar $n"'); $unit->addClosure(function ($n) { while ($n--) { $x = 'foobar ' . $n; } }, '$x = \'foobar \' . $n'); $report->unitList->add($unit); $unit2 = new Unit('String concatenation with varying length'); foreach ($unit->getMethods() as $method) { $unit2->addMethod($method); } $unit2->addParam(new Parameter('x', '$p = \'x\'')); $unit2->addParam(new Parameter('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', '$p = \'xxxxx..\'')); $report->unitList->add($unit2); $report->run(); }
public function testUnit() { Timer::$defaultMinDuration = 100; $unit = new Unit('test'); $unit->addMethod(new Method(function ($n, $cost) { if ($cost < 10) { while ($n--) { $x = $n * 2; } } else { while ($n--) { $x = $n * 2; $x = number_format($x); } } }, 'calc', 'calculate stuff')); $unit->addClosure(function ($n, $cost) { while ($n--) { $x = password_hash('xx', PASSWORD_DEFAULT, array('cost' => $cost)); } }, 'hash', 'password_hash'); $unit->addParam(new Parameter(5, 'easy')); $unit->addParam(new Parameter(10, 'hard')); $results = $unit->run(); }
<?php use nochso\Benchmark; use nochso\Benchmark\Parameter; use nochso\Benchmark\Report; use nochso\Benchmark\Timer; use nochso\Benchmark\Unit; use nochso\Omni; use nochso\Omni\Exec; use nochso\Omni\Folder; use nochso\Omni\Path; require 'vendor/autoload.php'; $repos = ['aura/sql' => 'https://github.com/auraphp/Aura.Sql.git', 'doctrine/dbal' => 'https://github.com/doctrine/dbal.git', 'fzaninotto/faker' => 'https://github.com/fzaninotto/Faker.git', 'paris' => 'https://github.com/j4mie/paris.git', 'phpunit' => 'https://github.com/sebastianbergmann/phpunit.git', 'plates' => 'https://github.com/thephpleague/plates.git', 'slim' => 'https://github.com/slimphp/Slim.git', 'symfony/yaml' => 'https://github.com/symfony/yaml.git', 'twig' => 'https://github.com/twigphp/Twig.git']; $tmpDir = Path::combine(sys_get_temp_dir(), 'phormat_benchmark'); Folder::ensure($tmpDir); Timer::$defaultMinDuration = 1000; $versions = ['phormat' => ['php', 'phormat', '-h'], 'php-cs-fixer' => ['php-cs-fixer', '--version'], 'phpfmt' => ['fmt.phar', '--version']]; foreach ($versions as $name => &$version) { $line = Exec::create()->run(...$version)->getOutput()[0]; if (preg_match('/([0-9][0-9.-]+[0-9a-z-]+)/i', $line, $m)) { $version = $m[1]; } $version = $name . ' ' . $version; } $report = new Report('Speed comparison of PHP source formatters', '', ['output_dir' => 'benchmark']); $desc = 'Various open source projects are formatted with common settings. Each repository is checked out `--hard` once before the benchmark is run. `.php_cs` and `.php_cs_cache` files are removed to make php-cs-fixer comparable with other formatters.