From 6a684b9ba469c52928dbc05725e51e1d88508c26 Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Thu, 22 Feb 2007 00:42:29 +0000 Subject: [PATCH] Add useful tools to trace protocole execution and cost git-svn-id: svn+ssh://murphy/home/svn/banana/trunk@207 9869982d-c50d-0410-be91-f2a2ec7c7c7b --- banana/banana.inc.php.in | 10 ++++++++++ banana/mbox.inc.php | 20 +++++++++++++++----- banana/nntpcore.inc.php | 25 +++++++++++++++++++++---- banana/protocoleinterface.inc.php | 8 ++++++++ 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/banana/banana.inc.php.in b/banana/banana.inc.php.in index 49b690e..0670860 100644 --- a/banana/banana.inc.php.in +++ b/banana/banana.inc.php.in @@ -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 */ /**************************************************************************/ diff --git a/banana/mbox.inc.php b/banana/mbox.inc.php index 52b9e7a..db3a1fd 100644 --- a/banana/mbox.inc.php +++ b/banana/mbox.inc.php @@ -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 . '
'; $start = microtime(true); } exec($cmd, $out, $return); if ($this->debug) { - echo '  Execution : ' . (microtime(true) - $start) . 's
'; - echo "  Retour : $return
"; - echo '  Sortie : ' . count($out) . ' ligne(s)
'; + $this->bt[] = array('action' => $cmd, 'time' => (microtime(true) - $start), + 'code' => $return, 'response' => count($out)); } if ($return != 0) { $this->_lasterrorno = 1; diff --git a/banana/nntpcore.inc.php b/banana/nntpcore.inc.php index 2d5434b..7867145 100644 --- a/banana/nntpcore.inc.php +++ b/banana/nntpcore.inc.php @@ -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; diff --git a/banana/protocoleinterface.inc.php b/banana/protocoleinterface.inc.php index 1e3e12a..f949a01 100644 --- a/banana/protocoleinterface.inc.php +++ b/banana/protocoleinterface.inc.php @@ -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: -- 2.1.4