File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed
Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 );
You can’t perform that action at this time.
0 commit comments