aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/php/vendor/react/stream/examples
diff options
context:
space:
mode:
authormarvin-borner@live.com2018-04-10 21:50:16 +0200
committermarvin-borner@live.com2018-04-10 21:54:48 +0200
commitfc9401f04a3aca5abb22f87ebc210de8afe11d32 (patch)
treeb0b310f3581764ec3955f4e496a05137a32951c3 /assets/php/vendor/react/stream/examples
parent286d643180672f20526f3dc3bd19d7b751e2fa97 (diff)
Initial Commit
Diffstat (limited to 'assets/php/vendor/react/stream/examples')
-rw-r--r--assets/php/vendor/react/stream/examples/01-http.php40
-rw-r--r--assets/php/vendor/react/stream/examples/02-https.php40
-rw-r--r--assets/php/vendor/react/stream/examples/11-cat.php28
-rw-r--r--assets/php/vendor/react/stream/examples/91-benchmark-throughput.php62
4 files changed, 170 insertions, 0 deletions
diff --git a/assets/php/vendor/react/stream/examples/01-http.php b/assets/php/vendor/react/stream/examples/01-http.php
new file mode 100644
index 0000000..3687f7c
--- /dev/null
+++ b/assets/php/vendor/react/stream/examples/01-http.php
@@ -0,0 +1,40 @@
+<?php
+
+// Simple plaintext HTTP client example (for illustration purposes only).
+// This shows how a plaintext TCP/IP connection is established to then send an
+// application level protocol message (HTTP).
+// Real applications should use react/http-client instead!
+//
+// This simple example only accepts an optional host parameter to send the
+// request to.
+//
+// $ php examples/01-http.php
+// $ php examples/01-http.php reactphp.org
+
+use React\EventLoop\Factory;
+use React\Stream\DuplexResourceStream;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
+
+// connect to tcp://www.google.com:80 (blocking call!)
+// for illustration purposes only, should use react/http-client or react/socket instead!
+$resource = stream_socket_client('tcp://' . $host . ':80');
+if (!$resource) {
+ exit(1);
+}
+
+$loop = Factory::create();
+$stream = new DuplexResourceStream($resource, $loop);
+
+$stream->on('data', function ($chunk) {
+ echo $chunk;
+});
+$stream->on('close', function () {
+ echo '[CLOSED]' . PHP_EOL;
+});
+
+$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
+
+$loop->run();
diff --git a/assets/php/vendor/react/stream/examples/02-https.php b/assets/php/vendor/react/stream/examples/02-https.php
new file mode 100644
index 0000000..163f7c8
--- /dev/null
+++ b/assets/php/vendor/react/stream/examples/02-https.php
@@ -0,0 +1,40 @@
+<?php
+
+// Simple secure HTTPS client example (for illustration purposes only).
+// This shows how a secure TLS connection is established to then send an
+// application level protocol message (HTTP).
+// Real applications should use react/http-client instead!
+//
+// This simple example only accepts an optional host parameter to send the
+// request to.
+//
+// $ php examples/02-https.php
+// $ php examples/02-https.php reactphp.org
+
+use React\EventLoop\Factory;
+use React\Stream\DuplexResourceStream;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
+
+// connect to tls://www.google.com:443 (blocking call!)
+// for illustration purposes only, should use react/http-client or react/socket instead!
+$resource = stream_socket_client('tls://' . $host . ':443');
+if (!$resource) {
+ exit(1);
+}
+
+$loop = Factory::create();
+$stream = new DuplexResourceStream($resource, $loop);
+
+$stream->on('data', function ($chunk) {
+ echo $chunk;
+});
+$stream->on('close', function () {
+ echo '[CLOSED]' . PHP_EOL;
+});
+
+$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
+
+$loop->run();
diff --git a/assets/php/vendor/react/stream/examples/11-cat.php b/assets/php/vendor/react/stream/examples/11-cat.php
new file mode 100644
index 0000000..90fadc0
--- /dev/null
+++ b/assets/php/vendor/react/stream/examples/11-cat.php
@@ -0,0 +1,28 @@
+<?php
+
+// Simple example piping everything from STDIN to STDOUT.
+// This allows you to output everything you type on your keyboard or to redirect
+// the pipes to show contents of files and other streams.
+//
+// $ php examples/11-cat.php
+// $ php examples/11-cat.php < README.md
+// $ echo hello | php examples/11-cat.php
+
+use React\EventLoop\Factory;
+use React\Stream\ReadableResourceStream;
+use React\Stream\WritableResourceStream;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+if (DIRECTORY_SEPARATOR === '\\') {
+ fwrite(STDERR, 'Non-blocking console I/O not supported on Microsoft Windows' . PHP_EOL);
+ exit(1);
+}
+
+$loop = Factory::create();
+
+$stdout = new WritableResourceStream(STDOUT, $loop);
+$stdin = new ReadableResourceStream(STDIN, $loop);
+$stdin->pipe($stdout);
+
+$loop->run();
diff --git a/assets/php/vendor/react/stream/examples/91-benchmark-throughput.php b/assets/php/vendor/react/stream/examples/91-benchmark-throughput.php
new file mode 100644
index 0000000..ecf695c
--- /dev/null
+++ b/assets/php/vendor/react/stream/examples/91-benchmark-throughput.php
@@ -0,0 +1,62 @@
+<?php
+
+// Benchmark to measure throughput performance piping an input stream to an output stream.
+// This allows you to get an idea of how fast stream processing with PHP can be
+// and also to play around with differnt types of input and output streams.
+//
+// This example accepts a number of parameters to control the timeout (-t 1),
+// the input file (-i /dev/zero) and the output file (-o /dev/null).
+//
+// $ php examples/91-benchmark-throughput.php
+// $ php examples/91-benchmark-throughput.php -t 10 -o zero.bin
+// $ php examples/91-benchmark-throughput.php -t 60 -i zero.bin
+
+require __DIR__ . '/../vendor/autoload.php';
+
+if (DIRECTORY_SEPARATOR === '\\') {
+ fwrite(STDERR, 'Non-blocking console I/O not supported on Microsoft Windows' . PHP_EOL);
+ exit(1);
+}
+
+$args = getopt('i:o:t:');
+$if = isset($args['i']) ? $args['i'] : '/dev/zero';
+$of = isset($args['o']) ? $args['o'] : '/dev/null';
+$t = isset($args['t']) ? $args['t'] : 1;
+
+// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
+$if = str_replace('/dev/fd/', 'php://fd/', $if);
+$of = str_replace('/dev/fd/', 'php://fd/', $of);
+
+$loop = new React\EventLoop\StreamSelectLoop();
+
+// setup information stream
+$info = new React\Stream\WritableResourceStream(STDERR, $loop);
+if (extension_loaded('xdebug')) {
+ $info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
+}
+$info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...'. PHP_EOL);
+
+// setup input and output streams and pipe inbetween
+$fh = fopen($if, 'r');
+$in = new React\Stream\ReadableResourceStream($fh, $loop);
+$out = new React\Stream\WritableResourceStream(fopen($of, 'w'), $loop);
+$in->pipe($out);
+
+// stop input stream in $t seconds
+$start = microtime(true);
+$timeout = $loop->addTimer($t, function () use ($in, &$bytes) {
+ $in->close();
+});
+
+// print stream position once stream closes
+$in->on('close', function () use ($fh, $start, $loop, $timeout, $info) {
+ $t = microtime(true) - $start;
+ $loop->cancelTimer($timeout);
+
+ $bytes = ftell($fh);
+
+ $info->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
+ $info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
+});
+
+$loop->run();