Created
March 18, 2014 17:22
-
-
Save Szpadel/9624801 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Benchmark | |
{ | |
private $tests = []; | |
private $command = ""; | |
private $multiple = 10; | |
private $results = []; | |
/** | |
* @param $name | |
* @param $params | |
* @return $this | |
*/ | |
public function addTest($name, $params) | |
{ | |
$this->tests[$name] = $params; | |
return $this; | |
} | |
/** | |
* @param string $command | |
* @return $this | |
*/ | |
public function setCommand($command) | |
{ | |
$this->command = $command; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getCommand() | |
{ | |
return $this->command; | |
} | |
/** | |
* @param int $multiple | |
* @return $this | |
*/ | |
public function setMultiple($multiple) | |
{ | |
$this->multiple = $multiple; | |
return $this; | |
} | |
/** | |
* @return int | |
*/ | |
public function getMultiple() | |
{ | |
return $this->multiple; | |
} | |
public function removeEdges($res) | |
{ | |
$rem = floor($this->multiple * 0.25); | |
sort($res); | |
$c = count($res); | |
$res = array_slice($res, 0, $c - $rem); | |
$res = array_slice($res, $rem); | |
return $res; | |
} | |
public function runTests() | |
{ | |
$total = $this->multiple * count($this->tests); | |
$numTest = 0; | |
for ($i = 0; $i < $this->multiple; $i++) { | |
foreach ($this->tests as $name => $test) { | |
if (isset($this->results[$name]) && count($this->results[$name]) >= $this->multiple) { | |
$numTest++; | |
continue; | |
} | |
$numTest++; | |
$testCommand = vsprintf($this->command, $test); | |
$start = time() + microtime(); | |
echo "[$numTest/$total] running: $testCommand... "; | |
shell_exec($testCommand); | |
$stop = time() + microtime(); | |
$time = ($stop - $start) * 1000; | |
echo round($time, 2) . "ms\n"; | |
$this->results[$name][] = $time; | |
} | |
} | |
} | |
private function progress($p) | |
{ | |
$ret = ""; | |
$n = $p * 70; | |
for ($i = 0; $i < $n; $i++) { | |
$ret .= '#'; | |
} | |
return $ret; | |
} | |
public function printResults() | |
{ | |
$averageResults = []; | |
$precisionResults = []; | |
foreach ($this->results as $name => $res) { | |
$res = $this->removeEdges($res); | |
$averageResults[$name] = array_sum($res) / count($res); | |
$precisionResults[$name] = max($res) - min($res); | |
} | |
$max = max($averageResults); | |
foreach ($averageResults as $name => $time) { | |
printf("%-10s: %-70s %d ms +/- %d ms\n", $name, $this->progress($time / $max), $time, $precisionResults[$name]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment