Skip to content

Instantly share code, notes, and snippets.

@badvision
Created February 9, 2016 23:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save badvision/b6d3dc6732815ddc18f6 to your computer and use it in GitHub Desktop.
Save badvision/b6d3dc6732815ddc18f6 to your computer and use it in GitHub Desktop.
Summarize all open JCR sessions by the stack trace of the code that opened them. This helps identify session leaks in code.
import groovy.json.JsonSlurper
//Get data from here: /system/sling/monitoring/mbeans/org/apache/jackrabbit/oak/%2522SessionStatistics%2522.2.json
def file = '/Users/brobert/Desktop/all_sessions.json' as File
def sanitizeStacktrace = {trace->
if (trace.contains("loginAdministrative")) {
def lines = trace.split("\n")
def startIdx;
for (def idx=0; idx < lines.length; idx++) {
if (lines[idx].contains("loginAdministrative")) {
if (lines[idx+1].contains("org.apache.sling.jcr.resource.internal.helper")) {
for (def idx2=idx+3; idx2 < lines.length; idx2++) {
if (!lines[idx2].contains("org.apache.sling.resourceresolver.impl")) {
return lines[idx2-1..Math.min(idx2+1,lines.length-1)].join("\n")
}
}
} else {
return lines[idx..Math.min(idx+2, lines.length-1)].join("\n")
}
break
}
}
}
return trace
}
def slurper = new JsonSlurper()
def data = slurper.parseText(file.text)
def counters = [:]
data.each{key,value->
if (value instanceof String || !value.containsKey('InitStackTrace')) return
def stacktrace = sanitizeStacktrace(value.InitStackTrace);
if (counters.get(stacktrace) == null) {
counters[stacktrace] = 1
} else {
counters[stacktrace]++
}
}
counters = counters.sort{-it.value}
def total = 0;
counters.each{stack,count->
println("${count} sessions")
println(stack)
total += count
println("------------------------------")
}
println("Total sessions: ${total}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment