Skip to content

Commit a8ef265

Browse files
committed
Basic benchmark scripts
1 parent 21286e2 commit a8ef265

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

benchmark/bench.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
class Bench {
4+
private $prev;
5+
6+
public function start() {
7+
$this->prev = microtime(true);
8+
}
9+
10+
public function snap() {
11+
$prev = $this->prev;
12+
13+
$current = microtime(true);
14+
$this->prev = $current;
15+
16+
return $current - $prev;
17+
}
18+
19+
public function printSnap($name) {
20+
printf("%-40s: %s s\n", $name, $this->snap());
21+
}
22+
}
23+
24+
function benchLoops(array $tests) {
25+
$loops = array(
26+
'StreamSelectLoop',
27+
'LibEventLoop',
28+
'LibEvLoop',
29+
// 'LibUvLoop',
30+
);
31+
32+
foreach ($tests as $testName => $test) {
33+
foreach ($loops as $loopName) {
34+
$loopClass = "React\\EventLoop\\$loopName";
35+
$loop = new $loopClass();
36+
37+
$bench = new Bench();
38+
$bench->start();
39+
40+
$test($loop);
41+
42+
$bench->printSnap("$loopName: $testName");
43+
}
44+
45+
printf("----------------------------------------\n");
46+
}
47+
}

benchmark/timers.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
require __DIR__.'/bench.php';
5+
6+
$tests = array(
7+
'1000 one-off timers' => function ($loop) {
8+
for ($i = 0; $i < 1000; $i++) {
9+
$loop->addTimer(1, function ($signature, $loop) {});
10+
}
11+
$loop->run();
12+
},
13+
'1000 periodic timers' => function ($loop) {
14+
for ($i = 0; $i < 1000; $i++) {
15+
$loop->addPeriodicTimer(1, function ($signature, $loop) use (&$i) {
16+
if ($i >= 1000) {
17+
$loop->cancelTimer($signature);
18+
}
19+
});
20+
}
21+
$loop->run();
22+
},
23+
);
24+
25+
benchLoops($tests);

0 commit comments

Comments
 (0)