This repository has been archived by the owner on May 10, 2024. It is now read-only.
forked from reactphp/http
-
Notifications
You must be signed in to change notification settings - Fork 1
/
92-client-benchmark-upload.php
122 lines (100 loc) · 3.4 KB
/
92-client-benchmark-upload.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
// a) simple 1 MB upload benchmark against public HTTP endpoint
// $ php examples/92-client-benchmark-upload.php http://httpbin.org/post 1
//
// b) local 10 GB upload benchmark against localhost address to avoid network overhead
//
// b1) first run example HTTP server:
// $ php examples/63-server-streaming-request.php 8080
//
// b2) run HTTP client sending a 10 GB upload
// $ php examples/92-client-benchmark-upload.php http://localhost:8080/ 10000
use Evenement\EventEmitter;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Loop;
use React\Http\Browser;
use React\Stream\ReadableStreamInterface;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;
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;
}
/** A readable stream that can emit a lot of data */
class ChunkRepeater extends EventEmitter implements ReadableStreamInterface
{
private $chunk;
private $count;
private $position = 0;
private $paused = true;
private $closed = false;
public function __construct($chunk, $count)
{
$this->chunk = $chunk;
$this->count = $count;
}
public function pause()
{
$this->paused = true;
}
public function resume()
{
if (!$this->paused || $this->closed) {
return;
}
// keep emitting until stream is paused
$this->paused = false;
while ($this->position < $this->count && !$this->paused) {
++$this->position;
$this->emit('data', array($this->chunk));
}
// end once the last chunk has been written
if ($this->position >= $this->count) {
$this->emit('end');
$this->close();
}
}
public function pipe(WritableStreamInterface $dest, array $options = array())
{
return Util::pipe($this, $dest, $options);
}
public function isReadable()
{
return !$this->closed;
}
public function close()
{
if ($this->closed) {
return;
}
$this->closed = true;
$this->count = 0;
$this->paused = true;
$this->emit('close');
}
public function getPosition()
{
return $this->position * strlen($this->chunk);
}
}
$client = new Browser();
$url = isset($argv[1]) ? $argv[1] : 'http://httpbin.org/post';
$n = isset($argv[2]) ? $argv[2] : 10;
$source = new ChunkRepeater(str_repeat('x', 1000000), $n);
Loop::futureTick(function () use ($source) {
$source->resume();
});
echo 'POSTing ' . $n . ' MB to ' . $url . PHP_EOL;
$start = microtime(true);
$report = Loop::addPeriodicTimer(0.05, function () use ($source, $start) {
printf("\r%d bytes in %0.3fs...", $source->getPosition(), microtime(true) - $start);
});
$client->post($url, array('Content-Length' => $n * 1000000), $source)->then(function (ResponseInterface $response) use ($source, $report, $start) {
$now = microtime(true);
Loop::cancelTimer($report);
printf("\r%d bytes in %0.3fs => %.1f MB/s\n", $source->getPosition(), $now - $start, $source->getPosition() / ($now - $start) / 1000000);
echo rtrim(preg_replace('/x{5,}/','x…', (string) $response->getBody()), PHP_EOL) . PHP_EOL;
}, function (Exception $e) use ($report) {
Loop::cancelTimer($report);
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});