<?php use Kicaj\Bench\Bench; require __DIR__ . '/../vendor/autoload.php'; $exampleArr = ['value1' => 1, 'value2' => 2]; $benchmark = function ($iterations) use($exampleArr) { for ($i = 0; $i < $iterations; ++$i) { if (isset($exampleArr['value1'])) { // set } else { // not set } } }; $summary = Bench::make(10000)->addBenchmark('isset', $benchmark)->run()->getSummary(); var_dump($summary); /* array(1) { ["isset"]=> array(5) { ["time"]=> string(8) "0.000369" ["memory"]=> int(696) ["to_fastest"]=> float(1) ["to_least_memory"]=> int(1) ["per_sec"]=> float(27100272) }
/** * @covers ::__construct * @covers ::make */ public function test___construct() { $bench = Bench::make(123); $this->assertInstanceOf('\\Kicaj\\Bench\\Bench', $bench); }
/** * Run benchmarks. * * @param Iterator $iterator The file iterator * @param int $iterations The number of benchmarking iterations * @param string $format The output format. One of the self::OUTPUT_* constants * * @throws \Exception * * @return string */ public function runBench($iterator, $iterations, $format) { $msg = ''; $csvHeader = ''; /** @var SplFileInfo $file */ foreach ($iterator as $file) { if ($file->getExtension() != 'php') { continue; } if (substr($file->getFilename(), -9) != 'Bench.php') { continue; } $bench = Bench::make($iterations); /* @noinspection PhpIncludeInspection */ $benchmarks = (require $file->getRealPath()); foreach ($benchmarks as $bName => $b) { $bench->addBenchmark($bName, $b); } $bench->run(); $benchSummary = $bench->getSummary(); $fileName = $file->getFilename(); switch ($format) { case self::OUTPUT_TXT: $printer = new Text($fileName, $benchSummary); $msg .= implode("\n", $printer->summaryToStr()); break; case self::OUTPUT_CSV: $printer = new Csv($fileName, $benchSummary); $arr = $printer->summaryToStr(); $csvHeader = array_shift($arr); $msg .= implode("\n", $arr); break; } } if ($format == self::OUTPUT_CSV) { $msg = $csvHeader . $msg; } return $msg; }