Created
February 6, 2011 09:08
-
-
Save yuya-takeyama/813246 to your computer and use it in GitHub Desktop.
Joshimane: A Tiny PHP Benchmarking Framework... But it's just a prototype yet.
This file contains 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 | |
require_once './Joshimane.php'; | |
// Create a pretty Joshimane!!! | |
$joshimane = new Joshimane; | |
// Creates Job A. | |
$jobA = $joshimane->createJob(function () { | |
$range = range(1, 100); | |
for ($i = 0; $i < count($range); $i++); | |
}); | |
// Creates Job B too. | |
$jobB = $joshimane->createJob(function () { | |
$range = range(1, 100); | |
$count = count($range); // The difference. Gets a count at first. | |
for ($i = 0; $i < $count; $i++); | |
}); | |
// Runs both of jobs 10,000 times. | |
$resultA = $jobA->run(10000); | |
$resultB = $jobB->run(10000); | |
// Dispalys both of job's result. | |
$format = '%0.8f'; | |
echo "Job A: ", $resultA->getTotalTime($format), PHP_EOL; | |
echo "Job B: ", $resultB->getTotalTime($format), PHP_EOL; |
This file contains 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 | |
/** | |
* Joshimane_Job | |
* | |
* A class contains a job under the benchmarking test as a callback. | |
* | |
* @author Yuya Takeyama | |
*/ | |
class Joshimane_Job | |
{ | |
/** | |
* A job under the benchmarking test. | |
* | |
* @var Closure | |
*/ | |
protected $_callback; | |
/** | |
* Constructor. | |
* | |
* @param Closure $callback A job as an anonymous function. | |
*/ | |
public function __construct($callback) | |
{ | |
$this->_callback = $callback; | |
} | |
/** | |
* Runs job N times. | |
* | |
* @param int $times Times the job runs. | |
* @return Joshimane_Result | |
*/ | |
public function run($times = 100) | |
{ | |
$begin = microtime(true); | |
for ($i = 0; $i < $times; $i++) { | |
$this->_callback->__invoke(); | |
} | |
$end = microtime(true); | |
return new Joshimane_Result(array( | |
'begin' => $begin, | |
'end' => $end | |
)); | |
} | |
} |
This file contains 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 | |
/** | |
* Joshimane: A Tiny PHP Benchmarking Framework. | |
* | |
* Joshimane Drives Our Development. | |
* | |
* @author Yuya Takeyama | |
*/ | |
require_once __DIR__ . '/Joshimane/Job.php'; | |
require_once __DIR__ . '/Joshimane/Result.php'; | |
class Joshimane | |
{ | |
public function createJob($callback) | |
{ | |
return new Joshimane_Job($callback); | |
} | |
} |
This file contains 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 | |
/** | |
* Joshimane_Result | |
* | |
* A class contains benchmarking result. | |
* | |
* @author Yuya Takeyama | |
*/ | |
class Joshimane_Result | |
{ | |
/** | |
* Constructor. | |
* | |
* @param array $params Parameters of the job result. | |
* + begin float When the job had begun. | |
* + end float When the job had ended. | |
*/ | |
public function __construct($params) | |
{ | |
$this->_begin = $params['begin']; | |
$this->_end = $params['end']; | |
} | |
/** | |
* Gets the total time elapsed. | |
* | |
* @param string $format Format string passed to sprintf(). | |
* @return float|string | |
*/ | |
public function getTotalTime($format = NULL) | |
{ | |
if (is_null($format)) { | |
$result = $this->_end - $this->_begin; | |
} else { | |
$result = sprintf($format, $this->_end - $this->_begin); | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment