Browse Source

add timer statistic

李亮 8 năm trước cách đây
mục cha
commit
f285b7ead4

+ 10 - 0
Events/Ev.php

@@ -181,4 +181,14 @@ class Ev implements EventInterface
             $event->stop();
         }
     }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return count($this->_eventTimer);
+    }
 }

+ 10 - 0
Events/Event.php

@@ -196,4 +196,14 @@ class Event implements EventInterface
             $event->del();
         }
     }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return count($this->_eventTimer);
+    }
 }

+ 7 - 0
Events/EventInterface.php

@@ -97,4 +97,11 @@ interface EventInterface
      * @return mixed
      */
     public function destroy();
+
+    /**
+     * Get Timer count.
+     *
+     * @return mixed
+     */
+    public function getTimerCount();
 }

+ 10 - 0
Events/Libevent.php

@@ -213,5 +213,15 @@ class Libevent implements EventInterface
             event_del($event);
         }
     }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return count($this->_eventTimer);
+    }
 }
 

+ 18 - 5
Events/React/ExtEventLoop.php

@@ -64,17 +64,20 @@ class ExtEventLoop extends \React\EventLoop\ExtEventLoop
             case EventInterface::EV_SIGNAL:
                 return $this->addSignal($fd, $func);
             case EventInterface::EV_TIMER:
+                $timer_id = ++$this->_timerIdIndex;
                 $timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
                     call_user_func_array($func, $args);
                 });
-                $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
-                return $this->_timerIdIndex;
+                $this->_timerIdMap[$timer_id] = $timer_obj;
+                return $timer_id;
             case EventInterface::EV_TIMER_ONCE:
-                $timer_obj = $this->addTimer($fd, function() use ($func, $args) {
+                $timer_id = ++$this->_timerIdIndex;
+                $timer_obj = $this->addTimer($fd, function() use ($func, $args, $timer_id) {
+                    unset($this->_timerIdMap[$timer_id]);
                     call_user_func_array($func, $args);
                 });
-                $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
-                return $this->_timerIdIndex;
+                $this->_timerIdMap[$timer_id] = $timer_obj;
+                return $timer_id;
         }
         return false;
     }
@@ -170,4 +173,14 @@ class ExtEventLoop extends \React\EventLoop\ExtEventLoop
             $event->del();
         }
     }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return count($this->_timerIdMap);
+    }
 }

+ 18 - 5
Events/React/LibEventLoop.php

@@ -64,17 +64,20 @@ class LibEventLoop extends \React\EventLoop\LibEventLoop
             case EventInterface::EV_SIGNAL:
                 return $this->addSignal($fd, $func);
             case EventInterface::EV_TIMER:
+                $timer_id = ++$this->_timerIdIndex;
                 $timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
                     call_user_func_array($func, $args);
                 });
-                $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
-                return $this->_timerIdIndex;
+                $this->_timerIdMap[$timer_id] = $timer_obj;
+                return $timer_id;
             case EventInterface::EV_TIMER_ONCE:
-                $timer_obj = $this->addTimer($fd, function() use ($func, $args) {
+                $timer_id = ++$this->_timerIdIndex;
+                $timer_obj = $this->addTimer($fd, function() use ($func, $args, $timer_id) {
+                    unset($this->_timerIdMap[$timer_id]);
                     call_user_func_array($func, $args);
                 });
-                $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
-                return $this->_timerIdIndex;
+                $this->_timerIdMap[$timer_id] = $timer_obj;
+                return $timer_id;
         }
         return false;
     }
@@ -171,4 +174,14 @@ class LibEventLoop extends \React\EventLoop\LibEventLoop
             event_del($event);
         }
     }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return count($this->_timerIdMap);
+    }
 }

+ 10 - 0
Events/React/StreamSelectLoop.php

@@ -173,4 +173,14 @@ class StreamSelectLoop extends \React\EventLoop\StreamSelectLoop
     {
 
     }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return count($this->_timerIdMap);
+    }
 }

+ 10 - 0
Events/Select.php

@@ -319,4 +319,14 @@ class Select implements EventInterface
     {
 
     }
+
+    /**
+     * Get timer count.
+     *
+     * @return integer
+     */
+    public function getTimerCount()
+    {
+        return count($this->_eventTimer);
+    }
 }

+ 2 - 2
Worker.php

@@ -1281,7 +1281,7 @@ class Worker
             file_put_contents(self::$_statisticsFile,
                 "pid\tmemory  " . str_pad('listening', self::$_maxSocketNameLength) . " " . str_pad('worker_name',
                     self::$_maxWorkerNameLength) . " connections " . str_pad('total_request',
-                    13) . " " . str_pad('send_fail', 9) . " " . str_pad('throw_exception', 15) . "\n", FILE_APPEND);
+                    13) . " " . str_pad('send_fail', 9) . " " . str_pad('timers', 15) . "\n", FILE_APPEND);
 
             chmod(self::$_statisticsFile, 0722);
 
@@ -1301,7 +1301,7 @@ class Worker
         $worker_status_str .= str_pad(ConnectionInterface::$statistics['connection_count'],
                 11) . " " . str_pad(ConnectionInterface::$statistics['total_request'],
                 14) . " " . str_pad(ConnectionInterface::$statistics['send_fail'],
-                9) . " " . str_pad(ConnectionInterface::$statistics['throw_exception'], 15) . "\n";
+                9) . " " . str_pad(self::$globalEvent->getTimerCount(), 15) . "\n";
         file_put_contents(self::$_statisticsFile, $worker_status_str, FILE_APPEND);
     }