1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
<?php
/**
* Usage:
* - Publish 100 5mb messages:
* php file_publish.php 100
* - Publish 1 5mb message:
* php file_publish.php
*
* NOTE: The script will take some time while it reads data from /dev/urandom
*/
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../tests/config.php';
//suboptimal function to generate random content
function generate_random_content($bytes)
{
$handle = @fopen('/dev/urandom', 'rb');
$buffer = '';
if ($handle) {
$len = 0;
$max = $bytes;
while ($len < $max - 1) {
$buffer .= fgets($handle, $max - $len);
$len = mb_strlen($buffer, 'ASCII');
}
fclose($handle);
}
return $buffer;
}
$exchange = 'file_exchange';
$queue = 'file_queue';
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
$ch->queue_declare($queue, false, false, false, false);
$ch->exchange_declare($exchange, 'direct', false, false, false);
$ch->queue_bind($queue, $exchange);
$max = isset($argv[1]) ? (int) $argv[1] : 1;
$msg_size = 1024 * 1024 * 5 + 1;
$msg_body = generate_random_content($msg_size);
$msg = new AMQPMessage($msg_body);
$time = microtime(true);
// Publishes $max messages using $msg_body as the content.
for ($i = 0; $i < $max; $i++) {
$ch->basic_publish($msg, $exchange);
}
echo microtime(true) - $time, "\n";
$ch->basic_publish(new AMQPMessage('quit'), $exchange);
$ch->close();
$conn->close();
|