-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-async-timing.php
54 lines (48 loc) · 1.66 KB
/
5-async-timing.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
<?php
require __DIR__.'/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\TransferStats;
use GuzzleHttp\HandlerStack;
$stats = function(TransferStats $stats)
{
echo $stats->getTransferTime()." ".$stats->getEffectiveUri()."\n";
};
// if you are going to use multiple clients, and want to make requests async among all of them
// you have to pass a handler into the client, so they all run in the same loop
$handler = HandlerStack::create();
$usernameClient = new Client([
'base_uri' => 'http://localhost:8000/',
'timeout' =>10 ,
'on_stats' => $stats,
'handler' => $handler,
]);
$passwordClient = new Client([
'base_uri' => 'http://localhost:8001/',
'timeout' => 10,
'on_stats' => $stats,
'handler' => $handler,
]);
$emailClient = new Client([
'base_uri' => 'http://localhost:8002/',
'timeout' =>10,
'on_stats' => $stats,
'handler' => $handler,
]);
$start = microtime(true);
$username1 = $usernameClient->post('generate-username');
$username2 = $usernameClient->post('generate-username');
$password = $passwordClient->post('generate-password');
$email = $emailClient->post('generate-email');
$stop = microtime(true);
$time = $stop-$start;
echo "4 requests in $time seconds\n";
$start = microtime(true);
$promises= [];
$promises['username1'] = $usernameClient->postAsync('generate-username');
$promises['username2'] = $usernameClient->postAsync('generate-username');
$promises['password'] = $passwordClient->postAsync('generate-password');
$promises['email'] = $emailClient->postAsync('generate-email');
$results = GuzzleHttp\Promise\unwrap($promises);
$stop = microtime(true);
$time = $stop-$start;
echo "4 requests in $time seconds\n";