Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added enhanced test-memory.php, originally from react/react
  • Loading branch information
WyriHaximus authored Nov 13, 2016
commit 480228fe16462a0b66d9e18ca5f66a5838c0ccc6
54 changes: 54 additions & 0 deletions examples/test-memory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

use React\EventLoop\Factory;
use React\EventLoop\Timer\TimerInterface;

require __DIR__.'/vendor/autoload.php';

if (isset($argv[2]) && class_exists('React\EventLoop\\' . $argv[2] . 'Loop')) {
$class = 'React\EventLoop\\' . $argv[2] . 'Loop';
$loop = new $class();
} else {
$loop = Factory::create();
}

$i = 0;

if (isset($argv[1]) && 5 < (int)$argv[1]) {
$loop->addTimer((int)$argv[1], function (TimerInterface $timer) {
$timer->getLoop()->stop();
});

}

$loop->addPeriodicTimer(0.001, function () use (&$i, $loop) {
$i++;

$loop->addPeriodicTimer(1, function (TimerInterface $timer) {
$timer->cancel();
});
});

$loop->addPeriodicTimer(2, function () use (&$i) {
$kmem = round(memory_get_usage() / 1024);
$kmemReal = round(memory_get_usage(true) / 1024);
echo "Runs:\t\t\t$i\n";
echo "Memory (internal):\t$kmem KiB\n";
echo "Memory (real):\t\t$kmemReal KiB\n";
echo str_repeat('-', 50), "\n";
});

echo "Loop\t\t\t", get_class($loop), "\n";
echo "Time\t\t\t", date('r'), "\n";

echo str_repeat('-', 50), "\n";

$beginTime = time();
$loop->run();
$endTime = time();
$timeTaken = $endTime - $beginTime;

echo "Loop\t\t\t", get_class($loop), "\n";
echo "Time\t\t\t", date('r'), "\n";
echo "Time taken\t\t", $timeTaken, " seconds\n";
echo "Runs per second\t\t", round($i / $timeTaken), "\n";