forked from omarahm/amqp-http-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.php
57 lines (47 loc) · 1.69 KB
/
http.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
<?php
require_once("vendor/autoload.php");
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
/*
* Configuration
*/
$totalRequestsToSend = !empty($argv[1]) && is_numeric($argv[1]) ? $argv[1] : 30000;
$concurrency = 50;
$rpcCallUrl = 'http://ec2-52-29-1-55.eu-central-1.compute.amazonaws.com:30390/createProduct';
////////////
$client = new Client();
$requests = function ($total) use ($rpcCallUrl) {
$uri = $rpcCallUrl;
$payload = require_once("requestPayload.php");
for ($i = 0; $i < $total; $i++) {
yield new Request('POST', $uri, [], $payload);
}
};
$requestsPerSecond = [];
$failedRequestsPerSecond = [];
$pool = new Pool($client, $requests($totalRequestsToSend), [
'concurrency' => $concurrency,
'fulfilled' => function ($response, $index) {
global $requestsPerSecond;
$now = (string) time();
if (!isset($requestsPerSecond[$now])) $requestsPerSecond[$now] = 0;
$requestsPerSecond[$now]++;
},
'rejected' => function ($reason, $index) {
global $failedRequestsPerSecond;
$now = (string) time();
if (!isset($failedRequestsPerSecond[$now])) $failedRequestsPerSecond[$now] = 0;
$failedRequestsPerSecond[$now]++;
},
]);
// Initiate the transfers and create a promise
$start = microtime(true);
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
$end = microtime(true);
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;
echo "Failed Requests: " . array_sum($failedRequestsPerSecond) . " requests" . PHP_EOL;