Skip to content

Commit 7c3465f

Browse files
committed
Add decoding benchmark
1 parent 95abcc2 commit 7c3465f

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

examples/91-benchmark-count.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
// simple usage:
4+
// $ php examples/91-benchmark-count.php < examples/users.csv
5+
//
6+
// getting reasonable results requires a large data set:
7+
// 1) download a large CSV data set, for example from https://github.com/fivethirtyeight/russian-troll-tweets
8+
// $ curl -OL https://github.com/fivethirtyeight/russian-troll-tweets/raw/master/IRAhandle_tweets_1.csv
9+
//
10+
// 2) pipe CSV into benchmark script:
11+
// $ php examples/91-benchmark-count.php < IRAhandle_tweets_1.csv
12+
13+
use Clue\React\Csv\AssocDecoder;
14+
use React\EventLoop\Factory;
15+
use React\Stream\ReadableResourceStream;
16+
17+
require __DIR__ . '/../vendor/autoload.php';
18+
19+
if (extension_loaded('xdebug')) {
20+
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
21+
}
22+
23+
$loop = Factory::create();
24+
$decoder = new AssocDecoder(new ReadableResourceStream(STDIN, $loop));
25+
26+
$count = 0;
27+
$decoder->on('data', function () use (&$count) {
28+
++$count;
29+
});
30+
31+
$start = microtime(true);
32+
$report = $loop->addPeriodicTimer(0.05, function () use (&$count, $start) {
33+
printf("\r%d records in %0.3fs...", $count, microtime(true) - $start);
34+
});
35+
36+
$decoder->on('close', function () use (&$count, $report, $loop, $start) {
37+
$now = microtime(true);
38+
$loop->cancelTimer($report);
39+
40+
printf("\r%d records in %0.3fs => %d records/s\n", $count, $now - $start, $count / ($now - $start));
41+
});
42+
43+
$loop->run();

0 commit comments

Comments
 (0)