forked from reactphp/stream
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark-throughput.php
More file actions
45 lines (34 loc) · 1.62 KB
/
benchmark-throughput.php
File metadata and controls
45 lines (34 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
require __DIR__ . '/../vendor/autoload.php';
$args = getopt('i:o:t:');
$if = isset($args['i']) ? $args['i'] : '/dev/zero';
$of = isset($args['o']) ? $args['o'] : '/dev/null';
$t = isset($args['t']) ? $args['t'] : 1;
// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
$if = str_replace('/dev/fd/', 'php://fd/', $if);
$of = str_replace('/dev/fd/', 'php://fd/', $of);
$loop = new React\EventLoop\StreamSelectLoop();
// setup information stream
$info = new React\Stream\WritableResourceStream(STDERR, $loop);
if (extension_loaded('xdebug')) {
$info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
}
$info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...'. PHP_EOL);
// setup input and output streams and pipe inbetween
$in = new React\Stream\ReadableResourceStream(fopen($if, 'r'), $loop);
$out = new React\Stream\WritableResourceStream(fopen($of, 'w'), $loop);
$in->pipe($out);
// stop input stream in $t seconds
$start = microtime(true);
$timeout = $loop->addTimer($t, function () use ($in, &$bytes) {
$in->close();
});
// print stream position once stream closes
$in->on('close', function () use ($in, $start, $timeout, $info) {
$t = microtime(true) - $start;
$timeout->cancel();
$bytes = ftell($in->stream);
$info->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
$info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
});
$loop->run();