aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/php/vendor/react/event-loop/examples
diff options
context:
space:
mode:
Diffstat (limited to 'assets/php/vendor/react/event-loop/examples')
-rw-r--r--assets/php/vendor/react/event-loop/examples/01-timers.php15
-rw-r--r--assets/php/vendor/react/event-loop/examples/02-periodic.php16
-rw-r--r--assets/php/vendor/react/event-loop/examples/03-ticks.php15
-rw-r--r--assets/php/vendor/react/event-loop/examples/04-signals.php19
-rw-r--r--assets/php/vendor/react/event-loop/examples/11-consume-stdin.php30
-rw-r--r--assets/php/vendor/react/event-loop/examples/12-generate-yes.php41
-rw-r--r--assets/php/vendor/react/event-loop/examples/13-http-client-blocking.php35
-rw-r--r--assets/php/vendor/react/event-loop/examples/14-http-client-async.php63
-rw-r--r--assets/php/vendor/react/event-loop/examples/21-http-server.php36
-rw-r--r--assets/php/vendor/react/event-loop/examples/91-benchmark-ticks.php15
-rw-r--r--assets/php/vendor/react/event-loop/examples/92-benchmark-timers.php15
-rw-r--r--assets/php/vendor/react/event-loop/examples/93-benchmark-ticks-delay.php22
-rw-r--r--assets/php/vendor/react/event-loop/examples/94-benchmark-timers-delay.php22
-rw-r--r--assets/php/vendor/react/event-loop/examples/95-benchmark-memory.php67
14 files changed, 411 insertions, 0 deletions
diff --git a/assets/php/vendor/react/event-loop/examples/01-timers.php b/assets/php/vendor/react/event-loop/examples/01-timers.php
new file mode 100644
index 0000000..e6107e4
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/01-timers.php
@@ -0,0 +1,15 @@
+<?php
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = React\EventLoop\Factory::create();
+
+$loop->addTimer(0.8, function () {
+ echo 'world!' . PHP_EOL;
+});
+
+$loop->addTimer(0.3, function () {
+ echo 'hello ';
+});
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/02-periodic.php b/assets/php/vendor/react/event-loop/examples/02-periodic.php
new file mode 100644
index 0000000..5e138a6
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/02-periodic.php
@@ -0,0 +1,16 @@
+<?php
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = React\EventLoop\Factory::create();
+
+$timer = $loop->addPeriodicTimer(0.1, function () {
+ echo 'tick!' . PHP_EOL;
+});
+
+$loop->addTimer(1.0, function () use ($loop, $timer) {
+ $loop->cancelTimer($timer);
+ echo 'Done' . PHP_EOL;
+});
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/03-ticks.php b/assets/php/vendor/react/event-loop/examples/03-ticks.php
new file mode 100644
index 0000000..3f36c6d
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/03-ticks.php
@@ -0,0 +1,15 @@
+<?php
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = React\EventLoop\Factory::create();
+
+$loop->futureTick(function () {
+ echo 'b';
+});
+$loop->futureTick(function () {
+ echo 'c';
+});
+echo 'a';
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/04-signals.php b/assets/php/vendor/react/event-loop/examples/04-signals.php
new file mode 100644
index 0000000..90b6898
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/04-signals.php
@@ -0,0 +1,19 @@
+<?php
+
+require __DIR__ . '/../vendor/autoload.php';
+
+if (!defined('SIGINT')) {
+ fwrite(STDERR, 'Not supported on your platform (ext-pcntl missing or Windows?)' . PHP_EOL);
+ exit(1);
+}
+
+$loop = React\EventLoop\Factory::create();
+
+$loop->addSignal(SIGINT, $func = function ($signal) use ($loop, &$func) {
+ echo 'Signal: ', (string)$signal, PHP_EOL;
+ $loop->removeSignal(SIGINT, $func);
+});
+
+echo 'Listening for SIGINT. Use "kill -SIGINT ' . getmypid() . '" or CTRL+C' . PHP_EOL;
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/11-consume-stdin.php b/assets/php/vendor/react/event-loop/examples/11-consume-stdin.php
new file mode 100644
index 0000000..2a77245
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/11-consume-stdin.php
@@ -0,0 +1,30 @@
+<?php
+
+use React\EventLoop\Factory;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+if (!defined('STDIN') || stream_set_blocking(STDIN, false) !== true) {
+ fwrite(STDERR, 'ERROR: Unable to set STDIN non-blocking (not CLI or Windows?)' . PHP_EOL);
+ exit(1);
+}
+
+$loop = Factory::create();
+
+// read everything from STDIN and report number of bytes
+// for illustration purposes only, should use react/stream instead
+$loop->addReadStream(STDIN, function ($stream) use ($loop) {
+ $chunk = fread($stream, 64 * 1024);
+
+ // reading nothing means we reached EOF
+ if ($chunk === '') {
+ $loop->removeReadStream($stream);
+ stream_set_blocking($stream, true);
+ fclose($stream);
+ return;
+ }
+
+ echo strlen($chunk) . ' bytes' . PHP_EOL;
+});
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/12-generate-yes.php b/assets/php/vendor/react/event-loop/examples/12-generate-yes.php
new file mode 100644
index 0000000..ebc2beb
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/12-generate-yes.php
@@ -0,0 +1,41 @@
+<?php
+
+require __DIR__ . '/../vendor/autoload.php';
+
+// data can be given as first argument or defaults to "y"
+$data = (isset($argv[1]) ? $argv[1] : 'y') . "\n";
+
+// repeat data X times in order to fill around 200 KB
+$data = str_repeat($data, round(200000 / strlen($data)));
+
+$loop = React\EventLoop\Factory::create();
+
+if (!defined('STDOUT') || stream_set_blocking(STDOUT, false) !== true) {
+ fwrite(STDERR, 'ERROR: Unable to set STDOUT non-blocking (not CLI or Windows?)' . PHP_EOL);
+ exit(1);
+}
+
+// write data to STDOUT whenever its write buffer accepts data
+// for illustrations purpose only, should use react/stream instead
+$loop->addWriteStream(STDOUT, function ($stdout) use ($loop, &$data) {
+ // try to write data
+ $r = fwrite($stdout, $data);
+
+ // nothing could be written despite being writable => closed
+ if ($r === 0) {
+ $loop->removeWriteStream($stdout);
+ fclose($stdout);
+ stream_set_blocking($stdout, true);
+ fwrite(STDERR, 'Stopped because STDOUT closed' . PHP_EOL);
+
+ return;
+ }
+
+ // implement a very simple ring buffer, unless everything has been written at once:
+ // everything written in this iteration will be appended for next iteration
+ if (isset($data[$r])) {
+ $data = substr($data, $r) . substr($data, 0, $r);
+ }
+});
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/13-http-client-blocking.php b/assets/php/vendor/react/event-loop/examples/13-http-client-blocking.php
new file mode 100644
index 0000000..a2dde55
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/13-http-client-blocking.php
@@ -0,0 +1,35 @@
+<?php
+
+use React\EventLoop\Factory;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = Factory::create();
+
+// connect to www.google.com:80 (blocking call!)
+// for illustration purposes only, should use react/socket instead
+$stream = stream_socket_client('tcp://www.google.com:80');
+if (!$stream) {
+ exit(1);
+}
+stream_set_blocking($stream, false);
+
+// send HTTP request
+fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
+
+// wait for HTTP response
+$loop->addReadStream($stream, function ($stream) use ($loop) {
+ $chunk = fread($stream, 64 * 1024);
+
+ // reading nothing means we reached EOF
+ if ($chunk === '') {
+ echo '[END]' . PHP_EOL;
+ $loop->removeReadStream($stream);
+ fclose($stream);
+ return;
+ }
+
+ echo $chunk;
+});
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/14-http-client-async.php b/assets/php/vendor/react/event-loop/examples/14-http-client-async.php
new file mode 100644
index 0000000..c82c988
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/14-http-client-async.php
@@ -0,0 +1,63 @@
+<?php
+
+use React\EventLoop\Factory;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = Factory::create();
+
+// resolve hostname before establishing TCP/IP connection (resolving DNS is still blocking here)
+// for illustration purposes only, should use react/socket or react/dns instead!
+$ip = gethostbyname('www.google.com');
+if (ip2long($ip) === false) {
+ echo 'Unable to resolve hostname' . PHP_EOL;
+ exit(1);
+}
+
+// establish TCP/IP connection (non-blocking)
+// for illustraction purposes only, should use react/socket instead!
+$stream = stream_socket_client('tcp://' . $ip . ':80', $errno, $errstr, null, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
+if (!$stream) {
+ exit(1);
+}
+stream_set_blocking($stream, false);
+
+// print progress every 10ms
+echo 'Connecting';
+$timer = $loop->addPeriodicTimer(0.01, function () {
+ echo '.';
+});
+
+// wait for connection success/error
+$loop->addWriteStream($stream, function ($stream) use ($loop, $timer) {
+ $loop->removeWriteStream($stream);
+ $loop->cancelTimer($timer);
+
+ // check for socket error (connection rejected)
+ if (stream_socket_get_name($stream, true) === false) {
+ echo '[unable to connect]' . PHP_EOL;
+ exit(1);
+ } else {
+ echo '[connected]' . PHP_EOL;
+ }
+
+ // send HTTP request
+ fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
+
+ // wait for HTTP response
+ $loop->addReadStream($stream, function ($stream) use ($loop) {
+ $chunk = fread($stream, 64 * 1024);
+
+ // reading nothing means we reached EOF
+ if ($chunk === '') {
+ echo '[END]' . PHP_EOL;
+ $loop->removeReadStream($stream);
+ fclose($stream);
+ return;
+ }
+
+ echo $chunk;
+ });
+});
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/21-http-server.php b/assets/php/vendor/react/event-loop/examples/21-http-server.php
new file mode 100644
index 0000000..89520ce
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/21-http-server.php
@@ -0,0 +1,36 @@
+<?php
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = React\EventLoop\Factory::create();
+
+// start TCP/IP server on localhost:8080
+// for illustration purposes only, should use react/socket instead
+$server = stream_socket_server('tcp://127.0.0.1:8080');
+if (!$server) {
+ exit(1);
+}
+stream_set_blocking($server, false);
+
+// wait for incoming connections on server socket
+$loop->addReadStream($server, function ($server) use ($loop) {
+ $conn = stream_socket_accept($server);
+ $data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
+ $loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
+ $written = fwrite($conn, $data);
+ if ($written === strlen($data)) {
+ fclose($conn);
+ $loop->removeWriteStream($conn);
+ } else {
+ $data = substr($data, $written);
+ }
+ });
+});
+
+$loop->addPeriodicTimer(5, function () {
+ $memory = memory_get_usage() / 1024;
+ $formatted = number_format($memory, 3).'K';
+ echo "Current memory usage: {$formatted}\n";
+});
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/91-benchmark-ticks.php b/assets/php/vendor/react/event-loop/examples/91-benchmark-ticks.php
new file mode 100644
index 0000000..3f4690b
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/91-benchmark-ticks.php
@@ -0,0 +1,15 @@
+<?php
+
+use React\EventLoop\Factory;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = Factory::create();
+
+$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
+
+for ($i = 0; $i < $n; ++$i) {
+ $loop->futureTick(function () { });
+}
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/92-benchmark-timers.php b/assets/php/vendor/react/event-loop/examples/92-benchmark-timers.php
new file mode 100644
index 0000000..e2e02e4
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/92-benchmark-timers.php
@@ -0,0 +1,15 @@
+<?php
+
+use React\EventLoop\Factory;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = Factory::create();
+
+$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
+
+for ($i = 0; $i < $n; ++$i) {
+ $loop->addTimer(0, function () { });
+}
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/93-benchmark-ticks-delay.php b/assets/php/vendor/react/event-loop/examples/93-benchmark-ticks-delay.php
new file mode 100644
index 0000000..95ee78c
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/93-benchmark-ticks-delay.php
@@ -0,0 +1,22 @@
+<?php
+
+use React\EventLoop\Factory;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = Factory::create();
+
+$ticks = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
+$tick = function () use (&$tick, &$ticks, $loop) {
+ if ($ticks > 0) {
+ --$ticks;
+ //$loop->addTimer(0, $tick);
+ $loop->futureTick($tick);
+ } else {
+ echo 'done';
+ }
+};
+
+$tick();
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/94-benchmark-timers-delay.php b/assets/php/vendor/react/event-loop/examples/94-benchmark-timers-delay.php
new file mode 100644
index 0000000..2d6cfa2
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/94-benchmark-timers-delay.php
@@ -0,0 +1,22 @@
+<?php
+
+use React\EventLoop\Factory;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$loop = Factory::create();
+
+$ticks = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
+$tick = function () use (&$tick, &$ticks, $loop) {
+ if ($ticks > 0) {
+ --$ticks;
+ //$loop->futureTick($tick);
+ $loop->addTimer(0, $tick);
+ } else {
+ echo 'done';
+ }
+};
+
+$tick();
+
+$loop->run();
diff --git a/assets/php/vendor/react/event-loop/examples/95-benchmark-memory.php b/assets/php/vendor/react/event-loop/examples/95-benchmark-memory.php
new file mode 100644
index 0000000..084c404
--- /dev/null
+++ b/assets/php/vendor/react/event-loop/examples/95-benchmark-memory.php
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * Run the script indefinitely seconds with the loop from the factory and report every 2 seconds:
+ * php 95-benchmark-memory.php
+ * Run the script for 30 seconds with the stream_select loop and report every 10 seconds:
+ * php 95-benchmark-memory.php -t 30 -l StreamSelect -r 10
+ */
+
+use React\EventLoop\Factory;
+use React\EventLoop\LoopInterface;
+use React\EventLoop\TimerInterface;
+
+require __DIR__ . '/../vendor/autoload.php';
+
+$args = getopt('t:l:r:');
+$t = isset($args['t']) ? (int)$args['t'] : 0;
+$loop = isset($args['l']) && class_exists('React\EventLoop\\' . $args['l'] . 'Loop') ? 'React\EventLoop\\' . $args['l'] . 'Loop' : Factory::create();
+
+if (!($loop instanceof LoopInterface)) {
+ $loop = new $loop();
+}
+
+$r = isset($args['r']) ? (int)$args['r'] : 2;
+
+$runs = 0;
+
+if (5 < $t) {
+ $loop->addTimer($t, function () use ($loop) {
+ $loop->stop();
+ });
+
+}
+
+$loop->addPeriodicTimer(0.001, function () use (&$runs, $loop) {
+ $runs++;
+
+ $loop->addPeriodicTimer(1, function (TimerInterface $timer) use ($loop) {
+ $loop->cancelTimer($timer);
+ });
+});
+
+$loop->addPeriodicTimer($r, function () use (&$runs) {
+ $kmem = round(memory_get_usage() / 1024);
+ $kmemReal = round(memory_get_usage(true) / 1024);
+ echo "Runs:\t\t\t$runs\n";
+ echo "Memory (internal):\t$kmem KiB\n";
+ echo "Memory (real):\t\t$kmemReal KiB\n";
+ echo str_repeat('-', 50), "\n";
+});
+
+echo "PHP Version:\t\t", phpversion(), "\n";
+echo "Loop\t\t\t", get_class($loop), "\n";
+echo "Time\t\t\t", date('r'), "\n";
+
+echo str_repeat('-', 50), "\n";
+
+$beginTime = time();
+$loop->run();
+$endTime = time();
+$timeTaken = $endTime - $beginTime;
+
+echo "PHP Version:\t\t", phpversion(), "\n";
+echo "Loop\t\t\t", get_class($loop), "\n";
+echo "Time\t\t\t", date('r'), "\n";
+echo "Time taken\t\t", $timeTaken, " seconds\n";
+echo "Runs per second\t\t", round($runs / $timeTaken), "\n";