-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
91-client-benchmark-download.php
59 lines (44 loc) · 1.91 KB
/
91-client-benchmark-download.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
<?php
// a) simple download benchmark against public HTTP endpoint:
// $ php examples/91-client-benchmark-download.php http://httpbingo.org/get
// b) local 10 GB download benchmark against localhost address to avoid network overhead
//
// b1) first run example HTTP server:
// $ php examples/99-server-benchmark-download.php 8080
//
// b2) run HTTP client receiving a 10 GB download:
// $ php examples/91-client-benchmark-download.php http://localhost:8080/10g.bin
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Loop;
use React\Http\Browser;
use React\Stream\ReadableStreamInterface;
$url = $argv[1] ?? 'http://google.com/';
require __DIR__ . '/../vendor/autoload.php';
if (extension_loaded('xdebug')) {
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}
$client = new Browser();
echo 'Requesting ' . $url . '…' . PHP_EOL;
$client->requestStreaming('GET', $url)->then(function (ResponseInterface $response) {
echo 'Received ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . PHP_EOL;
$stream = $response->getBody();
assert($stream instanceof ReadableStreamInterface);
// count number of bytes received
$bytes = 0;
$stream->on('data', function ($chunk) use (&$bytes) {
$bytes += strlen($chunk);
});
// report progress every 0.1s
$timer = Loop::addPeriodicTimer(0.1, function () use (&$bytes) {
echo "\rDownloaded " . $bytes . " bytes…";
});
// report results once the stream closes
$time = microtime(true);
$stream->on('close', function() use (&$bytes, $timer, $time) {
Loop::cancelTimer($timer);
$time = microtime(true) - $time;
echo "\r" . 'Downloaded ' . $bytes . ' bytes in ' . round($time, 3) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});