forked from omarahm/amqp-http-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp.php
48 lines (37 loc) · 1.4 KB
/
amqp.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
<?php
require_once("vendor/autoload.php");
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
/*
* Configuration
*/
$totalRequestsToSend = !empty($argv[1]) && is_numeric($argv[1]) ? $argv[1] : 30000;
$queueHost = 'ec2-52-29-1-55.eu-central-1.compute.amazonaws.com';
$queueUsername = 'admin';
$queuePassword = 'rock4me';
$queueVhost = '/';
$queuePort = 5672;
////////////
$payload = require_once("requestPayload.php");
$connection = new AMQPConnection($queueHost, $queuePort, $queueUsername, $queuePassword);
$channel = $connection->channel();
$queue = 'test-queue-direct';
$exchange = 'test-exchange-direct';
$channel->queue_declare($queue, false, true, false, false);
$channel->exchange_declare($exchange, 'direct', false, true, false);
$channel->queue_bind($queue, $exchange);
$requestsPerSecond = [];
$start = microtime(true);
for ($i = 0; $i < $totalRequestsToSend; $i++) {
$msg = new AMQPMessage($payload, array('content_type' => 'application/json'));
$channel->basic_publish($msg, $exchange);
$now = (string) time();
if (!isset($requestsPerSecond[$now])) $requestsPerSecond[$now] = 0;
$requestsPerSecond[$now]++;
}
$end = microtime(true);
$channel->close();
$connection->close();
echo str_repeat(PHP_EOL, 2);
echo "Total time: " . ($end - $start) . " seconds" . PHP_EOL;
echo "Average RPS: " . array_sum($requestsPerSecond) / count($requestsPerSecond) . " requests" . PHP_EOL;