1<?php 2 3use Sabre\Event\EventEmitter; 4 5include __DIR__ . '/../../vendor/autoload.php'; 6 7abstract class BenchMark { 8 9 protected $startTime; 10 protected $iterations = 10000; 11 protected $totalTime; 12 13 function setUp() { 14 15 } 16 17 abstract function test(); 18 19 function go() { 20 21 $this->setUp(); 22 $this->startTime = microtime(true); 23 $this->test(); 24 $this->totalTime = microtime(true) - $this->startTime; 25 return $this->totalTime; 26 27 } 28 29} 30 31class OneCallBack extends BenchMark { 32 33 protected $emitter; 34 protected $iterations = 100000; 35 36 function setUp() { 37 38 $this->emitter = new EventEmitter(); 39 $this->emitter->on('foo', function() { 40 // NOOP 41 }); 42 43 } 44 45 function test() { 46 47 for ($i = 0;$i < $this->iterations;$i++) { 48 $this->emitter->emit('foo', []); 49 } 50 51 } 52 53} 54 55class ManyCallBacks extends BenchMark { 56 57 protected $emitter; 58 59 function setUp() { 60 61 $this->emitter = new EventEmitter(); 62 for ($i = 0;$i < 100;$i++) { 63 $this->emitter->on('foo', function() { 64 // NOOP 65 }); 66 } 67 68 } 69 70 function test() { 71 72 for ($i = 0;$i < $this->iterations;$i++) { 73 $this->emitter->emit('foo', []); 74 } 75 76 } 77 78} 79 80class ManyPrioritizedCallBacks extends BenchMark { 81 82 protected $emitter; 83 84 function setUp() { 85 86 $this->emitter = new EventEmitter(); 87 for ($i = 0;$i < 100;$i++) { 88 $this->emitter->on('foo', function() { 89 }, 1000 - $i); 90 } 91 92 } 93 94 function test() { 95 96 for ($i = 0;$i < $this->iterations;$i++) { 97 $this->emitter->emit('foo', []); 98 } 99 100 } 101 102} 103 104$tests = [ 105 'OneCallBack', 106 'ManyCallBacks', 107 'ManyPrioritizedCallBacks', 108]; 109 110foreach ($tests as $test) { 111 112 $testObj = new $test(); 113 $result = $testObj->go(); 114 echo $test . " " . $result . "\n"; 115 116} 117