benchmark-throughput.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. $args = getopt('i:o:t:');
  4. $if = isset($args['i']) ? $args['i'] : '/dev/zero';
  5. $of = isset($args['o']) ? $args['o'] : '/dev/null';
  6. $t = isset($args['t']) ? $args['t'] : 1;
  7. // passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
  8. $if = str_replace('/dev/fd/', 'php://fd/', $if);
  9. $of = str_replace('/dev/fd/', 'php://fd/', $of);
  10. $loop = new React\EventLoop\StreamSelectLoop();
  11. // setup information stream
  12. $info = new React\Stream\Stream(STDERR, $loop);
  13. $info->pause();
  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. $info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...'. PHP_EOL);
  18. // setup input and output streams and pipe inbetween
  19. $in = new React\Stream\Stream(fopen($if, 'r'), $loop);
  20. $out = new React\Stream\Stream(fopen($of, 'w'), $loop);
  21. $out->pause();
  22. $in->pipe($out);
  23. // stop input stream in $t seconds
  24. $start = microtime(true);
  25. $timeout = $loop->addTimer($t, function () use ($in, &$bytes) {
  26. $in->close();
  27. });
  28. // print stream position once stream closes
  29. $in->on('close', function () use ($in, $start, $timeout, $info) {
  30. $t = microtime(true) - $start;
  31. $timeout->cancel();
  32. $bytes = ftell($in->stream);
  33. $info->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
  34. $info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
  35. });
  36. $loop->run();