Skip to content

Add decoding benchmark plus benchmark for GZIP-compressed CSV files #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"react/stream": "^1.0 || ^0.7 || ^0.6"
},
"require-dev": {
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.7 || ^4.8.35"
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.7 || ^4.8.35",
"react/child-process": "^0.6",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3"
}
}
43 changes: 43 additions & 0 deletions examples/91-benchmark-count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

// simple usage:
// $ php examples/91-benchmark-count.php < examples/users.csv
//
// getting reasonable results requires a large data set:
// 1) download a large CSV data set, for example from https://github.com/fivethirtyeight/russian-troll-tweets
// $ curl -OL https://github.com/fivethirtyeight/russian-troll-tweets/raw/master/IRAhandle_tweets_1.csv
//
// 2) pipe CSV into benchmark script:
// $ php examples/91-benchmark-count.php < IRAhandle_tweets_1.csv

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

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();
$decoder = new AssocDecoder(new ReadableResourceStream(STDIN, $loop));

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

$start = microtime(true);
$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) {
$now = microtime(true);
$loop->cancelTimer($report);

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

$loop->run();
55 changes: 55 additions & 0 deletions examples/92-benchmark-count-gzip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

// getting reasonable results requires a large data set:
// 1) download a large CSV data set, for example from https://github.com/fivethirtyeight/russian-troll-tweets
// $ curl -OL https://github.com/fivethirtyeight/russian-troll-tweets/raw/master/IRAhandle_tweets_1.csv
//
// 2) If your data set it not already in gzip format, compress it:
// $ gzip < IRAhandle_tweets_1.csv > IRAhandle_tweets_1.csv.gz
//
// 3) pipe compressed CSV into benchmark script:
// $ php examples/92-benchmark-count-gzip.php < IRAhandle_tweets_1.csv.gz

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

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)
// or if `gunzip` is not available (Windows), using a built-in decompressor
// such as https://github.com/clue/reactphp-zlib would be preferable.
$process = new Process('exec gunzip', null, null, array(
0 => STDIN,
1 => array('pipe', 'w'),
STDERR
));
$process->start($loop);
$decoder = new AssocDecoder($process->stdout);

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

$start = microtime(true);
$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) {
$now = microtime(true);
$loop->cancelTimer($report);

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

$loop->run();