Изучаю amqp используя брокер rabbitmq и единственное расширение в виде модуля к нему.
Представим ситуацию когда нам нужно создать много точек обмена и очередей сразу. Напишем скрипт, который в цикле создаст всё что нужно, используя расширение:
<?php <br><br>
set_time_limit(0);<br>
$start = microtime(true);<br><br>
$connection = new AMQPConnection();<br>
$connection->connect();<br><br>
if (!$connection->isConnected()) {<br>
die('Not connected :(' . PHP_EOL);<br>
}<br><br>
// Open Channel<br>
$channel = new AMQPChannel($connection);<br>
$exchange = new AMQPExchange($channel);<br>
$queue = new AMQPQueue($channel);<br><br>
$i = 1;<br>
while($i<1000) {<br><br>
$exchangeName = 'ex_'.random_key(5, false).'_'.$i;<br>
$queueName = 'qu_'.random_key(5, false).'_'.$i;<br><br>
// Create Exchange<br>
$exchange->setName($exchangeName);<br>
$exchange->setType('topic');<br>
$exchange->declare();<br><br>
// Create Queue<br>
$queue->setName($queueName);<br>
$queue->declare();<br><br>
if ($i%100 === 0) {<br>
echo $i.' Memory usage is '.memory_get_usage_(), PHP_EOL; <br>
}<br><br>
$i++;<br>
}<br><br>
$connection->disconnect();<br><br>
echo 'It took '.(microtime(true) - $start).' seconds for php.', PHP_EOL;<br><br>
// Generate a random key of length $len<br>
function random_key($len, $digit=true, $readable = true, $strlower=true){ <br>
$key = ''; <br>
if ($readable) <br>
{ <br>
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; <br>
if($strlower) { $chars = substr($chars, 26); }; <br>
if($digit) { $chars = substr($chars, -10); }; <br><br>
for ($i = 0; $i < $len; ++$i) { $key .= substr($chars, (mt_rand() % strlen($chars)), 1);};<br>
} else { <br>
for ($i = 0; $i < $len; ++$i) { $key .= chr(mt_rand(33, 126)); }; <br>
}; <br>
return $key; <br>
}<br><br>
function memory_get_usage_(){ <br>
$pid = getmypid(); exec("ps -o rss -p $pid", $output); <br>
return $output[1] *1024; <br>
} <br>
И скрипт который использует библиотеку php-amqplib —
github.com/videlalvaro/php-amqplib (выбрал потому что «живая»):
<?php <br><br>
include '/usr/src/php-amqplib/demo/config.php';<br>
use PhpAmqpLib\Connection\AMQPConnection;<br>
use PhpAmqpLib\Message\AMQPMessage;<br><br>
set_time_limit(0);<br>
$start = microtime(true);<br><br>
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);<br>
$channel = $connection->channel();<br><br>
$i = 1;<br>
while($i<1001) {<br><br>
$exchangeName = 'ex_'.random_key(5, false).'_'.$i;<br>
$queueName = 'qu_'.random_key(5, false).'_'.$i;<br><br>
// Create Exchange<br>
$channel->exchange_declare($exchangeName, 'topic', false, false, false);<br><br>
// Create Queue<br>
$channel->queue_declare($queueName, false, false, false, false);<br><br>
if ($i%100 === 0) {<br>
echo $i.' Memory usage is '.memory_get_usage_(), PHP_EOL; <br>
}<br><br>
$i++;<br>
}<br><br>
$channel->close();<br>
$connection->close();<br><br>
print 'It took '.(microtime(true) - $start).' seconds for php.'."\n";<br><br>
// Generate a random key of length $len<br>
function random_key($len, $digit=true, $readable = true, $strlower=true){ <br>
$key = ''; <br>
if ($readable) <br>
{ <br>
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; <br>
if($strlower) { $chars = substr($chars, 26); }; <br>
if($digit) { $chars = substr($chars, -10); }; <br><br>
for ($i = 0; $i < $len; ++$i) { $key .= substr($chars, (mt_rand() % strlen($chars)), 1);};<br>
} else { <br>
for ($i = 0; $i < $len; ++$i) { $key .= chr(mt_rand(33, 126)); }; <br>
}; <br>
return $key; <br>
}<br><br>
function memory_get_usage_(){ <br>
$pid = getmypid(); exec("ps -o rss -p $pid", $output); <br>
return $output[1] *1024; <br>
} <br>
Вызовем первый скрипт
php /home/www/application/controllers/rabbit_memory_test.php<br>
100 Memory usage is 23277568<br>
200 Memory usage is 39096320<br>
300 Memory usage is 56262656<br>
400 Memory usage is 74784768<br>
500 Memory usage is 94380032<br>
600 Memory usage is 115019776<br>
700 Memory usage is 135467008<br>
800 Memory usage is 156147712<br>
900 Memory usage is 177041408<br>
It took 2.9051361083984 seconds for php.<br>
Второй
php /home/www/application/controllers/rabbit_memory_test2.php<br>
100 Memory usage is 9306112<br>
200 Memory usage is 9342976<br>
300 Memory usage is 9342976<br>
400 Memory usage is 9342976<br>
500 Memory usage is 9342976<br>
600 Memory usage is 9342976<br>
700 Memory usage is 9342976<br>
800 Memory usage is 9342976<br>
900 Memory usage is 9342976<br>
1000 Memory usage is 9342976<br>
It took 2.7014260292053 seconds for php.<br>
Может быть я неправильно использую модуль? Или явно это memory leak?