Skip to content

Commit

Permalink
Added Aggregator as described in #4
Browse files Browse the repository at this point in the history
  • Loading branch information
markuspoerschke committed Nov 1, 2014
1 parent 23956a2 commit 92f3bc9
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 18 deletions.
3 changes: 2 additions & 1 deletion example.php
Expand Up @@ -28,7 +28,8 @@ function createArray($arrSize)
};

// Create a new benchmark instance
$phpench = new mre\PHPench('Compare array_flip and array_unique');
$phpench = new mre\PHPench();
$phpench->setTitle('Compare array_flip and array_unique');

// Add your test to the instance
$phpench->addTest($benchFunction1, 'array_flip');
Expand Down
4 changes: 3 additions & 1 deletion example2.php
Expand Up @@ -39,7 +39,8 @@ public function execute() {
}

// Create a new benchmark instance
$phpench = new mre\PHPench('Compare array_flip and array_unique');
$phpench = new mre\PHPench(new \mre\PHPench\Aggregator\AverageAggregator);
$phpench->setTitle('Compare array_flip and array_unique');

// Add your test to the instance
$phpench->addTest(new TestArrayFlip, 'array_flip');
Expand All @@ -49,5 +50,6 @@ public function execute() {
// With the second parameter you can specify
// the start, end and step for each call
$phpench->setInput(range(1,pow(2,16), 1024));
$phpench->setRepetitions(4);
$phpench->run();
$phpench->save('test2.png', 1024, 768);
94 changes: 78 additions & 16 deletions src/mre/PHPench.php
Expand Up @@ -3,6 +3,8 @@
namespace mre;

use Gregwar\GnuPlot\GnuPlot;
use mre\PHPench\Aggregator\SimpleAggregator;
use mre\PHPench\AggregatorInterface;
use mre\PHPench\TestInterface;
use PHP_Timer;

Expand All @@ -21,20 +23,42 @@ class PHPench
private $tests = [];
private $titles = [];

/**
* The title of the benchmark
*
* @var string
*/
private $title = 'untitled';

/**
* Contains an array with the run numbers
*
* @var array
*/
private $input = [];

public function __construct($title = 'untitled')
/**
* @var AggregatorInterface
*/
private $aggregator;

/**
* The number of times the bench should be executed.
*
* This can increase the precise.
*
* @var int
*/
private $repetitions = 1;

public function __construct(AggregatorInterface $aggregator = null)
{
if ($aggregator === null) {
$aggregator = new SimpleAggregator();
}

$this->plot = new GnuPlot();
$this->plot->reset();
$this->plot->setGraphTitle($title);
$this->plot->setXLabel('run');
$this->plot->setYLabel('time');
$this->aggregator = $aggregator;
}

/**
Expand All @@ -59,19 +83,20 @@ public function addTest($test, $title)
*/
public function run($keepAlive = false)
{
// set titles
foreach ($this->titles as $index => $title) {
$this->plot->setTitle($index, $title);
}
for ($r = 1; $r <= $this->repetitions; $r++)
{
foreach ($this->input as $i) {
foreach ($this->tests as $index => $test) {
$this->bench($test, $i, $index);
}

foreach ($this->input as $i) {
foreach ($this->tests as $index => $test) {
$this->bench($test, $i, $index);
$this->plot();
}

$this->plot->refresh();
}

// make sure that the graph will be plotted at the very end...
$this->plot();

if ($keepAlive) {
// Wait for user input to close
echo "Press enter to quit.";
Expand Down Expand Up @@ -101,7 +126,44 @@ public function setInput(array $input)
$this->input = $input;
}

private function bench($benchFunction, $i, $plotIndex)
/**
* @param $repetitions
*/
public function setRepetitions($repetitions)
{
$this->repetitions = $repetitions;
}

/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}

private function plot()
{
$this->plot->reset();
$this->plot->setGraphTitle($this->title);
$this->plot->setXLabel('run');
$this->plot->setYLabel('time');

// set titles
foreach ($this->titles as $index => $title) {
$this->plot->setTitle($index, $title);
}

foreach ($this->aggregator->getData() as $i => $results) {
foreach ($results as $index => $resultValue) {
$this->plot->push($i, $resultValue, $index);
}
}

$this->plot->refresh();
}

private function bench($benchFunction, $i, $index)
{
if ($benchFunction instanceof TestInterface) {
$benchFunction->setUp($i);
Expand All @@ -114,6 +176,6 @@ private function bench($benchFunction, $i, $plotIndex)
$time = PHP_Timer::stop();
}

$this->plot->push($i, $time, $plotIndex);
$this->aggregator->push($i, $index, $time);
}
}
32 changes: 32 additions & 0 deletions src/mre/PHPench/Aggregator/AverageAggregator.php
@@ -0,0 +1,32 @@
<?php

namespace mre\PHPench\Aggregator;
use mre\PHPench\AggregatorInterface;

/**
* The average of the data will be calculated
*
* @author Markus Poerschke <markus@eluceo.de>
*/
class AverageAggregator implements AggregatorInterface
{
private $data = [];

public function push($i, $index, $value)
{
if (!isset($this->data[$i][$index]))
{
$this->data[$i][$index] = $value;

return;
}

$this->data[$i][$index] += $value;
$this->data[$i][$index] /= 2;
}

public function getData()
{
return $this->data;
}
}
25 changes: 25 additions & 0 deletions src/mre/PHPench/Aggregator/SimpleAggregator.php
@@ -0,0 +1,25 @@
<?php

namespace mre\PHPench\Aggregator;

use mre\PHPench\AggregatorInterface;

/**
* Simple aggregator that will simply replace the data
*
* @author Markus Poerschke <markus@eluceo.de>
*/
class SimpleAggregator implements AggregatorInterface
{
private $data = [];

public function push($i, $index, $value)
{
$this->data[$i][$index] = $value;
}

public function getData()
{
return $this->data;
}
}
32 changes: 32 additions & 0 deletions src/mre/PHPench/AggregatorInterface.php
@@ -0,0 +1,32 @@
<?php

namespace mre\PHPench;

/**
* Aggregates the data from the test results
*
* @author Markus Poerschke <markus@eluceo.de>
*/
interface AggregatorInterface
{
/**
* Adds a new value to the aggregator
*
* @param $i
* @param $index
* @param $value
*/
public function push($i, $index, $value);

/**
* Returns the aggregated data as an array.
*
* array(
* run_1 => array (TEST_1 => 123, TEST_2 => 456, ...)
* ...
* )
*
* @return array
*/
public function getData();
}

0 comments on commit 92f3bc9

Please sign in to comment.