Show Action Show View

<?php

	namespace Showcase\Action\Demo\Events;

	use ObjectivePHP\Application\Action\RenderableAction;
	use ObjectivePHP\Application\Action\Parameter\NumericParameter;
	use ObjectivePHP\Application\ApplicationInterface;
	use ObjectivePHP\Application\Workflow\Event\WorkflowEvent;

	class Load extends RenderableAction
	{


		public function run(ApplicationInterface $app)
		{

			$eventsHandler = $app->getEventsHandler();

			// bind callbacks
			$callback = function ($event) use (&$count)
			{
				$count++;
			};


			$callbacks = $this->getParam('callbacks', 100);

			for ($i = 0; $i < $callbacks; $i++)
			{
				$eventsHandler->bind('demo.events.*', $callback);
			}


			// trigger events
			$triggers  = $this->getParam('triggers', 100);
			$startedAt = microtime(true);
			for ($i = 0; $i < $triggers; $i++)
			{
				$eventName = 'demo.events.' . $this->generateRandomEventName();
				$eventsHandler->trigger($eventName);
			}


			$stoppedAt = microtime(true);

			$elapsedTime = round($stoppedAt - $startedAt, 3) * 1000;

			return
				[
					'callbacks.ran'    => $count,
					'events.triggered' => $triggers,
					'callbacks.bound'  => $callbacks,
					'time.elapsed'     => $elapsedTime,
					'page.title'       => 'Events demo'
				];


		}


		protected function generateRandomEventName()
		{

			$character_set_array   = [];
			$character_set_array[] = ['count' => 7, 'characters' => 'abcdefghijklmnopqrstuvwxyz'];
			$character_set_array[] = ['count' => 1, 'characters' => '0123456789'];
			$temp_array            = [];
			foreach ($character_set_array as $character_set)
			{
				for ($i = 0; $i < $character_set['count']; $i++)
				{
					$temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
				}
			}

			return implode('', $temp_array);
		}


	}
<?php
    use ObjectivePHP\Application\View\Helper\Vars;

    Vars::set('page.subtitle', 'Triggers plenty of random triggers with loads of callbacks');
?>


    <h3>Current configuration:</h3>

    <h3>
        <span class="label label-primary">
            <span class="badge"><?= Vars::get('events.triggered') ?></span> Triggers
        </span>
    </h3>
    <h3>
        <span class="label label-danger">
            <span class="badge"><?= Vars::get('callbacks.bound') ?></span> Bound callbacks
        </span>
    </h3>
    <h3>
        <span class="label label-warning">
            <span class="badge"><?= Vars::get('callbacks.ran') ?></span> Ran callbacks
        </span>
    </h3>

    <h3>
        <span class="label label-success">
            Total time <span class="badge"><?= Vars::get('time.elapsed') ?></span> ms
        </span>
    </h3>

<br />
<br />
<br />
<p>
    You can play with parameters "callbacks" ans "triggers" to change the number of events triggered and/or callbacks connected.
    <br />
    <br />
    <a href="?callbacks=500&triggers=100">Example with 500 callbacks and 100 triggers</a> Don't be afraid to click this link, it should not last that long :)

</p>

Current configuration:

100 Triggers

500 Bound callbacks

50000 Ran callbacks

Total time 70 ms




You can play with parameters "callbacks" ans "triggers" to change the number of events triggered and/or callbacks connected.

Example with 500 callbacks and 100 triggers Don't be afraid to click this link, it should not last that long :)