diff options
author | Jonatan Kronqvist <jonatan.kronqvist@itmill.com> | 2012-04-19 06:33:34 +0000 |
---|---|---|
committer | Jonatan Kronqvist <jonatan.kronqvist@itmill.com> | 2012-04-19 06:33:34 +0000 |
commit | c558076a1981adabbe99c1ec3dbf869ecb27cace (patch) | |
tree | 2859bbc2f3bb030eb08d20a19e9ebd31943551b3 /src/com/vaadin/terminal/gwt/server/RequestTimer.java | |
parent | 5b9baad920779278f9e24f22e45499604ef6f7bf (diff) | |
download | vaadin-framework-c558076a1981adabbe99c1ec3dbf869ecb27cace.tar.gz vaadin-framework-c558076a1981adabbe99c1ec3dbf869ecb27cace.zip |
Implemented #8694 - API providing profiling info for TestBench 3.x
svn changeset:23582/svn branch:6.8
Diffstat (limited to 'src/com/vaadin/terminal/gwt/server/RequestTimer.java')
-rw-r--r-- | src/com/vaadin/terminal/gwt/server/RequestTimer.java | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/src/com/vaadin/terminal/gwt/server/RequestTimer.java b/src/com/vaadin/terminal/gwt/server/RequestTimer.java new file mode 100644 index 0000000000..3649ed3a80 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/RequestTimer.java @@ -0,0 +1,138 @@ +package com.vaadin.terminal.gwt.server; + +import javax.portlet.PortletRequest; +import javax.portlet.PortletSession; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +/** + * Times the handling of requests and stores the information as an attribute in + * the request. The timing info is later passed on to the client in the UIDL and + * the client provides JavaScript API for accessing this data from e.g. + * TestBench. + * + * @author Jonatan Kronqvist / Vaadin Ltd + */ +public class RequestTimer { + public static final String SESSION_ATTR_ID = "REQUESTTIMER"; + + private long requestStartTime = 0; + private long totalSessionTime = 0; + private long lastRequestTime = -1; + + /** + * This class acts as a proxy for setting and getting session and request + * attributes on HttpServletRequests and PortletRequests. Using this class + * we don't need to duplicate code everywhere. + */ + static class RequestWrapper { + private final HttpServletRequest servletRequest; + private final PortletRequest portletRequest; + + public RequestWrapper(HttpServletRequest servletRequest) { + this.servletRequest = servletRequest; + portletRequest = null; + } + + public RequestWrapper(PortletRequest portletRequest) { + this.portletRequest = portletRequest; + servletRequest = null; + } + + public void setAttribute(String name, Object value) { + if (servletRequest != null) { + servletRequest.setAttribute(name, value); + } else { + portletRequest.setAttribute(name, value); + } + } + + public void setSessionAttribute(String name, Object value) { + if (servletRequest != null) { + HttpSession session = servletRequest.getSession(); + if (session != null) { + session.setAttribute(name, value); + } + } else { + PortletSession portletSession = portletRequest + .getPortletSession(); + if (portletSession != null) { + portletSession.setAttribute(name, value); + } + } + } + + public Object getSessionAttribute(String name) { + if (servletRequest != null) { + HttpSession session = servletRequest.getSession(); + if (session != null) { + return session.getAttribute(name); + } + } else { + PortletSession portletSession = portletRequest + .getPortletSession(); + if (portletSession != null) { + return portletSession.getAttribute(name); + } + } + return null; + } + } + + /** + * Starts the timing of a request. This should be called before any + * processing of the request. + * + * @param request + * the request. + */ + public void start(RequestWrapper request) { + requestStartTime = System.nanoTime(); + request.setAttribute("TOTAL", totalSessionTime); + request.setAttribute("LASTREQUEST", lastRequestTime); + } + + /** + * Stops the timing of a request. This should be called when all processing + * of a request has finished. + */ + public void stop() { + // Measure and store the total handling time. This data can be + // used in TestBench 3 tests. + long time = (System.nanoTime() - requestStartTime) / 1000000; + lastRequestTime = time; + totalSessionTime += time; + } + + /** + * Returns a valid request timer for the specified request. Timers are + * session bound. + * + * @param request + * the request for which to get a valid timer. + * @return a valid timer. + */ + public static RequestTimer get(RequestWrapper request) { + RequestTimer timer = (RequestTimer) request + .getSessionAttribute(SESSION_ATTR_ID); + if (timer == null) { + timer = new RequestTimer(); + } + return timer; + } + + /** + * Associates the specified request timer with the specified request. Since + * {@link #get(RequestWrapper)} will, at one point or another, return a new + * instance, this method should be called to keep the request <-> timer + * association updated. + * + * @param request + * the request for which to set the timer. + * @param requestTimer + * the timer. + */ + public static void set(RequestWrapper request, RequestTimer requestTimer) { + request.setSessionAttribute(RequestTimer.SESSION_ATTR_ID, requestTimer); + } +} |