Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

\GuzzleHttp\Client() + \GuzzleHttp\Promise\EachPromise consumes too much memory #468

Open
vbarbarosh opened this issue Dec 13, 2018 · 0 comments

Comments

@vbarbarosh
Copy link

No sure if this is the right place for the issue.

For some reason \GuzzleHttp\Client() used with \GuzzleHttp\Promise\EachPromise consumes too much memory.

Steps to reproduce

$ cat > index.php
    <?php

    if (!shell_exec('which composer')) {
        passthru('curl https://getcomposer.org/composer.phar > /usr/bin/composer && chmod a+x /usr/bin/composer');
        passthru('apt update && apt install -y git');
    }

    passthru('composer require guzzlehttp/guzzle');

    require_once 'vendor/autoload.php';

    $mem1 = meminfo();

    $client = new \GuzzleHttp\Client();
    $pool = function () use ($client) {
        foreach (range(1, 100) as $tmp) {
            yield $client->getAsync("https://httpbin.org/get");
        }
    };
    (new GuzzleHttp\Promise\EachPromise($pool(), ['concurrency' => 100]))->promise()->wait();

    $mem2 = meminfo();
    echo sprintf("consumed memory: %s\n", format_bytes($mem1['free'] - $mem2['free']));

    function meminfo()
    {
        preg_match('/^MemFree:\s*(\d+)/m', file_get_contents('/proc/meminfo'), $free);
        preg_match('/^MemAvailable:\s*(\d+)/m', file_get_contents('/proc/meminfo'), $available);
        $free = $free[1]*1024;
        $available = $available[1]*1024;
        return compact('free', 'available');
    }

    function format_bytes($bytes)
    {
        if ($bytes < 1000) {
            return number_format($bytes, 2);
        }
        $kilo = $bytes/1024;
        if ($kilo < 1000) {
            return number_format($kilo, 2) . 'K';
        }
        $mega = $kilo/1024;
        if ($mega < 1000) {
            return number_format($mega, 2) . 'M';
        }
        $giga = $mega/1024;
        if ($giga < 1000) {
            return number_format($giga, 2) . 'G';
        }
        return number_format($giga/1024, 2) . 'T';
    }

--- end ---

$ docker run --rm -v $PWD/index.php:/app/index.php:ro gcr.io/google-appengine/php70 php /app/index.php
[...]
consumed memory: 1.94G

$ docker run --rm -v $PWD/index.php:/app/index.php:ro gcr.io/google-appengine/php71 php /app/index.php
[...]
consumed memory: 1.80G

$ docker run --rm -v $PWD/index.php:/app/index.php:ro gcr.io/google-appengine/php72 php /app/index.php
[...]
consumed memory: 1.93G

$ docker run --rm -v $PWD/index.php:/app/index.php:ro php:7.0 php /app/index.php
[...]
consumed memory: 96.14M

$ docker run --rm -v $PWD/index.php:/app/index.php:ro php:7.1 php /app/index.php
[...]
consumed memory: 88.78M

$ docker run --rm -v $PWD/index.php:/app/index.php:ro php:7.2 php /app/index.php
[...]
consumed memory: 99.74M

$ docker run --rm -v $PWD/index.php:/app/index.php:ro php:7.3 php /app/index.php
[...]
consumed memory: 100.23M
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant