Skip to content

Instantly share code, notes, and snippets.

@znerol
Created September 26, 2014 07:51
Show Gist options
  • Save znerol/fab6f736efb901a28501 to your computer and use it in GitHub Desktop.
Save znerol/fab6f736efb901a28501 to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once __DIR__ . '/vendor/autoload.php';
$n_listeners = 1024;
$n_invocations = 10;
class Sub implements EventSubscriberInterface {
public function onEvent() {
}
public static function getSubscribedEvents() {
return array(
'test.event' => array('onEvent', mt_rand(-1000, 1000)),
);
}
}
$t = array();
// Add definitions.
$t[] = microtime(TRUE);
$container = new ContainerBuilder();
for ($i = 0; $i < $n_listeners; $i++) {
$c = new Sub();
$container->setDefinition('subscriber_' . $i, new Definition('Sub'));
}
// Add subscribers.
$t[] = microtime(TRUE);
$event_dispatcher = new ContainerAwareEventDispatcher($container);
for ($i = 0; $i < $n_listeners; $i++) {
$event_dispatcher->addSubscriberService('subscriber_' . $i, 'Sub');
}
// Prepare listeners.
$t[] = microtime(TRUE);
$event_dispatcher->getListeners('test.event');
// First call to dispatch.
$t[] = microtime(TRUE);
$event_dispatcher->dispatch('test.event');
// Subsequent calls to dispatch.
$t[] = microtime(TRUE);
for ($i = 0; $i < $n_invocations; $i++) {
$event_dispatcher->dispatch('test.event');
}
$t[] = microtime(TRUE);
$dt = array();
for ($i = 1; $i < count($t); $i++) {
$dt[] = $t[$i]-$t[$i-1];
}
error_log(implode(',', array_merge(array($n_listeners, $n_invocations), $dt)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment