Last active
December 17, 2015 07:59
Revisions
-
krakjoe revised this gist
May 14, 2013 . 1 changed file with 65 additions and 21 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,4 @@ <?php class Job extends \Stackable { function run() { $this->isComplete = TRUE; @@ -14,26 +9,75 @@ 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(); } } -
krakjoe revised this gist
May 14, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ function run() {} echo str_pad($i, 9), "mem usage: $mem | stack size: $stacked\n"; // Manual garbage collection doesn't help; it only makes the code really slow without mitigating memory growth. if (!($i%500)) foreach ($jobs as $id => $job) { if (is_object($job) && $job->isComplete) { unset( -
krakjoe revised this gist
May 14, 2013 . 1 changed file with 13 additions and 10 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ <?php class Jobs extends \Stackable { function run() {} } @@ -9,27 +9,30 @@ function run() { $this->isComplete = TRUE; } } class MyWorker extends \Worker { function run() {} } $jobs = []; $worker = new MyWorker($jobs); $worker->start(); for ($i=0;;) { $worker->stack($jobs[] = new Job); if (!(++$i%500)) { $mem = memory_get_usage(); $stacked = $worker->getStacked(); echo str_pad($i, 9), "mem usage: $mem | stack size: $stacked\n"; // Manual garbage collection doesn't help; it only makes the code really slow without mitigating memory growth. foreach ($jobs as $id => $job) { if (is_object($job) && $job->isComplete) { unset( $jobs[$id]); } } } -
krakjoe revised this gist
May 14, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ function run() {} echo str_pad($i, 9), "mem usage: $mem | stack size: $stacked\n"; // Manual garbage collection doesn't help; it only makes the code really slow without mitigating memory growth. foreach ((array)$jobs as $id => $job) { if ($job->isComplete) { unset($jobs[$id]); } -
rdlowrey revised this gist
May 14, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ function run() {} } $jobs = new Jobs; $worker = new MyWorker; $worker->start(); for ($i=0;;) { -
rdlowrey created this gist
May 14, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ <?php class Jobs extends \Stackable { function run() {} } class Job extends \Stackable { function run() { $this->isComplete = TRUE; } } class MyWorker extends \Worker { function run() {} } $jobs = new Jobs; $worker = new MyWorker($jobs); $worker->start(); for ($i=0;;) { $worker->stack($jobs[$i] = new Job); $stacked = $worker->getStacked(); if (!(++$i%500)) { $mem = memory_get_usage(); echo str_pad($i, 9), "mem usage: $mem | stack size: $stacked\n"; // Manual garbage collection doesn't help; it only makes the code really slow without mitigating memory growth. foreach ($jobs as $id => $job) { if ($job->isComplete) { unset($jobs[$id]); } } } }