95-benchmark-memory.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Run the script indefinitely seconds with the loop from the factory and report every 2 seconds:
  4. * php 95-benchmark-memory.php
  5. * Run the script for 30 seconds with the stream_select loop and report every 10 seconds:
  6. * php 95-benchmark-memory.php -t 30 -l StreamSelect -r 10
  7. */
  8. use React\EventLoop\Loop;
  9. use React\EventLoop\LoopInterface;
  10. use React\EventLoop\TimerInterface;
  11. require __DIR__ . '/../vendor/autoload.php';
  12. $args = getopt('t:l:r:');
  13. $t = isset($args['t']) ? (int)$args['t'] : 0;
  14. $loop = isset($args['l']) && class_exists('React\EventLoop\\' . $args['l'] . 'Loop') ? 'React\EventLoop\\' . $args['l'] . 'Loop' : Loop::get();
  15. if (!($loop instanceof LoopInterface)) {
  16. Loop::set(new $loop());
  17. }
  18. $r = isset($args['r']) ? (int)$args['r'] : 2;
  19. $runs = 0;
  20. if (5 < $t) {
  21. Loop::addTimer($t, function () {
  22. Loop::stop();
  23. });
  24. }
  25. Loop::addPeriodicTimer(0.001, function () use (&$runs) {
  26. $runs++;
  27. Loop::addPeriodicTimer(1, function (TimerInterface $timer) {
  28. Loop::cancelTimer($timer);
  29. });
  30. });
  31. Loop::addPeriodicTimer($r, function () use (&$runs) {
  32. $kmem = round(memory_get_usage() / 1024);
  33. $kmemReal = round(memory_get_usage(true) / 1024);
  34. echo "Runs:\t\t\t$runs\n";
  35. echo "Memory (internal):\t$kmem KiB\n";
  36. echo "Memory (real):\t\t$kmemReal KiB\n";
  37. echo str_repeat('-', 50), "\n";
  38. });
  39. echo "PHP Version:\t\t", phpversion(), "\n";
  40. echo "Loop\t\t\t", get_class(Loop::get()), "\n";
  41. echo "Time\t\t\t", date('r'), "\n";
  42. echo str_repeat('-', 50), "\n";
  43. $beginTime = time();
  44. Loop::run();
  45. $endTime = time();
  46. $timeTaken = $endTime - $beginTime;
  47. echo "PHP Version:\t\t", phpversion(), "\n";
  48. echo "Loop\t\t\t", get_class(Loop::get()), "\n";
  49. echo "Time\t\t\t", date('r'), "\n";
  50. echo "Time taken\t\t", $timeTaken, " seconds\n";
  51. echo "Runs per second\t\t", round($runs / $timeTaken), "\n";