From bd1e9dcc2586be6881d1408ff35f6b866377e992 Mon Sep 17 00:00:00 2001 From: hns Date: Mon, 28 Sep 2009 13:54:14 +0000 Subject: [PATCH] Add rhino.profile.session property to limit profiling to certain user sessions. If set, the indexOf method of the current session id is called with the property as argument, and the profiler is disabled unless the result is 0. To only profile sessions from localhost (but beware of local proxies!): rhino.profile = true rhino.profile.session = 127.0.0.1 To only profile a particular user session: rhino.profile = true rhino.profile.session = 127.0.0.1n5guagu2sdl2jslf Note that for the profiler to work you need to start helma with rhino.profile = true since Helma 1 doesn't support mixed compiled/interpreted mode like Helma NG. --- src/helma/scripting/rhino/RhinoEngine.java | 24 +++++++++++++++++-- src/helma/scripting/rhino/debug/Profiler.java | 5 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/helma/scripting/rhino/RhinoEngine.java b/src/helma/scripting/rhino/RhinoEngine.java index 515eb604..f5c62554 100644 --- a/src/helma/scripting/rhino/RhinoEngine.java +++ b/src/helma/scripting/rhino/RhinoEngine.java @@ -164,7 +164,7 @@ public class RhinoEngine implements ScriptingEngine { if (core.hasTracer) { context.setDebugger(new Tracer(getResponse()), null); - } else if (core.hasProfiler) { + } else if (useProfiler()) { context.setDebugger(new Profiler(), null); } @@ -212,7 +212,7 @@ public class RhinoEngine implements ScriptingEngine { * execution context has terminated. */ public synchronized void exitContext() { - if (core.hasProfiler) { + if (useProfiler()) { try { Profiler profiler = (Profiler) Context.getCurrentContext().getDebugger(); String result = profiler.getResult(); @@ -718,4 +718,24 @@ public class RhinoEngine implements ScriptingEngine { return skin; } + /** + * Determine if we should use a profiler on the current thread. This returns true if + * the rhino.profile app property is set to true (requires restart) and the + * rhino.profile.session property is either unset, or set to "all", or matching + * the session id of the current request. + * @return true if the current request should be profiled + */ + private boolean useProfiler() { + if (!core.hasProfiler) { + return false; + } + String profilerSession = app.getProperty("rhino.profile.session"); + if (profilerSession == null || "all".equalsIgnoreCase(profilerSession)) { + return true; + } + RequestTrans req = getRequest(); + return req != null && req.getSession() != null + && req.getSession().indexOf(profilerSession) == 0; + } + } diff --git a/src/helma/scripting/rhino/debug/Profiler.java b/src/helma/scripting/rhino/debug/Profiler.java index fcd7556d..8c2f9548 100644 --- a/src/helma/scripting/rhino/debug/Profiler.java +++ b/src/helma/scripting/rhino/debug/Profiler.java @@ -15,10 +15,9 @@ public class Profiler implements Debugger { HashMap frames = new HashMap(); /** - * Create a profiler that writes to this response object + * Create a new profiler. */ - public Profiler() { - } + public Profiler() {} /** * Implementws handleCompilationDone in interface org.mozilla.javascript.debug.Debugger