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