Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"require-dev": {
"infection/infection": "^0.27.8||^0.29.0",
"maglnet/composer-require-checker": "^4.4",
"phpbench/phpbench": "^1.4",
"phpunit/phpunit": "^9.5",
"rector/rector": "^2.0.15",
"roave/infection-static-analysis-plugin": "^1.16",
Expand Down
8 changes: 8 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests/Benchmark",
"runner.revs": 100000,
"runner.iterations": 5,
"runner.warmup": 5
}
54 changes: 28 additions & 26 deletions src/ExistingMessagesConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,36 @@
*/
public function consume(callable $callback): void
{
$this->channel->basic_consume(
$this->queueName,
'',
false,
false,
false,
false,
function (AMQPMessage $amqpMessage) use ($callback): void {
try {
$message = $this->serializer->unserialize($amqpMessage->getBody());
if ($this->messageConsumed = $callback($message)) {
$this->channel->basic_ack($amqpMessage->getDeliveryTag());
}
} catch (Throwable $exception) {
$this->messageConsumed = false;
$consumerTag = $amqpMessage->getConsumerTag();
if ($consumerTag !== null) {
$this->channel->basic_cancel($consumerTag);
}
$consumerTag = uniqid(more_entropy: true);
try {

Check warning on line 33 in src/ExistingMessagesConsumer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "UnwrapFinally": @@ @@ public function consume(callable $callback): void { $consumerTag = uniqid(more_entropy: true); - try { - $this->channel->basic_consume($this->queueName, $consumerTag, false, false, false, false, function (AMQPMessage $amqpMessage) use ($callback): void { - try { - $message = $this->serializer->unserialize($amqpMessage->getBody()); - if ($this->messageConsumed = $callback($message)) { - $this->channel->basic_ack($amqpMessage->getDeliveryTag()); - } - } catch (Throwable $exception) { - $this->messageConsumed = false; - $this->channel->basic_nack($amqpMessage->getDeliveryTag(), false, true); - throw $exception; + $this->channel->basic_consume($this->queueName, $consumerTag, false, false, false, false, function (AMQPMessage $amqpMessage) use ($callback): void { + try { + $message = $this->serializer->unserialize($amqpMessage->getBody()); + if ($this->messageConsumed = $callback($message)) { + $this->channel->basic_ack($amqpMessage->getDeliveryTag()); } - }); - do { + } catch (Throwable $exception) { $this->messageConsumed = false; - $this->channel->wait(null, true); - } while ($this->messageConsumed === true); - } finally { - $this->channel->basic_cancel($consumerTag, false, false); - } + $this->channel->basic_nack($amqpMessage->getDeliveryTag(), false, true); + throw $exception; + } + }); + do { + $this->messageConsumed = false; + $this->channel->wait(null, true); + } while ($this->messageConsumed === true); + $this->channel->basic_cancel($consumerTag, false, false); } }
$this->channel->basic_consume(
$this->queueName,
$consumerTag,
false,

Check warning on line 37 in src/ExistingMessagesConsumer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": @@ @@ { $consumerTag = uniqid(more_entropy: true); try { - $this->channel->basic_consume($this->queueName, $consumerTag, false, false, false, false, function (AMQPMessage $amqpMessage) use ($callback): void { + $this->channel->basic_consume($this->queueName, $consumerTag, true, false, false, false, function (AMQPMessage $amqpMessage) use ($callback): void { try { $message = $this->serializer->unserialize($amqpMessage->getBody()); if ($this->messageConsumed = $callback($message)) {
false,
false,

Check warning on line 39 in src/ExistingMessagesConsumer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": @@ @@ { $consumerTag = uniqid(more_entropy: true); try { - $this->channel->basic_consume($this->queueName, $consumerTag, false, false, false, false, function (AMQPMessage $amqpMessage) use ($callback): void { + $this->channel->basic_consume($this->queueName, $consumerTag, false, false, true, false, function (AMQPMessage $amqpMessage) use ($callback): void { try { $message = $this->serializer->unserialize($amqpMessage->getBody()); if ($this->messageConsumed = $callback($message)) {
false,
function (AMQPMessage $amqpMessage) use ($callback): void {
try {
$message = $this->serializer->unserialize($amqpMessage->getBody());
if ($this->messageConsumed = $callback($message)) {
$this->channel->basic_ack($amqpMessage->getDeliveryTag());

Check warning on line 45 in src/ExistingMessagesConsumer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ try { $message = $this->serializer->unserialize($amqpMessage->getBody()); if ($this->messageConsumed = $callback($message)) { - $this->channel->basic_ack($amqpMessage->getDeliveryTag()); + } } catch (Throwable $exception) { $this->messageConsumed = false;
}
} catch (Throwable $exception) {
$this->messageConsumed = false;
$this->channel->basic_nack($amqpMessage->getDeliveryTag(), false, true);

throw $exception;
throw $exception;
}
}
}
);
);

do {
$this->messageConsumed = false;
$this->channel->wait(null, true);
} while ($this->messageConsumed === true);
do {
$this->messageConsumed = false;
$this->channel->wait(null, true);
} while ($this->messageConsumed === true);
} finally {
$this->channel->basic_cancel($consumerTag, false, false);

Check warning on line 61 in src/ExistingMessagesConsumer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ $this->channel->wait(null, true); } while ($this->messageConsumed === true); } finally { - $this->channel->basic_cancel($consumerTag, false, false); + } } }

