|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Benchmark; |
| 6 | + |
| 7 | +use PhpAmqpLib\Connection\AMQPStreamConnection; |
| 8 | +use PhpBench\Attributes\AfterMethods; |
| 9 | +use PhpBench\Attributes\BeforeMethods; |
| 10 | +use PhpBench\Attributes\Iterations; |
| 11 | +use PhpBench\Attributes\OutputMode; |
| 12 | +use PhpBench\Attributes\OutputTimeUnit; |
| 13 | +use PhpBench\Attributes\Revs; |
| 14 | +use Yiisoft\Queue\AMQP\Adapter; |
| 15 | +use Yiisoft\Queue\AMQP\QueueProvider; |
| 16 | +use Yiisoft\Queue\AMQP\Settings\Queue as QueueSettings; |
| 17 | +use Yiisoft\Queue\Cli\SimpleLoop; |
| 18 | +use Yiisoft\Queue\Message\JsonMessageSerializer; |
| 19 | +use Yiisoft\Queue\Message\Message; |
| 20 | + |
| 21 | +final class QueueBench |
| 22 | +{ |
| 23 | + private const CONSUME_MESSAGE_COUNT = 10_000; |
| 24 | + |
| 25 | + private Adapter $adapter; |
| 26 | + |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + $loop = new SimpleLoop(); |
| 30 | + $serializer = new JsonMessageSerializer(); |
| 31 | + $queueProvider = new QueueProvider( |
| 32 | + new AMQPStreamConnection( |
| 33 | + getenv('RABBITMQ_HOST'), |
| 34 | + getenv('RABBITMQ_PORT'), |
| 35 | + getenv('RABBITMQ_USER'), |
| 36 | + getenv('RABBITMQ_PASSWORD'), |
| 37 | + ), |
| 38 | + new QueueSettings() |
| 39 | + ); |
| 40 | + $adapter = new Adapter($queueProvider, $serializer, $loop); |
| 41 | + |
| 42 | + $this->adapter = $adapter; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * How fast we can push 1 message |
| 47 | + */ |
| 48 | + #[Iterations(5)] |
| 49 | + #[Revs(self::CONSUME_MESSAGE_COUNT)] |
| 50 | + #[BeforeMethods('cleanupQueue')] |
| 51 | + #[AfterMethods('cleanupQueue')] |
| 52 | + #[OutputMode('throughput')] |
| 53 | + #[OutputTimeUnit('seconds')] |
| 54 | + public function benchPush(): void |
| 55 | + { |
| 56 | + $this->adapter->push(new Message('test', ['payload' => 'test'])); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * How fast we can push 100 messages |
| 61 | + */ |
| 62 | + #[Iterations(5)] |
| 63 | + #[Revs(100)] |
| 64 | + #[BeforeMethods('cleanupQueue')] |
| 65 | + #[AfterMethods('cleanupQueue')] |
| 66 | + #[OutputMode('throughput')] |
| 67 | + #[OutputTimeUnit('seconds')] |
| 68 | + public function benchPushBatch(): void |
| 69 | + { |
| 70 | + $message = new Message('test', ['payload' => 'test']); |
| 71 | + for ($i = 0; $i < 100; $i++) { |
| 72 | + $this->adapter->push($message); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * How fast we can consume 100_000 messages |
| 78 | + */ |
| 79 | + #[Iterations(5)] |
| 80 | + #[Revs(1)] |
| 81 | + #[BeforeMethods('cleanupQueue')] |
| 82 | + #[BeforeMethods('pushMessagesForConsume')] |
| 83 | + public function benchConsume(): void |
| 84 | + { |
| 85 | + $this->adapter->runExisting(static fn (): bool => true); |
| 86 | + } |
| 87 | + |
| 88 | + public function pushMessagesForConsume(): void |
| 89 | + { |
| 90 | + for ($i = 0; $i < self::CONSUME_MESSAGE_COUNT; $i++) { |
| 91 | + $this->adapter->push(new Message('test', ['payload' => 'test'])); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public function cleanupQueue(): void |
| 96 | + { |
| 97 | + $this->adapter->runExisting(static fn (): bool => true); |
| 98 | + } |
| 99 | +} |
0 commit comments