What techniques could I use to modify a set of JSPs to measure the overall performance of the JSPs and then to further narrow down and identify the specific areas within the JSP where the most time is spent?
My current approach is to simply use a scriptlet and System.currentTimeMillis():
top of JSP
<%
StringBuilder result = new StringBuilder();
long startTime = System.currentTimeMillis();
%>
… JSP code here
<%
long duration = System.currentTimeMillis() - startTime;
if (duration > 100L) { //over 100 ms
result.append("JSP page took "+duration+"ms");
}
%>
<!-- <%=result%> -->
But the problems with this are:
- polluted JSPs with this performance code,
- copy pasted code in several JSPs,
- does not work well when JSPs are included with <%@ include … %> since the result variable is already be defined in multiple JSPs.
#1 – the golden rule – don’t put any logic in JSPs. write custom tags. extract any logic into java code. JSP is the presentation layer.
#2 – run servlet container in a profiler (like jvisualvm or even better: yourkit). This will tell you exactly where the time is spent
NOTE – nowadays you don’t need to “run an app in a profiler” – if using JDK6+ you can pop open yourkit or jvisualvm (comes with JDK) and attach magically while it’s running. it’s really handy.