Check warning on line 61 in src/ExistingMessagesConsumer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": @@ @@ $this->channel->wait(null, true); } while ($this->messageConsumed === true); } finally { - $this->channel->basic_cancel($consumerTag, false, false); + $this->channel->basic_cancel($consumerTag, false, true); } } }
}
}
}
72 changes: 72 additions & 0 deletions tests/Benchmark/QueueBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Benchmark;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpBench\Attributes\OutputMode;
use PhpBench\Attributes\OutputTimeUnit;
use PhpBench\Attributes\Skip;
use Yiisoft\Queue\AMQP\Adapter;
use Yiisoft\Queue\AMQP\QueueProvider;
use Yiisoft\Queue\AMQP\Settings\Queue as QueueSettings;
use Yiisoft\Queue\Cli\SimpleLoop;
use Yiisoft\Queue\Message\JsonMessageSerializer;
use Yiisoft\Queue\Message\Message;

final class QueueBench
{
private const CONSUME_REVISIONS = 100_000;

private Adapter $adapter;

public function __construct()
{
$loop = new SimpleLoop();
$serializer = new JsonMessageSerializer();
$queueProvider = new QueueProvider(
new AMQPStreamConnection(
getenv('RABBITMQ_HOST'),
getenv('RABBITMQ_PORT'),
getenv('RABBITMQ_USER'),
getenv('RABBITMQ_PASSWORD'),
),
new QueueSettings()
);
$adapter = new Adapter($queueProvider, $serializer, $loop);

$this->adapter = $adapter;
}

#[\PhpBench\Attributes\BeforeMethods('cleanupQueue')]
#[OutputMode('throughput')]
#[OutputTimeUnit('seconds', 3)]
#[Skip]
public function benchPush(): void
{
$this->adapter->push(new Message('test', ['payload' => 'test']));
}

public function cleanupQueue(): void
{
$this->adapter->runExisting(static fn (): bool => true);
}

#[\PhpBench\Attributes\Iterations(5)]
#[\PhpBench\Attributes\Revs(self::CONSUME_REVISIONS)]
#[\PhpBench\Attributes\BeforeMethods('pushMessagesForConsume')]
#[OutputMode('throughput')]
#[OutputTimeUnit('seconds', 3)]
public function benchConsume(): void
{
$this->adapter->runExisting(static fn (): bool => false);
}

public function pushMessagesForConsume(): void
{
for ($i = 0; $i < self::CONSUME_REVISIONS; $i++) {
$this->adapter->push(new Message('test', ['payload' => 'test']));
}
}
}
Loading