Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Last active December 17, 2015 07:59

Revisions

  1. krakjoe revised this gist May 14, 2013. 1 changed file with 65 additions and 21 deletions.
    86 changes: 65 additions & 21 deletions leak.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,4 @@
    <?php

    class Jobs extends \Stackable {
    function run() {}
    }

    class Job extends \Stackable {
    function run() {
    $this->isComplete = TRUE;
    @@ -14,26 +9,75 @@ 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.
    if (!($i%500)) foreach ($jobs as $id => $job) {
    /*
    * 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(
    $jobs[$id]);
    $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();
    }
    }
  2. krakjoe revised this gist May 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion leak.php
    Original 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.
    foreach ($jobs as $id => $job) {
    if (!($i%500)) foreach ($jobs as $id => $job) {
    if (is_object($job) &&
    $job->isComplete) {
    unset(
  3. krakjoe revised this gist May 14, 2013. 1 changed file with 13 additions and 10 deletions.
    23 changes: 13 additions & 10 deletions leak.php
    Original 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 = new Jobs;
    $worker = new MyWorker;
    $jobs = [];
    $worker = new MyWorker($jobs);
    $worker->start();

    for ($i=0;;) {
    $worker->stack($jobs[$i] = new Job);
    $stacked = $worker->getStacked();

    $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 ((array)$jobs as $id => $job) {
    if ($job->isComplete) {
    unset($jobs[$id]);
    foreach ($jobs as $id => $job) {
    if (is_object($job) &&
    $job->isComplete) {
    unset(
    $jobs[$id]);
    }
    }
    }
  4. krakjoe revised this gist May 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion leak.php
    Original 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 ($jobs as $id => $job) {
    foreach ((array)$jobs as $id => $job) {
    if ($job->isComplete) {
    unset($jobs[$id]);
    }
  5. @rdlowrey rdlowrey revised this gist May 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion leak.php
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ function run() {}
    }

    $jobs = new Jobs;
    $worker = new MyWorker($jobs);
    $worker = new MyWorker;
    $worker->start();

    for ($i=0;;) {
  6. @rdlowrey rdlowrey created this gist May 14, 2013.
    36 changes: 36 additions & 0 deletions leak.php
    Original 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]);
    }
    }
    }
    }