Skip to content

Added enhanced test-memory.php, originally from react/react #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2017
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
Prev Previous commit
Next Next commit
Applied argument suggestions by @clue from #59 (comment)
  • Loading branch information
WyriHaximus authored Nov 14, 2016
commit 27ce0787d042f6434c1ea61116f1d20702721eaf
39 changes: 23 additions & 16 deletions examples/test-memory.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
<?php

/**
* Usage, runs indefinetly with the loop from Factory::create: php test-memory.php
* Usage, runs for 6 seconds (anything lower than 5 seconds will be ignored): php test-memory.php 6
* Usage, runs with the stream select loop for 6 seconds: php test-memory.php 6 StreamSelect
* Run the script indefinitely seconds with the loop from the factory and report every 2 seconds:
* php test-memory.php
* Run the script for 30 seconds with the stream_select loop and report every 10 seconds:
* php test-memory.php -t 30 -l StreamSelect -r 10
*/

use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;
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();
$args = getopt('t:l:r:');
$t = isset($args['t']) ? (int)$args['t'] : 0;
$loop = isset($args['l']) && class_exists('React\EventLoop\\' . $args['l'] . 'Loop') ? 'React\EventLoop\\' . $args['l'] . 'Loop' : Factory::create();

if (!($loop instanceof LoopInterface)) {
$loop = new $loop();
}

$i = 0;
$r = isset($args['r']) ? (int)$args['r'] : 2;

$runs = 0;

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

}

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

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

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

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

Expand All @@ -54,7 +60,8 @@
$endTime = time();
$timeTaken = $endTime - $beginTime;

echo "PHP Version:\t\t", phpversion(), "\n";
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";
echo "Runs per second\t\t", round($runs / $timeTaken), "\n";
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy