r9968 - in helma/helma/trunk/src/helma/scripting/rhino: . debug

[email protected] Sun, 27 Sep 2009 20:59:38 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090927185938.F30DF3D0E2@mia>
Author: hannes
Date: 2009-09-27 20:59:38 +0200 (Sun, 27 Sep 2009)
New Revision: 9968

Added:
   helma/helma/trunk/src/helma/scripting/rhino/debug/Profiler.java
Modified:
   helma/helma/trunk/src/helma/scripting/rhino/RhinoCore.java
   helma/helma/trunk/src/helma/scripting/rhino/RhinoEngine.java
Log:
Backport JS profiler from Helma NG. Use rhino.profile = true to activate.

Details at http://dev.helma.org/trac/helma/changeset/9968

Modified: helma/helma/trunk/src/helma/scripting/rhino/RhinoCore.java
===================================================================
--- helma/helma/trunk/src/helma/scripting/rhino/RhinoCore.java	2009-09-25 10:57:50 UTC (rev 9967)
+++ helma/helma/trunk/src/helma/scripting/rhino/RhinoCore.java	2009-09-27 18:59:38 UTC (rev 9968)
@@ -95,6 +95,7 @@
     // debugger/tracer flags
     boolean hasDebugger = false;
     boolean hasTracer = false;
+    boolean hasProfiler = false;
     private boolean isInitialized = false;
 
     // dynamic portion of the type check sleep that grows
@@ -120,9 +121,10 @@
 
         hasDebugger = "true".equalsIgnoreCase(app.getProperty("rhino.debug"));
         hasTracer = "true".equalsIgnoreCase(app.getProperty("rhino.trace"));
+        hasProfiler = "true".equalsIgnoreCase(app.getProperty("rhino.profile"));
 
         // Set default optimization level according to whether debugger is on
-        if (hasDebugger || hasTracer) {
+        if (hasDebugger || hasTracer || hasProfiler) {
             optLevel = -1;
         } else {
             String opt = app.getProperty("rhino.optlevel");

Modified: helma/helma/trunk/src/helma/scripting/rhino/RhinoEngine.java
===================================================================
--- helma/helma/trunk/src/helma/scripting/rhino/RhinoEngine.java	2009-09-25 10:57:50 UTC (rev 9967)
+++ helma/helma/trunk/src/helma/scripting/rhino/RhinoEngine.java	2009-09-27 18:59:38 UTC (rev 9968)
@@ -28,6 +28,7 @@
 import helma.objectmodel.db.Node;
 import helma.scripting.*;
 import helma.scripting.rhino.debug.Tracer;
+import helma.scripting.rhino.debug.Profiler;
 import helma.util.StringUtils;
 import org.mozilla.javascript.*;
 import org.mozilla.javascript.serialize.ScriptableOutputStream;
@@ -163,6 +164,8 @@
 
         if (core.hasTracer) {
             context.setDebugger(new Tracer(getResponse()), null);
+        } else if (core.hasProfiler) {
+            context.setDebugger(new Profiler(), null);
         }
 
         // register the engine with the current thread
@@ -209,6 +212,16 @@
      *   execution context has terminated.
      */
     public synchronized void exitContext() {
+        if (core.hasProfiler) {
+            try {
+                Profiler profiler = (Profiler) Context.getCurrentContext().getDebugger();
+                String result = profiler.getResult();
+                getResponse().debug("<pre>" + result + "</pre>");
+                System.out.println(result);
+            } catch (Exception x) {
+                app.logError("Error in profiler: " + x, x);
+            }
+        }
         // unregister the engine threadlocal
         engines.set(null);
         Context.exit();

Added: helma/helma/trunk/src/helma/scripting/rhino/debug/Profiler.java