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(); }
while ($n--) { $phpCsFixer->run($parameter); } chdir(__DIR__); }, 'php-cs-fixer Symfony', "```bash\n\$ " . $phpCsFixer->getCommand() . "\n```"); $phpFmt = Exec::create('fmt.phar', '--dry-run', '--psr'); $unit->addClosure(function ($n, $parameter) use($phpFmt) { chdir($parameter); while ($n--) { $phpFmt->run($parameter); } chdir(__DIR__); }, 'phpfmt PSR-2', "```bash\n\$ " . $phpFmt->getCommand() . "\n```"); foreach ($repos as $repoName => $repo) { $repoDir = Path::combine($tmpDir, 'repo', $repoName); if (!is_dir($repoDir)) { $clone = Exec::create('git')->run('clone', '--depth=1', '--single-branch', $repo, $repoDir); } $reset = Exec::create('git')->run('--git-dir=' . $repoDir . '/.git', '--work-tree=' . $repoDir, 'reset', '--hard', 'HEAD'); $phpCsCache = Path::combine($repoDir, '.php_cs.cache'); if (is_file($phpCsCache)) { unlink($phpCsCache); } $phpCsConfig = Path::combine($repoDir, '.php_cs'); if (is_file($phpCsConfig)) { unlink($phpCsConfig); } $unit->addParam(new Parameter($repoDir, $repoName, "[{$repo}]({$repo})")); } $report->unitList->add($unit); $report->run();
$result = $map[$needle . '_']; } if ($result !== $needle) { throw new \Exception(''); } } }, '`isset()`'); $list = array(); $map = array(); // Fill with a sorted list for ($i = 0; $i < 1000; $i++) { $list[] = $i * 2; $map[$i * 2 . '_'] = $i * 2; } $params = array('list' => $list, 'map' => $map, 'needle' => $list[0]); $sortUnit->addParam(new Parameter($params, '1/1000')); $params['needle'] = $list[332]; $sortUnit->addParam(new Parameter($params, '333/1000')); $params['needle'] = $list[499]; $sortUnit->addParam(new Parameter($params, '500/1000')); $params['needle'] = $list[999]; $sortUnit->addParam(new Parameter($params, '1000/1000')); $list = array(); $map = array(); for ($i = 0; $i < 100000; $i++) { $list[] = $i * 2; $map[$i * 2 . '_'] = $i * 2; } $params = array('list' => $list, 'map' => $map, 'needle' => $list[0]); $sortUnit->addParam(new Parameter($params, '1/100k')); $params['needle'] = $list[33332];