Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Performance test
<?php
set_time_limit(0);
interface PerformanceTest
{
public function test($numberOfIteraftion);
}
class PerformanceTester
{
/**
* @var PerformanceTest
*/
protected $testObject;
protected $numberOfTests;
protected $numberOfIterations;
protected $times;
public function __construct($numberOfTests, $numberOfIteration)
{
$this->numberOfTests = $numberOfTests;
$this->numberOfIterations = $numberOfIteration;
}
public function setTest(PerformanceTest $test)
{
$this->testObject = $test;
return $this;
}
public function runTests()
{
$this->times = array();
for ($i = 0; $i < $this->numberOfTests; $i++) {
$start = microtime(true);
$this->testObject->test($this->numberOfIterations);
$end = microtime(true);
$this->times[] = $end - $start;
}
return $this;
}
public function getStats()
{
$times = $this->times;
sort($times);
return array(
'min' => min($times),
'max' => max($times),
'avg' => array_sum($times) / count($times),
'sorted' => $times,
);
}
public function printStats()
{
$stats = $this->getStats();
$ref = new ReflectionObject($this->testObject);
echo 'Test: ' . $ref->getName() . PHP_EOL;
echo 'Tests runs: ' . $this->numberOfTests . PHP_EOL;
echo 'Iterations: ' . $this->numberOfIterations . PHP_EOL;
echo 'Average: ' . number_format($stats['avg'], 10) . PHP_EOL;
echo 'Avg/iter: ' . number_format($stats['avg']/$this->numberOfIterations, 10) . PHP_EOL;
echo 'Min: ' . number_format($stats['min'], 10) . PHP_EOL;
echo 'Max: ' . number_format($stats['max'], 10) . PHP_EOL;
echo PHP_EOL;
}
}
class TestClassWithSetter
{
public $public;
protected $protected;
function __set($name, $value) {
$this->$name = $value;
}
}
class TestClassWithArrayConstructor
{
protected $protected = array();
function __construct(array $array) {
$this->protected = $array;
}
}
class TestClassEmpty
{
}
class PublicAssignmenetTest implements PerformanceTest
{
public function test($numberOfIterations)
{
for ($i = 0; $i < $numberOfIterations; $i++) {
$test = new TestClassWithSetter();
$test->public = $i;
unset($test);
}
}
}
class MagicAssignmenetTest implements PerformanceTest
{
public function test($numberOfIterations)
{
for ($i = 0; $i < $numberOfIterations; $i++) {
$test = new TestClassWithSetter();
$test->protected = $i;
unset($test);
}
}
}
class EmptyObjectTest implements PerformanceTest
{
public function test($numberOfIterations)
{
for ($i = 0; $i < $numberOfIterations; $i++) {
$test = new TestClassEmpty();
$test->name = $i;
unset($test);
}
}
}
class ArrayConstructorTest implements PerformanceTest
{
public function test($numberOfIterations)
{
$array = array('name' => 1);
for ($i = 0; $i < $numberOfIterations; $i++) {
$test = new TestClassWithArrayConstructor($array);
unset($test);
}
}
}
$numberOfTests = 100;
$numberOfIterations = 100000;
$tester = new PerformanceTester($numberOfTests, $numberOfIterations);
$tester->setTest(new PublicAssignmenetTest())
->runTests()
->printStats();
$tester->setTest(new EmptyObjectTest())
->runTests()
->printStats();
$tester->setTest(new MagicAssignmenetTest())
->runTests()
->printStats();
$tester->setTest(new ArrayConstructorTest())
->runTests()
->printStats();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.