Add a new PlProfiler tool based on PlBacktrace.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 10 Nov 2007 22:36:50 +0000 (23:36 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 10 Nov 2007 22:36:50 +0000 (23:36 +0100)
To use:
Just add
PlProfiler::start("category", "event");
PlProfiler::stop("category");

so, if you want both to profile a loop and each step of the loop:
PlProfiler::start("my loop");
for ($i = 0 ; $i < 2000 ; ++$i) {
  PlProfiler::start("my step", $i);
  do_sth();
  PlProfiler::stop("my step");
}
PlProfiler::stop("my loop");

The result will be displayed as a backtrace by category.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
ChangeLog
classes/plprofiler.php [new file with mode: 0644]

index 25fbb14..0098811 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,9 @@ VERSION 0.9.16                                                        XX XX 2007
 
 New:
 
+    * Core:
+        - New PlProfiler tool                                              -FRU
+
     * Carnet:
         - List updated fields in notifications                             -FRU
 
diff --git a/classes/plprofiler.php b/classes/plprofiler.php
new file mode 100644 (file)
index 0000000..3b66426
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+class PlProfiler
+{
+    static public function start($name, $info = "action")
+    {
+        global $globals;
+        if (!($globals->debug & DEBUG_BT)) {
+            return false;
+        }
+        if (!isset(PlBacktrace::$bt[$name])) {
+            new PlBacktrace($name);
+        }
+        PlBacktrace::$bt[$name]->start($info);
+        return true;
+    }
+
+    static public function stop($name)
+    {
+        global $globals;
+        if (!($globals->debug & DEBUG_BT)) {
+            return false;
+        }
+        PlBacktrace::$bt[$name]->stop();
+        return true;
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>