Last active
December 23, 2015 09:39
-
-
Save rande/6615971 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 | |
namespace Stats\Test; | |
use Stats\Entry; | |
interface ComputerInterface | |
{ | |
/** | |
* @return string | |
*/ | |
public function getName(); | |
/** | |
* @param Entry $entry | |
*/ | |
public function handle(Entry $entry); | |
/** | |
* @return mixed | |
*/ | |
public function get(); | |
} | |
class SumComputer implements ComputerInterface | |
{ | |
protected $total = 0; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'sum'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function handle(Entry $entry) | |
{ | |
$this->total += $entry->value; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function get() | |
{ | |
return $this->total; | |
} | |
} | |
class AvgComputer implements ComputerInterface | |
{ | |
protected $count; | |
protected $total; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'avg'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function handle(Entry $entry) | |
{ | |
$this->total += $entry->value; | |
$this->count++; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function get() | |
{ | |
return $this->total / $this->count; | |
} | |
} | |
class MinComputer implements ComputerInterface | |
{ | |
protected $min = false; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'min'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function handle(Entry $entry) | |
{ | |
if ($this->min === false) { | |
$this->min = $entry->value; | |
} | |
if ($this->min > $entry->value) { | |
$this->min = $entry->value; | |
} | |
} | |
/** | |
* @return mixed | |
*/ | |
public function get() | |
{ | |
return $this->min; | |
} | |
} | |
class Collection | |
{ | |
protected $data = array(); | |
protected $count = 0; | |
protected $computers = array(); | |
public function addComputer($code, ComputerInterface $listener) | |
{ | |
$this->computers[$code] = $listener; | |
} | |
/** | |
* @param Entry $entry | |
*/ | |
public function addEntry(Entry $entry) | |
{ | |
$this->data[] = $entry; | |
foreach ($this->computers as $code => $listener) { | |
$listener->handle($entry); | |
} | |
} | |
/** | |
* @return \ArrayIterator | |
*/ | |
public function getIterator() | |
{ | |
return new \ArrayIterator($this->data); | |
} | |
/** | |
* @return array | |
*/ | |
public function getComputers() | |
{ | |
return $this->computers; | |
} | |
} | |
class ComputeTest extends \PHPUnit_Framework_TestCase | |
{ | |
public function testInitialize() | |
{ | |
$collection = new Collection(); | |
$collection->addComputer('sum', new SumComputer()); | |
$collection->addComputer('min', new MinComputer()); | |
$collection->addComputer('avg', new AvgComputer()); | |
for ($i = 0; $i < 100000; $i++) { | |
$collection->addEntry(Entry::create('stat.value', rand(2, 100))); | |
} | |
foreach ($collection->getComputers() as $name => $computer) { | |
var_dump(sprintf("%s => %f", $name, $computer->get())); | |
} | |
} | |
} |
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
string(21) "sum => 5116025.000000" | |
string(15) "min => 2.000000" | |
string(16) "avg => 51.160250" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment