Skip to content

Commit 9b06ecc

Browse files
authored
Merge pull request #33 from clue-labs/examples
Add examples and benchmarking scripts
2 parents a73ab0d + f5f8b42 commit 9b06ecc

8 files changed

Lines changed: 257 additions & 21 deletions

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ install:
1616

1717
script:
1818
- vendor/bin/phpunit --coverage-text
19+
- php examples/13-benchmark-throughput.php

README.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,39 @@ as [Streams](https://github.com/reactphp/stream).
1313

1414
**Table of contents**
1515

16+
* [Quickstart example](#quickstart-example)
1617
* [Processes](#processes)
1718
* [EventEmitter Events](#eventemitter-events)
1819
* [Methods](#methods)
1920
* [Stream Properties](#stream-properties)
20-
* [Usage](#usage)
2121
* [Prepending Commands with `exec`](#prepending-commands-with-exec)
2222
* [Sigchild Compatibility](#sigchild-compatibility)
2323
* [Command Chaining](#command-chaining)
2424
* [Install](#install)
2525
* [Tests](#tests)
2626
* [License](#license)
2727

28+
## Quickstart example
29+
30+
```php
31+
$loop = React\EventLoop\Factory::create();
32+
33+
$process = new React\ChildProcess\Process('echo foo');
34+
$process->start($loop);
35+
36+
$process->stdout->on('data', function ($chunk) {
37+
echo $chunk;
38+
});
39+
40+
$process->on('exit', function($exitCode, $termSignal) {
41+
echo 'Process exited with code ' . $exitCode . PHP_EOL;
42+
});
43+
44+
$loop->run();
45+
```
46+
47+
See also the [examples](examples).
48+
2849
## Processes
2950

3051
### EventEmitter Events
@@ -81,26 +102,6 @@ $process->stdin->close();
81102
For more details, see the
82103
[`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).
83104

84-
## Usage
85-
```php
86-
$loop = React\EventLoop\Factory::create();
87-
88-
$process = new React\ChildProcess\Process('echo foo');
89-
90-
$process->on('exit', function($exitCode, $termSignal) {
91-
// ...
92-
});
93-
94-
$loop->addTimer(0.001, function($timer) use ($process) {
95-
$process->start($timer->getLoop());
96-
97-
$process->stdout->on('data', function($output) {
98-
// ...
99-
});
100-
});
101-
102-
$loop->run();
103-
```
104105
### Prepending Commands with `exec`
105106

106107
Symfony pull request [#5759](https://github.com/symfony/symfony/issues/5759)

examples/01-stdio.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$loop = Factory::create();
9+
10+
$process = new Process('cat');
11+
$process->start($loop);
12+
13+
$process->stdout->on('data', function ($chunk) {
14+
echo $chunk;
15+
});
16+
17+
$process->on('exit', function ($code) {
18+
echo 'EXIT with code ' . $code . PHP_EOL;
19+
});
20+
21+
// periodically send something to stream
22+
$periodic = $loop->addPeriodicTimer(0.2, function () use ($process) {
23+
$process->stdin->write('hello');
24+
});
25+
26+
// stop sending after a few seconds
27+
$loop->addTimer(2.0, function () use ($periodic, $loop, $process) {
28+
$loop->cancelTimer($periodic);
29+
$process->stdin->end();
30+
});
31+
32+
$loop->run();

examples/02-race.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$loop = Factory::create();
9+
10+
$first = new Process('sleep 2; echo welt');
11+
$first->start($loop);
12+
13+
$second = new Process('sleep 1; echo hallo');
14+
$second->start($loop);
15+
16+
$first->stdout->on('data', function ($chunk) {
17+
echo $chunk;
18+
});
19+
20+
$second->stdout->on('data', function ($chunk) {
21+
echo $chunk;
22+
});
23+
24+
$loop->run();

examples/03-stdout-stderr.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$loop = Factory::create();
9+
10+
$process = new Process('echo hallo;sleep 1;echo welt >&2;sleep 1;echo error;sleep 1;nope');
11+
$process->start($loop);
12+
13+
$process->stdout->on('data', function ($chunk) {
14+
echo '(' . $chunk . ')';
15+
});
16+
17+
$process->stderr->on('data', function ($chunk) {
18+
echo '[' . $chunk . ']';
19+
});
20+
21+
$process->on('exit', function ($code) {
22+
echo 'EXIT with code ' . $code . PHP_EOL;
23+
});
24+
25+
$loop->run();

examples/11-benchmark-read.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
// $ php examples/11-benchmark.php
4+
// $ php examples/11-benchmark.php echo test
5+
// $ php examples/11-benchmark.php dd if=/dev/zero bs=1M count=1000
6+
7+
use React\EventLoop\Factory;
8+
use React\ChildProcess\Process;
9+
10+
require __DIR__ . '/../vendor/autoload.php';
11+
12+
$cmd = isset($argv[1]) ? implode(' ', array_slice($argv, 1)) : 'dd if=/dev/zero bs=1M count=1000';
13+
14+
$loop = Factory::create();
15+
16+
$info = new React\Stream\Stream(STDERR, $loop);
17+
$info->pause();
18+
$info->write('Counts number of chunks/bytes received from process STDOUT' . PHP_EOL);
19+
$info->write('Command: ' . $cmd . PHP_EOL);
20+
if (extension_loaded('xdebug')) {
21+
$info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
22+
}
23+
24+
$process = new Process($cmd);
25+
$process->start($loop);
26+
$start = microtime(true);
27+
28+
$chunks = 0;
29+
$bytes = 0;
30+
$process->stdout->on('data', function ($chunk) use (&$chunks, &$bytes) {
31+
++$chunks;
32+
$bytes += strlen($chunk);
33+
});
34+
35+
// print stream position once stream closes
36+
$process->on('exit', function () use (&$chunks, &$bytes, $start, $info) {
37+
$t = microtime(true) - $start;
38+
39+
$info->write('read ' . $chunks . ' chunks with ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
40+
$info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
41+
});
42+
43+
// report any other output/errors
44+
$process->stdout->on('error', array($info, 'write'));
45+
$process->stderr->on('data', 'printf');
46+
$process->stdout->on('error', 'printf');
47+
48+
$loop->run();

examples/12-benchmark-write.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
use React\Stream\Stream;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$loop = Factory::create();
10+
11+
$info = new React\Stream\Stream(STDERR, $loop);
12+
$info->pause();
13+
$info->write('Pipes data to process STDIN' . PHP_EOL);
14+
if (extension_loaded('xdebug')) {
15+
$info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
16+
}
17+
18+
$process = new Process('dd of=/dev/zero');
19+
$process->start($loop);
20+
21+
// 10000 * 100 KB => 1 GB
22+
$i = 10000;
23+
$chunk = str_repeat("\0", 100 * 1000);
24+
$write = function () use ($chunk, $process, &$i, &$write) {
25+
do {
26+
--$i;
27+
$continue = $process->stdin->write($chunk);
28+
} while ($i && $continue);
29+
30+
if ($i > 0) {
31+
// buffer full => wait for drain to continue
32+
$process->stdin->once('drain', $write);
33+
} else {
34+
$process->stdin->end();
35+
}
36+
};
37+
$write();
38+
39+
// report any other output/errors
40+
$process->stdout->on('data', 'printf');
41+
$process->stdout->on('error', 'printf');
42+
$process->stderr->on('data', 'printf');
43+
$process->stdout->on('error', 'printf');
44+
45+
$loop->run();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
use React\Stream\Stream;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$loop = Factory::create();
10+
11+
$info = new React\Stream\Stream(STDERR, $loop);
12+
$info->pause();
13+
$info->write('Pipes data through process STDIN and reads STDOUT again' . PHP_EOL);
14+
if (extension_loaded('xdebug')) {
15+
$info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
16+
}
17+
18+
$process = new Process('cat');
19+
$process->start($loop);
20+
$start = microtime(true);
21+
22+
$chunks = 0;
23+
$bytes = 0;
24+
$process->stdout->on('data', function ($chunk) use (&$chunks, &$bytes) {
25+
++$chunks;
26+
$bytes += strlen($chunk);
27+
});
28+
29+
// 10000 * 100 KB => 1 GB
30+
$i = 10000;
31+
$chunk = str_repeat("\0", 100 * 1000);
32+
$write = function () use ($chunk, $process, &$i, &$write) {
33+
do {
34+
--$i;
35+
$continue = $process->stdin->write($chunk);
36+
} while ($i && $continue);
37+
38+
if ($i > 0) {
39+
// buffer full => wait for drain to continue
40+
$process->stdin->once('drain', $write);
41+
} else {
42+
$process->stdin->end();
43+
}
44+
};
45+
$write();
46+
47+
// print stream position once process exits
48+
$process->on('exit', function () use (&$chunks, &$bytes, $start, $info) {
49+
$t = microtime(true) - $start;
50+
51+
$info->write('read ' . $chunks . ' chunks with ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
52+
$info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
53+
});
54+
55+
// report any other output/errors
56+
$process->stdout->on('error', 'printf');
57+
$process->stderr->on('data', 'printf');
58+
$process->stdout->on('error', 'printf');
59+
60+
$loop->run();

0 commit comments

Comments
 (0)