Last active
December 17, 2015 07:59
-
-
Save krakjoe/5576666 to your computer and use it in GitHub Desktop.
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 | |
class Job extends \Stackable { | |
function run() { | |
$this->isComplete = TRUE; | |
} | |
} | |
class MyWorker extends \Worker { | |
function run() {} | |
} | |
/* | |
* Container for workers and jobs | |
*/ | |
class MyPool { | |
public $workers = []; | |
public $jobs = []; | |
/* | |
* Preinitialize Worker Threads | |
*/ | |
public function __construct($max, $inherit = PTHREADS_INHERIT_NONE) { | |
if ($max) { | |
/* create threads */ | |
for ($start = 0; $start < $max; $start++) { | |
$this->workers[ | |
$start] = new MyWorker(); | |
$this->workers[ | |
$start]->start($inherit); | |
} | |
} | |
} | |
/* submit to random worker */ | |
public function submit(Job $job){ | |
if (count($this->workers)) { | |
$this->workers[ | |
array_rand($this->workers)]->stack( | |
$this->jobs[]=$job); | |
} else $this->workers[0]->stack( | |
$this->jobs[]=$job); | |
} | |
/* clean pool */ | |
public function clean() { | |
foreach ($this->jobs as $id => $job) { | |
if (is_object($job) && | |
$job->isComplete) { | |
unset( | |
$this->jobs[$id]); | |
} | |
} | |
} | |
} | |
/* normal array */ | |
$start = microtime(true); | |
/* create wrapper round multiple workers */ | |
/* @NOTE see what happens when you raise amount of available workers ... */ | |
$pool = new MyPool(1); | |
printf( | |
"JOB\tMem\tReal\tTime\tJPS\n"); | |
$last = []; | |
for ($i=0;;) { | |
$pool->submit(new Job()); | |
if (($i++%5000)==0) { | |
echo vsprintf("%d\t%d\t%d\t%.4f\t%d\n", $last=array( | |
"job" => $i, | |
"mem" => memory_get_usage(), | |
"real" => memory_get_usage(true), | |
"runtime" => $runtime = (microtime(true) - $start), | |
"jps" => ceil($i / $runtime) | |
)); | |
$pool->clean(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
spin off to create pool of workers & show tidy in production ...