Skip to content

Commit

Permalink
Merge pull request #24 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 committed Jul 23, 2021
2 parents bcbb58a + 7f62945 commit 6b22819
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 56 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ test,1,24
"hello world",2,48
```
```php
$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin = new ReadableResourceStream(STDIN);

$stream = new Decoder($stdin);

Expand Down Expand Up @@ -261,7 +261,7 @@ test,1
"hello world",2
```
```php
$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin = new ReadableResourceStream(STDIN);

$stream = new AssocDecoder($stdin);

Expand Down Expand Up @@ -304,7 +304,7 @@ and accepts its data through the same interface, but handles any data as complet
CSV elements instead of just chunks of strings:

```php
$stdout = new WritableResourceStream(STDOUT, $loop);
$stdout = new WritableResourceStream(STDOUT);

$stream = new Encoder($stdout);

Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
"email": "christian@clue.engineering"
}
],
"autoload": {
"psr-4": { "Clue\\React\\Csv\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\Csv\\": "tests/" }
},
"require": {
"php": ">=5.3",
"react/stream": "^1.0 || ^0.7 || ^0.6"
"react/stream": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/child-process": "^0.6",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3"
"react/child-process": "^0.6.3",
"react/event-loop": "^1.2"
},
"autoload": {
"psr-4": { "Clue\\React\\Csv\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\Csv\\": "tests/" }
}
}
10 changes: 4 additions & 6 deletions examples/01-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
// $ php examples/01-count.php < examples/users.csv

use Clue\React\Csv\AssocDecoder;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;

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

$loop = Factory::create();

$exit = 0;
$in = new ReadableResourceStream(STDIN, $loop);
$info = new WritableResourceStream(STDERR, $loop);
$in = new ReadableResourceStream(STDIN);
$info = new WritableResourceStream(STDERR);

$delimiter = isset($argv[1]) ? $argv[1] : ',';

Expand All @@ -37,6 +35,6 @@
$info->write('The resulting number of records (rows minus header row) will be printed to STDOUT' . PHP_EOL);
$info->write('Invalid CSV will raise an error on STDERR and exit with code 1' . PHP_EOL);

$loop->run();
Loop::run();

exit($exit);
12 changes: 5 additions & 7 deletions examples/02-validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@

use Clue\React\Csv\Decoder;
use Clue\React\Csv\Encoder;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;

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

$loop = Factory::create();

$exit = 0;
$in = new ReadableResourceStream(STDIN, $loop);
$out = new WritableResourceStream(STDOUT, $loop);
$info = new WritableResourceStream(STDERR, $loop);
$in = new ReadableResourceStream(STDIN);
$out = new WritableResourceStream(STDOUT);
$info = new WritableResourceStream(STDERR);

$delimiter = isset($argv[1]) ? $argv[1] : ',';

Expand All @@ -32,6 +30,6 @@
$info->write('Valid CSV will be forwarded to STDOUT' . PHP_EOL);
$info->write('Invalid CSV will raise an error on STDERR and exit with code 1' . PHP_EOL);

$loop->run();
Loop::run();

exit($exit);
12 changes: 5 additions & 7 deletions examples/11-csv2ndjson.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
// see also https://github.com/clue/reactphp-ndjson

use Clue\React\Csv\AssocDecoder;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;
use React\Stream\ThroughStream;

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

$loop = Factory::create();

$exit = 0;
$in = new ReadableResourceStream(STDIN, $loop);
$out = new WritableResourceStream(STDOUT, $loop);
$info = new WritableResourceStream(STDERR, $loop);
$in = new ReadableResourceStream(STDIN);
$out = new WritableResourceStream(STDOUT);
$info = new WritableResourceStream(STDERR);

$delimiter = isset($argv[1]) ? $argv[1] : ',';

Expand All @@ -41,6 +39,6 @@
$info->write('Valid NDJSON (Newline-Delimited JSON) will be forwarded to STDOUT' . PHP_EOL);
$info->write('Invalid CSV will raise an error on STDERR and exit with code 1' . PHP_EOL);

$loop->run();
Loop::run();

exit($exit);
12 changes: 5 additions & 7 deletions examples/12-csv2tsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
// see also https://github.com/clue/reactphp-tsv

use Clue\React\Csv\Decoder;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;
use React\Stream\ThroughStream;

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

$loop = Factory::create();

$exit = 0;
$in = new ReadableResourceStream(STDIN, $loop);
$out = new WritableResourceStream(STDOUT, $loop);
$info = new WritableResourceStream(STDERR, $loop);
$in = new ReadableResourceStream(STDIN);
$out = new WritableResourceStream(STDOUT);
$info = new WritableResourceStream(STDERR);

$delimiter = isset($argv[1]) ? $argv[1] : ',';

Expand Down Expand Up @@ -50,6 +48,6 @@
$info->write('Valid TSV (Tab-Separated Values) will be forwarded to STDOUT' . PHP_EOL);
$info->write('Invalid CSV will raise an error on STDERR and exit with code 1' . PHP_EOL);

$loop->run();
Loop::run();

exit($exit);
13 changes: 5 additions & 8 deletions examples/91-benchmark-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// $ php examples/91-benchmark-count.php < IRAhandle_tweets_1.csv

use Clue\React\Csv\AssocDecoder;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -20,24 +20,21 @@
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}

$loop = Factory::create();
$decoder = new AssocDecoder(new ReadableResourceStream(STDIN, $loop));
$decoder = new AssocDecoder(new ReadableResourceStream(STDIN));

$count = 0;
$decoder->on('data', function () use (&$count) {
++$count;
});

$start = microtime(true);
$report = $loop->addPeriodicTimer(0.05, function () use (&$count, $start) {
$report = Loop::addPeriodicTimer(0.05, function () use (&$count, $start) {
printf("\r%d records in %0.3fs...", $count, microtime(true) - $start);
});

$decoder->on('close', function () use (&$count, $report, $loop, $start) {
$decoder->on('close', function () use (&$count, $report, $start) {
$now = microtime(true);
$loop->cancelTimer($report);
Loop::cancelTimer($report);

printf("\r%d records in %0.3fs => %d records/s\n", $count, $now - $start, $count / ($now - $start));
});

$loop->run();
14 changes: 5 additions & 9 deletions examples/92-benchmark-count-gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@

use Clue\React\Csv\AssocDecoder;
use React\ChildProcess\Process;
use React\EventLoop\Factory;
use React\EventLoop\Loop;

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

if (extension_loaded('xdebug')) {
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}

$loop = Factory::create();

// This benchmark example spawns the decompressor in a child `gunzip` process
// because parsing CSV files is already mostly CPU-bound and multi-processing
// is preferred here. If the input source is slower (such as an HTTP download)
Expand All @@ -32,7 +30,7 @@
1 => array('pipe', 'w'),
STDERR
));
$process->start($loop);
$process->start();
$decoder = new AssocDecoder($process->stdout);

$count = 0;
Expand All @@ -41,15 +39,13 @@
});

$start = microtime(true);
$report = $loop->addPeriodicTimer(0.05, function () use (&$count, $start) {
$report = Loop::addPeriodicTimer(0.05, function () use (&$count, $start) {
printf("\r%d records in %0.3fs...", $count, microtime(true) - $start);
});

$decoder->on('close', function () use (&$count, $report, $loop, $start) {
$decoder->on('close', function () use (&$count, $report, $start) {
$now = microtime(true);
$loop->cancelTimer($report);
Loop::cancelTimer($report);

printf("\r%d records in %0.3fs => %d records/s\n", $count, $now - $start, $count / ($now - $start));
});

$loop->run();

0 comments on commit 6b22819

Please sign in to comment.