Add useful tools to trace protocole execution and cost
authorx2003bruneau <x2003bruneau@9869982d-c50d-0410-be91-f2a2ec7c7c7b>
Thu, 22 Feb 2007 00:42:29 +0000 (00:42 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Fri, 4 Jan 2008 23:35:29 +0000 (00:35 +0100)
git-svn-id: svn+ssh://murphy/home/svn/banana/trunk@207 9869982d-c50d-0410-be91-f2a2ec7c7c7b

banana/banana.inc.php.in
banana/mbox.inc.php
banana/nntpcore.inc.php
banana/protocoleinterface.inc.php

index 49b690e..0670860 100644 (file)
@@ -281,6 +281,16 @@ class Banana
         return Banana::$page->css;
     }
 
+    /** Return the execution backtrace of the current BananaProtocole
+     */
+    public function backtrace()
+    {
+        if (Banana::$protocole) {
+            return Banana::$protocole->backtrace();
+        }
+        return null;
+    }
+
     /**************************************************************************/
     /* actions                                                                */
     /**************************************************************************/
index 52b9e7a..db3a1fd 100644 (file)
@@ -14,7 +14,8 @@ require_once dirname(__FILE__) . '/message.inc.php';
 class BananaMBox implements BananaProtocoleInterface
 {
     private $debug      = false;
-    
+    private $bt         = array();
+
     private $_lasterrno = 0;
     private $_lasterror = null;
     
@@ -290,6 +291,17 @@ class BananaMBox implements BananaProtocoleInterface
         return $file . $mail;
     }
 
+    /** Return the execution backtrace
+     */
+    public function backtrace()
+    {
+        if ($this->debug) {
+            return $this->bt;
+        } else {
+            return null;
+        }
+    }
+
 #######
 # Filesystem functions
 #######
@@ -312,14 +324,12 @@ class BananaMBox implements BananaProtocoleInterface
         $action .= ' -f ' . $this->getFileName();
         $cmd = Banana::$mbox_helper . " $action " . implode(' ', $options) . ' ' . implode(' ', $headers);
         if ($this->debug) {
-            echo $cmd . '<br />';
             $start = microtime(true);
         }
         exec($cmd, $out, $return);
         if ($this->debug) {
-            echo '&nbsp;&nbsp;Execution : ' . (microtime(true) - $start) . 's<br />';
-            echo "&nbsp;&nbsp;Retour : $return<br />";
-            echo '&nbsp;&nbsp;Sortie : ' . count($out) . ' ligne(s)<br />';
+            $this->bt[] = array('action' => $cmd, 'time' => (microtime(true) - $start),
+                                'code' => $return, 'response' => count($out));
         }
         if ($return != 0) {
             $this->_lasterrorno = 1;
index 2d5434b..7867145 100644 (file)
@@ -29,6 +29,7 @@ class BananaNNTPCore
 
     /** debug mode */
     private $debug = false;
+    private $bt    = array();
 
     /** constructor
      * @param $host STRING NNTP host
@@ -81,6 +82,15 @@ class BananaNNTPCore
         return $this->lasterrortext;
     }
 
+    public function backtrace()
+    {
+        if ($this->debug) {
+            return $this->bt;
+        } else {
+            return null;
+        }
+    }
+
 # Socket functions
 
     /** get a line from server
@@ -112,6 +122,9 @@ class BananaNNTPCore
                 $array[] = $result;
             }
         }
+        if ($this->debug && $this->bt) {
+            $this->bt[count($this->bt) - 1]['response'] = count($array);
+        }
         return $array;
     }
 
@@ -126,7 +139,7 @@ class BananaNNTPCore
         }
         if ($this->debug) {
             $db_line = preg_replace('/PASS .*/', 'PASS *******', $line);
-            echo $db_line;
+            $this->bt[] = array('action' => $db_line, 'time' => microtime(true));
         }
         return fputs($this->ns, $line, strlen($line));
     }
@@ -163,11 +176,15 @@ class BananaNNTPCore
     private function checkState($strict = true)
     {
         $result = $this->getLine();
-        if ($this->debug) {
-            echo "$result\n";
-        }
         $this->lastresultcode = substr($result, 0, 3);
         $this->lastresulttext = substr($result, 4);
+        if ($this->debug && $this->bt) {
+            $trace =& $this->bt[count($this->bt) - 1];
+            $trace['time']     = microtime(true) - $trace['time'];
+            $trace['code']     = $this->lastresultcode;
+            $trace['message']  = $this->lastresulttext;
+            $trace['response'] = 0;
+        }
         $c = $this->lastresultcode{0};
         if ($c == '2' || (($c == '1' || $c == '3') && !$strict)) {
             return true;
index 1e3e12a..f949a01 100644 (file)
@@ -105,6 +105,14 @@ interface BananaProtocoleInterface
      * @param box STRING boxname
      */
     public function filename();
+
+    /** Return the execution backtrace of the protocole
+     * @return array(trace1, trace2, ...)
+     * a trace has the following structure:
+     *  array('action' => action, 'time' => microtime, 'code' => return code, 'response' => size of the response)
+     * if no backtrace is available, return null
+     */
+    public function backtrace();
 }
 
 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: