Feature which optionally times the performance of snippet evaluation #1377

Closed
wants to merge 3 commits into
from
View
6 contributors.md
@@ -65,3 +65,9 @@ Francis Rhys-Jones
### Email: ###
francis.rhys-jones at guardian dot co dot uk
+
+### Name: ###
+Noel Kennedy
+
+### Email: ###
+nkennedy at rvc dot ac dot uk
View
5 web/webkit/src/main/scala/net/liftweb/http/LiftSession.scala
@@ -1686,10 +1686,11 @@ class LiftSession(private[http] val _contextPath: String, val uniqueId: String,
val ret: NodeSeq =
try {
-
snippetName.map{snippet =>
val (cls, method) = splitColonPair(snippet)
- S.doSnippet(snippet)(
+ val snippetTimer = SnippetTimer.evaluate(snippet)
+
+ snippetTimer apply S.doSnippet(snippet)(
runWhitelist(snippet, cls, method, kids){(S.locateMappedSnippet(snippet).map(_(kids)) or
locSnippet(snippet)).openOr(
S.locateSnippet(snippet).map(_(kids)) openOr {
View
39 web/webkit/src/main/scala/net/liftweb/http/SnippetTimer.scala
@@ -0,0 +1,39 @@
+package net.liftweb.http
+
+import xml.NodeSeq
+import net.liftweb.util.TimeHelpers._
+import net.liftweb.util.Props
+
+/**
+ * Times the performance of evaluating of individual snippets
+ */
+object SnippetTimer {
+
+ /**
+ * The configured snippet timing function. Defaults to not timing snippets.
+ */
+ lazy val evaluate: String => (=> NodeSeq) => NodeSeq = {
+ if(Props.getBool("run.timesnippets",defVal = false))
+ timeSnippet _
+ else
+ noTiming _
+ }
+
+ /**
+ * Times the evaluation of a snippet
+ * @param snippetName String The name of the snippet
+ * @param f The snippet function
+ * @return NodeSeq The result of evaluating f
+ */
+ def timeSnippet(snippetName:String)(f: => NodeSeq) = {
+ logTime("Snippet %s evaluation" format snippetName, f)
+ }
+
+ /**
+ * A function which doesn't time a snippet but just evaluates it
+ * @param snippetName String Name of the snippet which is ignored
+ * @param f The snippet function
+ * @return NodeSeq The result of evaluating f
+ */
+ def noTiming(snippetName:String)(f: => NodeSeq) = f
+}