File tree Expand file tree Collapse file tree 6 files changed +125
-16
lines changed
Expand file tree Collapse file tree 6 files changed +125
-16
lines changed Original file line number Diff line number Diff line change @@ -46,4 +46,4 @@ install:
4646
4747script :
4848 - vendor/bin/phpunit --coverage-text
49- - time php examples/benchmark-throughput.php
49+ - time php examples/91- benchmark-throughput.php
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ // Simple plaintext HTTP client example (for illustration purposes only).
4+ // This shows how a plaintext TCP/IP connection is established to then send an
5+ // application level protocol message (HTTP).
6+ // Real applications should use react/http-client instead!
7+ //
8+ // This simple example only accepts an optional host parameter to send the
9+ // request to.
10+ //
11+ // $ php examples/01-http.php
12+ // $ php examples/01-http.php reactphp.org
13+
14+ use React \EventLoop \Factory ;
15+ use React \Stream \DuplexResourceStream ;
16+
17+ require __DIR__ . '/../vendor/autoload.php ' ;
18+
19+ $ host = isset ($ argv [1 ]) ? $ argv [1 ] : 'www.google.com ' ;
20+
21+ // connect to tcp://www.google.com:80 (blocking call!)
22+ // for illustration purposes only, should use react/http-client or react/socket instead!
23+ $ resource = stream_socket_client ('tcp:// ' . $ host . ':80 ' );
24+ if (!$ resource ) {
25+ exit (1 );
26+ }
27+
28+ $ loop = Factory::create ();
29+ $ stream = new DuplexResourceStream ($ resource , $ loop );
30+
31+ $ stream ->on ('data ' , function ($ chunk ) {
32+ echo $ chunk ;
33+ });
34+ $ stream ->on ('close ' , function () {
35+ echo '[CLOSED] ' . PHP_EOL ;
36+ });
37+
38+ $ stream ->write ("GET / HTTP/1.0 \r\nHost: $ host \r\n\r\n" );
39+
40+ $ loop ->run ();
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ // Simple secure HTTPS client example (for illustration purposes only).
4+ // This shows how a secure TLS connection is established to then send an
5+ // application level protocol message (HTTP).
6+ // Real applications should use react/http-client instead!
7+ //
8+ // This simple example only accepts an optional host parameter to send the
9+ // request to.
10+ //
11+ // $ php examples/02-https.php
12+ // $ php examples/02-https.php reactphp.org
13+
14+ use React \EventLoop \Factory ;
15+ use React \Stream \DuplexResourceStream ;
16+
17+ require __DIR__ . '/../vendor/autoload.php ' ;
18+
19+ $ host = isset ($ argv [1 ]) ? $ argv [1 ] : 'www.google.com ' ;
20+
21+ // connect to tls://www.google.com:443 (blocking call!)
22+ // for illustration purposes only, should use react/http-client or react/socket instead!
23+ $ resource = stream_socket_client ('tls:// ' . $ host . ':443 ' );
24+ if (!$ resource ) {
25+ exit (1 );
26+ }
27+
28+ $ loop = Factory::create ();
29+ $ stream = new DuplexResourceStream ($ resource , $ loop );
30+
31+ $ stream ->on ('data ' , function ($ chunk ) {
32+ echo $ chunk ;
33+ });
34+ $ stream ->on ('close ' , function () {
35+ echo '[CLOSED] ' . PHP_EOL ;
36+ });
37+
38+ $ stream ->write ("GET / HTTP/1.0 \r\nHost: $ host \r\n\r\n" );
39+
40+ $ loop ->run ();
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ // Simple example piping everything from STDIN to STDOUT.
4+ // This allows you to output everything you type on your keyboard or to redirect
5+ // the pipes to show contents of files and other streams.
6+ //
7+ // $ php examples/11-cat.php
8+ // $ php examples/11-cat.php < README.md
9+ // $ echo hello | php examples/11-cat.php
10+
11+ use React \EventLoop \Factory ;
12+ use React \Stream \ReadableResourceStream ;
13+ use React \Stream \WritableResourceStream ;
14+
15+ require __DIR__ . '/../vendor/autoload.php ' ;
16+
17+ if (DIRECTORY_SEPARATOR === '\\' ) {
18+ fwrite (STDERR , 'Non-blocking console I/O not supported on Microsoft Windows ' . PHP_EOL );
19+ exit (1 );
20+ }
21+
22+ $ loop = Factory::create ();
23+
24+ $ stdout = new WritableResourceStream (STDOUT , $ loop );
25+ $ stdin = new ReadableResourceStream (STDIN , $ loop );
26+ $ stdin ->pipe ($ stdout );
27+
28+ $ loop ->run ();
Original file line number Diff line number Diff line change 11<?php
22
3+ // Benchmark to measure throughput performance piping an input stream to an output stream.
4+ // This allows you to get an idea of how fast stream processing with PHP can be
5+ // and also to play around with differnt types of input and output streams.
6+ //
7+ // This example accepts a number of parameters to control the timeout (-t 1),
8+ // the input file (-i /dev/zero) and the output file (-o /dev/null).
9+ //
10+ // $ php examples/91-benchmark-throughput.php
11+ // $ php examples/91-benchmark-throughput.php -t 10 -o zero.bin
12+ // $ php examples/91-benchmark-throughput.php -t 60 -i zero.bin
13+
314require __DIR__ . '/../vendor/autoload.php ' ;
415
16+ if (DIRECTORY_SEPARATOR === '\\' ) {
17+ fwrite (STDERR , 'Non-blocking console I/O not supported on Microsoft Windows ' . PHP_EOL );
18+ exit (1 );
19+ }
20+
521$ args = getopt ('i:o:t: ' );
622$ if = isset ($ args ['i ' ]) ? $ args ['i ' ] : '/dev/zero ' ;
723$ of = isset ($ args ['o ' ]) ? $ args ['o ' ] : '/dev/null ' ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments