Skip to content

Commit f5f8b42

Browse files
committed
Add benchmarking scripts to measure throughput in CI
1 parent f07bdb2 commit f5f8b42

4 files changed

Lines changed: 154 additions & 0 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ install:
1414

1515
script:
1616
- vendor/bin/phpunit --coverage-text
17+
- php examples/13-benchmark-throughput.php

examples/11-benchmark-read.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
// $ php examples/11-benchmark.php
4+
// $ php examples/11-benchmark.php echo test
5+
// $ php examples/11-benchmark.php dd if=/dev/zero bs=1M count=1000
6+
7+
use React\EventLoop\Factory;
8+
use React\ChildProcess\Process;
9+
10+
require __DIR__ . '/../vendor/autoload.php';
11+
12+
$cmd = isset($argv[1]) ? implode(' ', array_slice($argv, 1)) : 'dd if=/dev/zero bs=1M count=1000';
13+
14+
$loop = Factory::create();
15+
16+
$info = new React\Stream\Stream(STDERR, $loop);
17+
$info->pause();
18+
$info->write('Counts number of chunks/bytes received from process STDOUT' . PHP_EOL);
19+
$info->write('Command: ' . $cmd . PHP_EOL);
20+
if (extension_loaded('xdebug')) {
21+
$info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
22+
}
23+
24+
$process = new Process($cmd);
25+
$process->start($loop);
26+
$start = microtime(true);
27+
28+
$chunks = 0;
29+
$bytes = 0;
30+
$process->stdout->on('data', function ($chunk) use (&$chunks, &$bytes) {
31+
++$chunks;
32+
$bytes += strlen($chunk);
33+
});
34+
35+
// print stream position once stream closes
36+
$process->on('exit', function () use (&$chunks, &$bytes, $start, $info) {
37+
$t = microtime(true) - $start;
38+
39+
$info->write('read ' . $chunks . ' chunks with ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
40+
$info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
41+
});
42+
43+
// report any other output/errors
44+
$process->stdout->on('error', array($info, 'write'));
45+
$process->stderr->on('data', 'printf');
46+
$process->stdout->on('error', 'printf');
47+
48+
$loop->run();

examples/12-benchmark-write.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
use React\Stream\Stream;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$loop = Factory::create();
10+
11+
$info = new React\Stream\Stream(STDERR, $loop);
12+
$info->pause();
13+
$info->write('Pipes data to process STDIN' . PHP_EOL);
14+
if (extension_loaded('xdebug')) {
15+
$info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
16+
}
17+
18+
$process = new Process('dd of=/dev/zero');
19+
$process->start($loop);
20+
21+
// 10000 * 100 KB => 1 GB
22+
$i = 10000;
23+
$chunk = str_repeat("\0", 100 * 1000);
24+
$write = function () use ($chunk, $process, &$i, &$write) {
25+
do {
26+
--$i;
27+
$continue = $process->stdin->write($chunk);
28+
} while ($i && $continue);
29+
30+
if ($i > 0) {
31+
// buffer full => wait for drain to continue
32+
$process->stdin->once('drain', $write);
33+
} else {
34+
$process->stdin->end();
35+
}
36+
};
37+
$write();
38+
39+
// report any other output/errors
40+
$process->stdout->on('data', 'printf');
41+
$process->stdout->on('error', 'printf');
42+
$process->stderr->on('data', 'printf');
43+
$process->stdout->on('error', 'printf');
44+
45+
$loop->run();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
use React\Stream\Stream;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$loop = Factory::create();
10+
11+
$info = new React\Stream\Stream(STDERR, $loop);
12+
$info->pause();
13+
$info->write('Pipes data through process STDIN and reads STDOUT again' . PHP_EOL);
14+
if (extension_loaded('xdebug')) {
15+
$info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
16+
}
17+
18+
$process = new Process('cat');
19+
$process->start($loop);
20+
$start = microtime(true);
21+
22+
$chunks = 0;
23+
$bytes = 0;
24+
$process->stdout->on('data', function ($chunk) use (&$chunks, &$bytes) {
25+
++$chunks;
26+
$bytes += strlen($chunk);
27+
});
28+
29+
// 10000 * 100 KB => 1 GB
30+
$i = 10000;
31+
$chunk = str_repeat("\0", 100 * 1000);
32+
$write = function () use ($chunk, $process, &$i, &$write) {
33+
do {
34+
--$i;
35+
$continue = $process->stdin->write($chunk);
36+
} while ($i && $continue);
37+
38+
if ($i > 0) {
39+
// buffer full => wait for drain to continue
40+
$process->stdin->once('drain', $write);
41+
} else {
42+
$process->stdin->end();
43+
}
44+
};
45+
$write();
46+
47+
// print stream position once process exits
48+
$process->on('exit', function () use (&$chunks, &$bytes, $start, $info) {
49+
$t = microtime(true) - $start;
50+
51+
$info->write('read ' . $chunks . ' chunks with ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
52+
$info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
53+
});
54+
55+
// report any other output/errors
56+
$process->stdout->on('error', 'printf');
57+
$process->stderr->on('data', 'printf');
58+
$process->stdout->on('error', 'printf');
59+
60+
$loop->run();

0 commit comments

Comments
 (0)