-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
benchmark.php
64 lines (46 loc) 路 1.87 KB
/
benchmark.php
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace Psl\Example\IO;
use Psl\Async;
use Psl\DateTime;
use Psl\IO;
use Psl\Math;
use Psl\Regex;
use function fopen;
use function getopt;
use function memory_get_peak_usage;
use const PHP_OS_FAMILY;
require __DIR__ . '/../../vendor/autoload.php';
Async\main(static function (): int {
if (PHP_OS_FAMILY === 'Windows') {
IO\write_error_line('This example does not support Windows.');
return 0;
}
$args = getopt('i:o:t:');
$input_file = $args['i'] ?? '/dev/zero';
$output_file = $args['o'] ?? '/dev/null';
$seconds = DateTime\Duration::seconds((int)($args['t'] ?? 5));
// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
$input_file = Regex\replace($input_file, '(^/dev/fd/)', 'php://fd/');
$output_file = Regex\replace($output_file, '(^/dev/fd/)', 'php://fd/');
$input = new IO\CloseReadStreamHandle(fopen($input_file, 'rb'));
$output = new IO\CloseWriteStreamHandle(fopen($output_file, 'wb'));
IO\write_error_line('piping from %s to %s (for max %d second(s)) ...', $input_file, $output_file, $seconds);
Async\Scheduler::delay($seconds, static fn() => $input->close());
$start = DateTime\Timestamp::monotonic();
$i = 0;
try {
while ($chunk = $input->read(65536)) {
$output->writeAll($chunk);
$i++;
Async\later();
}
} catch (IO\Exception\AlreadyClosedException) {
}
$duration = DateTime\Timestamp::monotonic()->since($start);
$bytes = $i * 65536;
$bytes_formatted = Math\round($bytes / 1024 / 1024 / $duration->getTotalSeconds(), 1);
IO\write_error_line('read %d byte(s) in %s => %dMiB/s', $bytes, $duration->toString(), $bytes_formatted);
IO\write_error_line('peak memory usage of %dMiB', Math\round(memory_get_peak_usage(true) / 1024 / 1024, 1));
return 0;
});