|
11 | 11 | // $ php examples/92-benchmark-count-gzip.php < IRAhandle_tweets_1.csv.gz |
12 | 12 |
|
13 | 13 | use Clue\React\Csv\AssocDecoder; |
14 | | -use Clue\React\Zlib\Decompressor; |
| 14 | +use React\ChildProcess\Process; |
15 | 15 | use React\EventLoop\Factory; |
16 | | -use React\Stream\ReadableResourceStream; |
17 | 16 |
|
18 | 17 | require __DIR__ . '/../vendor/autoload.php'; |
19 | 18 |
|
|
22 | 21 | } |
23 | 22 |
|
24 | 23 | $loop = Factory::create(); |
25 | | -$input = new ReadableResourceStream(STDIN, $loop); |
26 | | -$decompressor = new Decompressor(ZLIB_ENCODING_GZIP); |
27 | | -$input->pipe($decompressor); |
28 | | -$decoder = new AssocDecoder($decompressor); |
29 | 24 |
|
30 | | -$decompressor->on('error', function (Exception $e) { |
31 | | - printf("\nDecompression error: " . $e->getMessage() . "\n"); |
32 | | -}); |
| 25 | +// This benchmark example spawns the decompressor in a child `gunzip` process |
| 26 | +// because parsing CSV files is already mostly CPU-bound and multi-processing |
| 27 | +// is preferred here. If the input source is slower (such as an HTTP download) |
| 28 | +// or if `gunzip` is not available (Windows), using a built-in decompressor |
| 29 | +// such as https://github.com/clue/reactphp-zlib would be preferable. |
| 30 | +$process = new Process('exec gunzip', null, null, array( |
| 31 | + 0 => STDIN, |
| 32 | + 1 => array('pipe', 'w'), |
| 33 | + STDERR |
| 34 | +)); |
| 35 | +$process->start($loop); |
| 36 | +$decoder = new AssocDecoder($process->stdout); |
33 | 37 |
|
34 | 38 | $count = 0; |
35 | 39 | $decoder->on('data', function () use (&$count) { |
|
0 commit comments