Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/benchmarks'
Browse files Browse the repository at this point in the history
Close #8
  • Loading branch information
weierophinney committed Sep 29, 2015
2 parents fb3ab06 + d1cc740 commit f5d3956
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ and interrupt execution of listeners.

- File issues at https://github.com/zendframework/zend-eventmanager/issues
- Documentation is at http://framework.zend.com/manual/current/en/index.html#zend-eventmanager

## Benchmarks

We provide scripts for benchmarking zend-eventmanager using the
[Athletic](https://github.com/polyfractal/athletic) framework; these can be
found in the `benchmarks/` directory.

To execute the benchmarks you can run the following command:

```bash
$ vendor/bin/athletic -p benchmarks
```
46 changes: 46 additions & 0 deletions benchmarks/MultipleEventIndividualSharedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ZendBench\EventManager;

use Zend\EventManager\SharedEventManager;
use Zend\EventManager\EventManager;
use Athletic\AthleticEvent;

class MultipleEventIndividualSharedListener extends AthleticEvent
{
use TraitEventBench;

private $sharedEvents;

private $events;

private $eventsToTrigger;

public function setUp()
{
$identifiers = $this->getIdentifierList();
$this->sharedEvents = new SharedEventManager();
foreach ($this->getEventList() as $event) {
$this->sharedEvents->attach($identifiers[0], $event, $this->generateCallback());
}
$this->events = new EventManager();
$this->events->setSharedManager($this->sharedEvents);
$this->events->setIdentifiers([$identifiers[0]]);

$this->eventsToTrigger = array_filter($this->getEventList(), function ($value) {
return ($value !== '*');
});
}

/**
* Trigger the event list
*
* @iterations 5000
*/
public function trigger()
{
foreach ($this->eventsToTrigger as $event) {
$this->events->trigger($event);
}
}
}
35 changes: 35 additions & 0 deletions benchmarks/MultipleEventLocalListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ZendBench\EventManager;

use Zend\EventManager\EventManager;
use Athletic\AthleticEvent;

class MultipleEventLocalListener extends AthleticEvent
{
use TraitEventBench;

private $eventsToTrigger;

public function setUp()
{
$this->events = new EventManager();

$this->eventsToTrigger = array_filter($this->getEventList(), function ($value) {
return ($value !== '*');
});
}

/**
* Attach and trigger the event list
*
* @iterations 5000
*/
public function trigger()
{
foreach ($this->eventsToTrigger as $event) {
$this->events->attach($event, $this->generateCallback());
$this->events->trigger($event);
}
}
}
51 changes: 51 additions & 0 deletions benchmarks/MultipleEventMultipleLocalAndSharedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace ZendBench\EventManager;

use Zend\EventManager\SharedEventManager;
use Zend\EventManager\EventManager;
use Athletic\AthleticEvent;

class MultipleEventMultipleLocalAndSharedListener extends AthleticEvent
{
use TraitEventBench;

private $sharedEvents;

private $events;

private $eventsToTrigger;

public function setUp()
{
$identifiers = $this->getIdentifierList();
$this->sharedEvents = new SharedEventManager();
foreach ($this->getIdentifierList() as $identifier) {
foreach ($this->getEventList() as $event) {
$this->sharedEvents->attach($identifier, $event, $this->generateCallback());
}
}
$this->events = new EventManager();
$this->events->setSharedManager($this->sharedEvents);
$this->events->setIdentifiers($identifiers);

$this->eventsToTrigger = array_filter($this->getEventList(), function ($value) {
return ($value !== '*');
});
}

/**
* Attach and trigger the event list
*
* @iterations 5000
*/
public function trigger()
{
foreach ($this->eventsToTrigger as $event) {
for ($i = 0; $i < $this->numListeners; $i += 1) {
$this->events->attach($event, $this->generateCallback());
}
$this->events->trigger($event);
}
}
}
48 changes: 48 additions & 0 deletions benchmarks/MultipleEventMultipleSharedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace ZendBench\EventManager;

use Zend\EventManager\SharedEventManager;
use Zend\EventManager\EventManager;
use Athletic\AthleticEvent;

class MultipleEventMultipleSharedListener extends AthleticEvent
{
use TraitEventBench;

private $sharedEvents;

private $events;

private $eventsToTrigger;

public function setUp()
{
$identifiers = $this->getIdentifierList();
$this->sharedEvents = new SharedEventManager();
foreach ($this->getIdentifierList() as $identifier) {
foreach ($this->getEventList() as $event) {
$this->sharedEvents->attach($identifier, $event, $this->generateCallback());
}
}
$this->events = new EventManager();
$this->events->setSharedManager($this->sharedEvents);
$this->events->setIdentifiers($identifiers);

$this->eventsToTrigger = array_filter($this->getEventList(), function ($value) {
return ($value !== '*');
});
}

/**
* Trigger the event list
*
* @iterations 5000
*/
public function trigger()
{
foreach ($this->eventsToTrigger as $event) {
$this->events->trigger($event);
}
}
}
31 changes: 31 additions & 0 deletions benchmarks/SingleEventMultipleListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ZendBench\EventManager;

use Zend\EventManager\EventManager;
use Athletic\AthleticEvent;

class SingleEventMultipleListener extends AthleticEvent
{
use TraitEventBench;

private $events;

public function setUp()
{
$this->events = new EventManager();
for ($i = 0; $i < $this->numListeners; $i++) {
$this->events->attach('dispatch', $this->generateCallback());
}
}

/**
* Trigger the dispatch event
*
* @iterations 5000
*/
public function trigger()
{
$this->events->trigger('dispatch');
}
}
38 changes: 38 additions & 0 deletions benchmarks/SingleEventMultipleSharedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ZendBench\EventManager;

use Zend\EventManager\SharedEventManager;
use Zend\EventManager\EventManager;
use Athletic\AthleticEvent;

class SingleEventMultipleSharedListener extends AthleticEvent
{
use TraitEventBench;

private $sharedEvents;

private $events;

public function setUp()
{
$identifiers = $this->getIdentifierList();
$this->sharedEvents = new SharedEventManager();
for ($i = 0; $i < $this->numListeners; $i += 1) {
$this->sharedEvents->attach($identifiers[0], 'dispatch', $this->generateCallback());
}
$this->events = new EventManager();
$this->events->setSharedManager($this->sharedEvents);
$this->events->setIdentifiers([$identifiers[0]]);
}

/**
* Trigger the dispatch event
*
* @iterations 5000
*/
public function trigger()
{
$this->events->trigger('dispatch');
}
}
29 changes: 29 additions & 0 deletions benchmarks/SingleEventSingleListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ZendBench\EventManager;

use Zend\EventManager\EventManager;
use Athletic\AthleticEvent;

class SingleEventSingleListener extends AthleticEvent
{
use TraitEventBench;

private $events;

public function setUp()
{
$this->events = new EventManager();
$this->events->attach('dispatch', $this->generateCallback());
}

/**
* Trigger the dispatch event
*
* @iterations 5000
*/
public function trigger()
{
$this->events->trigger('dispatch');
}
}
36 changes: 36 additions & 0 deletions benchmarks/SingleEventSingleSharedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ZendBench\EventManager;

use Zend\EventManager\SharedEventManager;
use Zend\EventManager\EventManager;
use Athletic\AthleticEvent;

class SingleEventSingleSharedListener extends AthleticEvent
{
use TraitEventBench;

private $sharedEvents;

private $events;

public function setUp()
{
$identifiers = $this->getIdentifierList();
$this->sharedEvents = new SharedEventManager();
$this->sharedEvents->attach($identifiers[0], 'dispatch', $this->generateCallback());
$this->events = new EventManager();
$this->events->setSharedManager($this->sharedEvents);
$this->events->setIdentifiers([$identifiers[0]]);
}

/**
* Trigger the dispatch event
*
* @iterations 5000
*/
public function trigger()
{
$this->events->trigger('dispatch');
}
}
34 changes: 34 additions & 0 deletions benchmarks/TraitEventBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace ZendBench\EventManager;

trait TraitEventBench
{
private $numListeners = 50;

private function generateCallback()
{
return function ($e) {
};
}

private function getEventList()
{
return [
'dispatch',
'dispatch.post',
'*',
];
}

private function getIdentifierList()
{
return [
'Zend\Stdlib\DispatchableInterface',
'Zend\Mvc\Controller\AbstractController',
'Zend\Mvc\Controller\AbstractActionController',
'Zend\Mvc\Controller\AbstractRestfulController',
'ZF\Rest\RestController',
'CustomRestController',
];
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"require": {
"php": ">=5.5",
"zendframework/zend-stdlib": "~2.5"
"zendframework/zend-stdlib": "~2.5",
"athletic/athletic": "dev-master"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -26,7 +27,8 @@
},
"autoload-dev": {
"psr-4": {
"ZendTest\\EventManager\\": "test/"
"ZendTest\\EventManager\\": "test/",
"ZendBench\\EventManager\\": "benchmarks/"
}
},
"require-dev": {
Expand Down

0 comments on commit f5d3956

Please sign in to comment.