forked from php-amqp/php-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.php
103 lines (74 loc) · 2.85 KB
/
benchmark.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
set_time_limit(getenv('TEST_TIMEOUT') ?: 120);
try {
$ext =new ReflectionExtension('xdebug');
$xdebug = '(with xdebug '.$ext->getVersion().')';
} catch (Exception $e) {
$xdebug = '(without xdebug)';
}
$ext = new ReflectionExtension('amqp');
$srcVersion = $ext->getVersion();
echo 'Running benchmark for php-amqp ', $srcVersion, ' on PHP ', PHP_VERSION, ' ', $xdebug, PHP_EOL;
$iterations = 10000;
if (isset($argv[1])) {
$iterations = max((int) $argv[1], 0) ?: $iterations;
}
echo ' running ', $iterations, ' iterations:', PHP_EOL, PHP_EOL;
$conn = new AMQPConnection();
$conn->setHost(getenv('PHP_AMQP_HOST'));
$conn->connect();
$ch = new AMQPChannel($conn);
$exchange = new AMQPExchange($ch);
$exchange->setType(AMQP_EX_TYPE_FANOUT);
$exchange->setFlags(AMQP_AUTODELETE);
$exchange->setName('benchmark_exchange_' . bin2hex(random_bytes(32)));
$exchange->declareExchange();
$q = new AMQPQueue($ch);
$q->setFlags(AMQP_AUTODELETE);
$q->declareQueue();
$q->bind($exchange->getName());
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
$timer = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$exchange->publish($message);
}
$timer = microtime(true) - $timer;
echo 'Publish: ', $iterations, ' iterations took ', $timer, 'sec', PHP_EOL;
$timer = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
if (!$q->get(AMQP_AUTOACK)) {
echo 'GET failed', PHP_EOL;
}
}
$timer = microtime(true) - $timer;
echo ' Get: ', $iterations, ' iterations took ', $timer, 'sec', PHP_EOL;
$q->delete();
$exchange->delete();
echo PHP_EOL;
// ==================================
$exchange = new AMQPExchange($ch);
$exchange->setType(AMQP_EX_TYPE_FANOUT);
$exchange->setFlags(AMQP_AUTODELETE);
$exchange->setName('benchmark_exchange_' . bin2hex(random_bytes(32)));
$exchange->declareExchange();
$q = new AMQPQueue($ch);
$q->setFlags(AMQP_AUTODELETE);
$q->declareQueue();
$q->bind($exchange->getName());
$timer = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$exchange->publish($message);
}
$timer = microtime(true) - $timer;
echo 'Publish: ', $iterations, ' iterations took ', $timer, 'sec', PHP_EOL;
$consumer_iterations = $iterations;
$timer = microtime(true);
$q->consume(
function () use (&$consumer_iterations) {
return (--$consumer_iterations > 0);
},
AMQP_AUTOACK);
$timer = microtime(true) - $timer;
echo 'Consume: ', $iterations, ' iterations took ', $timer, 'sec', PHP_EOL;
$q->delete();
$exchange->delete();