-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
91-benchmark-download.php
61 lines (45 loc) · 1.93 KB
/
91-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
60
61
<?php
// a) simple download benchmark against public HTTP endpoint:
// $ php examples/91-benchmark-download.php http://httpbin.org/get
// b) local 10 GB download benchmark against localhost address to avoid network overhead
//
// b1) first run example HTTP server, e.g. from react/http:
// $ cd workspace/reactphp-http
// $ php examples/99-benchmark-download.php 8080
//
// b2) run HTTP client receiving a 10 GB download:
// $ php examples/91-benchmark-download.php http://localhost:8080/10g.bin
use Clue\React\Buzz\Browser;
use Psr\Http\Message\ResponseInterface;
use React\Stream\ReadableStreamInterface;
$url = isset($argv[1]) ? $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;
}
$loop = React\EventLoop\Factory::create();
$client = new Browser($loop);
echo 'Requesting ' . $url . '…' . PHP_EOL;
$client->requestStreaming('GET', $url)->then(function (ResponseInterface $response) use ($loop) {
echo 'Headers received' . PHP_EOL;
echo RingCentral\Psr7\str($response);
$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, $loop, $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;
});
}, 'printf');
$loop->run();