Skip to content

Commit dd4cdb6

Browse files
Create benchmarks (#120)
1 parent 5b3417a commit dd4cdb6

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

.github/workflows/bechmark.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
on:
2+
pull_request:
3+
paths-ignore:
4+
- 'docs/**'
5+
- 'README.md'
6+
- 'CHANGELOG.md'
7+
- '.gitignore'
8+
- '.gitattributes'
9+
- 'infection.json.dist'
10+
- 'psalm.xml'
11+
- 'tests/**'
12+
13+
push:
14+
branches: ['master']
15+
paths-ignore:
16+
- 'docs/**'
17+
- 'README.md'
18+
- 'CHANGELOG.md'
19+
- '.gitignore'
20+
- '.gitattributes'
21+
- 'infection.json.dist'
22+
- 'psalm.xml'
23+
- 'tests/**'
24+
25+
name: bechmark
26+
27+
jobs:
28+
phpbench:
29+
uses: yiisoft/actions/.github/workflows/phpbench.yml@master
30+
with:
31+
os: >-
32+
['ubuntu-latest', 'windows-latest']
33+
php: >-
34+
['8.1', '8.2', '8.3', '8.4']

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"require-dev": {
3434
"infection/infection": "^0.27.8||^0.29.0",
3535
"maglnet/composer-require-checker": "^4.4",
36+
"phpbench/phpbench": "^1.4",
3637
"phpunit/phpunit": "^9.5",
3738
"rector/rector": "^2.0.15",
3839
"roave/infection-static-analysis-plugin": "^1.16",

phpbench.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
3+
"runner.bootstrap": "vendor/autoload.php",
4+
"runner.path": "tests/Benchmark",
5+
"runner.revs": 100000,
6+
"runner.iterations": 5,
7+
"runner.warmup": 5
8+
}

tests/Benchmark/QueueBench.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)