Skip to content

Instantly share code, notes, and snippets.

@WyriHaximus
Created July 27, 2025 14:19
Show Gist options
  • Save WyriHaximus/65fd98e099820aded1b79e9111e02916 to your computer and use it in GitHub Desktop.
Save WyriHaximus/65fd98e099820aded1b79e9111e02916 to your computer and use it in GitHub Desktop.
ext-amqp benchmark for Bunny
<?php
declare(strict_types=1);
namespace Bunny;
use function getmypid;
use function microtime;
use function printf;
require_once __DIR__ . '/../vendor/autoload.php';
//Establish connection to AMQP
$connection = new \AMQPConnection();
$connection->setHost('127.0.0.1');
$connection->setLogin('guest');
$connection->setPassword('guest');
$connection->connect();
//Create and declare channel
$channel = new \AMQPChannel($connection);
$queue = new \AMQPQueue($channel);
$queue->setName('bench_queue');
$queue->setFlags(AMQP_NOPARAM);
$queue->declareQueue();
$time = null;
$count = 0;
$queue->consume(static function (\AMQPEnvelope $msg, \AMQPQueue $queue) use (&$time, &$count, $connection): void {
if ($time === null) {
$time = microtime(true);
}
if ($msg->getBody() === 'quit') {
$runTime = microtime(true) - $time;
printf("Consume: Pid: %s, Count: %s, Time: %.6f, Msg/sec: %.0f\n", getmypid(), $count, $runTime, (1 / $runTime) * $count);
$queue->ack($msg->getDeliveryTag());
$queue->cancel();
$connection->disconnect();
} else {
++$count;
$queue->ack($msg->getDeliveryTag());
}
});
<?php
declare(strict_types=1);
namespace Bunny;
use function getmypid;
use function microtime;
use function printf;
require_once __DIR__ . '/../vendor/autoload.php';
//Establish connection to AMQP
$connection = new \AMQPConnection();
$connection->setHost('127.0.0.1');
$connection->setLogin('guest');
$connection->setPassword('guest');
$connection->connect();
//Create and declare channel
$channel = new \AMQPChannel($connection);
//AMQPC Exchange is the publishing mechanism
$exchange = new \AMQPExchange($channel);
$queue = new \AMQPQueue($channel);
$queue->setName('bench_queue');
$queue->setFlags(AMQP_NOPARAM);
$queue->declareQueue();
$body = <<<'EOT'
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza
EOT;
$time = microtime(true);
$max = isset($argv[1]) ? (int) $argv[1] : 1;
for ($i = 0; $i < $max; $i++) {
$exchange->publish($body, 'bench_queue');
}
$runTime = microtime(true) - $time;
printf("Produce: Pid: %s, Time: %.6f, Msg/sec: %.0f\n", getmypid(), $runTime, (1 / $runTime) * $max);
$exchange->publish('quit', 'bench_queue');
$connection->disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment