Skip to content

Commit

Permalink
Merge pull request #34 from clue-labs/default-loop
Browse files Browse the repository at this point in the history
Simplify examples by updating to new default loop
  • Loading branch information
clue authored Jul 14, 2021
2 parents b2b7519 + 848be49 commit 3c12eaf
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 37 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,14 @@ gzip file stream into an decompressor which emits decompressed data events for
each individual log file chunk:

```php
$loop = React\EventLoop\Factory::create();
$stream = new React\Stream\ReadableResourceStream(fopen('access.log.gz', 'r'), $loop);
$stream = new React\Stream\ReadableResourceStream(fopen('access.log.gz', 'r'));

$decompressor = new Clue\React\Zlib\Decompressor(ZLIB_ENCODING_GZIP);
$stream->pipe($decompressor);

$decompressor->on('data', function ($data) {
echo $data; // chunk of decompressed log data
});

$loop->run();
```

See also the [examples](examples).
Expand Down
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
"require": {
"php": ">=7.0",
"ext-zlib": "*",
"react/stream": "^1.0 || ^0.7 || ^0.6"
"react/stream": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^9.3 || ^6.5",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3"
},
"suggest": {
"ext-zlib": "Requires ext-zlib extension"
"react/event-loop": "^1.2"
},
"autoload": {
"psr-4": { "Clue\\React\\Zlib\\": "src/" }
Expand Down
14 changes: 6 additions & 8 deletions examples/91-benchmark-compress.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//
// $ dd if=/dev/zero bs=1M count=1k status=progress | gzip > /dev/null

use React\EventLoop\Loop;

require __DIR__ . '/../vendor/autoload.php';

if (DIRECTORY_SEPARATOR === '\\') {
Expand All @@ -26,11 +28,9 @@
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}

$loop = React\EventLoop\Factory::create();

// read 1 MiB * 1 Ki times
$count = 0;
$stream = new React\Stream\ReadableResourceStream(fopen('/dev/zero', 'r'), $loop, 1024*1024);
$stream = new React\Stream\ReadableResourceStream(fopen('/dev/zero', 'r'), null, 1024*1024);
$stream->on('data', function () use (&$count, $stream) {
if (++$count > 1024) {
$stream->close();
Expand All @@ -47,17 +47,15 @@
});

// report progress periodically
$timer = $loop->addPeriodicTimer(0.05, function () use (&$bytes) {
$timer = Loop::addPeriodicTimer(0.05, function () use (&$bytes) {
echo "\rCompressed $bytes bytes…";
});

// report results once the stream closes
$start = microtime(true);
$stream->on('close', function () use (&$bytes, $start, $loop, $timer) {
$stream->on('close', function () use (&$bytes, $start, $timer) {
$time = microtime(true) - $start;
$loop->cancelTimer($timer);
Loop::cancelTimer($timer);

echo "\rCompressed $bytes bytes in " . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});

$loop->run();
14 changes: 6 additions & 8 deletions examples/92-benchmark-decompress.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
//
// $ php examples/gunzip.php < null.gz | dd of=/dev/zero status=progress

use React\EventLoop\Loop;

require __DIR__ . '/../vendor/autoload.php';

if (DIRECTORY_SEPARATOR === '\\') {
Expand All @@ -35,9 +37,7 @@
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}

$loop = React\EventLoop\Factory::create();

$in = new React\Stream\ReadableResourceStream(fopen($argv[1], 'r'), $loop);
$in = new React\Stream\ReadableResourceStream(fopen($argv[1], 'r'));
$stream = new Clue\React\Zlib\Decompressor(ZLIB_ENCODING_GZIP);
$in->pipe($stream);

Expand All @@ -49,17 +49,15 @@
$stream->on('error', 'printf');

//report progress periodically
$timer = $loop->addPeriodicTimer(0.2, function () use (&$bytes) {
$timer = Loop::addPeriodicTimer(0.2, function () use (&$bytes) {
echo "\rDecompressed $bytes bytes…";
});

// show stats when stream ends
$start = microtime(true);
$stream->on('close', function () use (&$bytes, $start, $loop, $timer) {
$stream->on('close', function () use (&$bytes, $start, $timer) {
$time = microtime(true) - $start;
$loop->cancelTimer($timer);
Loop::cancelTimer($timer);

echo "\rDecompressed $bytes bytes in " . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});

$loop->run();
8 changes: 2 additions & 6 deletions examples/gunzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
exit(1);
}

$loop = React\EventLoop\Factory::create();

$in = new React\Stream\ReadableResourceStream(STDIN, $loop);
$out = new React\Stream\WritableResourceStream(STDOUT, $loop);
$in = new React\Stream\ReadableResourceStream(STDIN);
$out = new React\Stream\WritableResourceStream(STDOUT);

$decompressor = new Clue\React\Zlib\Decompressor(ZLIB_ENCODING_GZIP);
$in->pipe($decompressor)->pipe($out);

$decompressor->on('error', function ($e) {
fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL);
});

$loop->run();
8 changes: 2 additions & 6 deletions examples/gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
exit(1);
}

$loop = React\EventLoop\Factory::create();

$in = new React\Stream\ReadableResourceStream(STDIN, $loop);
$out = new React\Stream\WritableResourceStream(STDOUT, $loop);
$in = new React\Stream\ReadableResourceStream(STDIN);
$out = new React\Stream\WritableResourceStream(STDOUT);

$compressor = new Clue\React\Zlib\Compressor(ZLIB_ENCODING_GZIP);
$in->pipe($compressor)->pipe($out);

$loop->run();

0 comments on commit 3c12eaf

Please sign in to comment.