Skip to content

Instantly share code, notes, and snippets.

@rmariuzzo
Last active July 24, 2020 06:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmariuzzo/b9a930c9ba59a7962721 to your computer and use it in GitHub Desktop.
Save rmariuzzo/b9a930c9ba59a7962721 to your computer and use it in GitHub Desktop.
How to profile Spring MVC request?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
MVC Profiling configuration.
-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/resources/**"/>
<bean class="com.mariuzzo.profiler.ProfilerInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
package com.mariuzzo.profiler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Rubens Mariuzzo <rubens@x-team.com>
*/
public class ProfilerInterceptor extends HandlerInterceptorAdapter {
private static final Logger logger = LoggerFactory.getLogger(ProfilerInterceptor.class);
/**
* This implementation always returns {@code true}.
*
* @param request The current HTTP Servlet request.
* @param response The current HTTP Servlet response.
* @param handler The handler.
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
put(request, "preHandleTime", System.currentTimeMillis());
return true;
}
/**
* This implementation is empty.
*
* @param request The current HTTP Servlet request.
* @param response The current HTTP Servlet response.
* @param handler The handler.
* @param modelAndView The current model and view.
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
put(request, "postHandleTime", System.currentTimeMillis());
}
/**
* This implementation is empty.
*
* @param request The current HTTP Servlet request.
* @param response The current HTTP Servlet response.
* @param handler The handler.
* @param ex The thrown exception, if any.
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
long preHandleTime = (long) get(request, "preHandleTime");
long postHandleTime = (long) get(request, "postHandleTime");
long afterCompletionTime = System.currentTimeMillis();
long handlingTime = postHandleTime - preHandleTime;
long completionTime = afterCompletionTime - postHandleTime;
long totalTime = afterCompletionTime - preHandleTime;
logger.trace(String.format("[%s] handling: %dms, completion: %dms, total: %dms", request.getRequestURI(), handlingTime, completionTime, totalTime));
}
/**
* Put an attribute value to a request.
*
* @param request The request.
* @param key The key of the attribute.
* @param obj The value of the attribute.
*/
private void put(HttpServletRequest request, String key, Object obj) {
request.setAttribute("com.mariuzzo.profiler.ProfilerInterceptor." + key, obj);
}
/**
* Get the attribute value from a request.
*
* @param request The request.
* @param key The key of the attribute.
* @return The value of the attribute.
*/
private Object get(HttpServletRequest request, String key) {
return request.getAttribute("com.mariuzzo.profiler.ProfilerInterceptor." + key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment