database icon indicating copy to clipboard operation
database copied to clipboard

Memory usage

Open tricarte opened this issue 2 years ago • 0 comments

Hello. Thanks for this library.

In the example code, you say there is no need to return the connection back as they will be returned when the coroutines are destroyed. I need a clarification here. Here is a sample code. Notice that I have changed the namespace but it is irrelevant at this point:

<?php

require __DIR__ . '/vendor/autoload.php';
(new Database\ConnectionPool\PDOConfig())
    ->withDriver('mysql')
    ->withHost('127.0.0.1')
    ->withDbname('someDatabase')
    ->withUsername('admin')
    ->withPassword('password')
    ->withCharset('utf8mb4')
    ->setConfig('default');

Database\ConnectionPool\PoolManager::addPool(2, 'default');

\Swoole\Runtime::enableCoroutine();
\Swoole\Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]);

echo "Benchmark started\n";
for ($i = 0; $i < 15; $i++) {
    $s = microtime(true);
    \Swoole\Coroutine\run(function () {
        for ($i = 0; $i < 10000; $i++) {
            \Swoole\Coroutine::create(function () {
                $builder = Database\ConnectionPool\Adapter\Manager::table('people');
                $people = $builder->get('name');
            });
        }
    });
    echo 'Benchmark time:' . (microtime(true) - $s) . "second[s]\n";
}

sleep(5);

It's just your example with a sleep() added at the end. And I create 10000 coroutines in a loop exactly 15 times. On each iteration of the outer loop, the memory used raises. This is what I found abnormal. So the question is: Is this the normal behaviour?

Actually, I tried to integrate this library into another Swoole based web framework called Fomo. Interestingly when I benchmark the relevant url, on each benchmark, used memory keeps raising and raising.

tricarte avatar Jan 31 '24 14:01 tricarte