Servlet Profiling

What is the best way to perform servlet profiling?

In particular, I'm looking for a solution that can display the execution time of method calls inside a servlet.

I am running Servlet in a Tomcat environment.

I tried VisualVM, but it only allows me to see method calls from the Tomcat thread, not the servlets themselves.

+4
source share
4 answers

I used JProfiler for profiling, they have support for many different servers and give very good control and good reports. However, this is a commercial application, although you can get an evaluation version. You can also take a look at open-source profilers , although I have not used any of them and cannot say how good or bad they are.

Edit I assume that you understand how profiling works, the profiler will execute some tools and attach the agent to the JVM, all this affects the performance. Therefore, never use it in a production environment.

+2
source

A manual approach to this may be to create a filter for the servlets in question, you can measure the time required to complete all the executions and store them somewhere. This is good because the overhead for profiling calls is just as much as the code you write in the filter, I also found it very useful to find pages and servlets that take longer to fully load.

Here's how filters work.

  • Url called
  • The filter enters and saves the URL and current time (startTime)
  • The filter invokes the recipient servlet
  • Servlet runs (servlet, page or jsp)
  • Control returns to filter runtime: currentTime - startTime
  • Record or save the data obtained for future comparisons (i.e. save it in a csv file).
  • Filtering ends.

To do this, create a class that implements the javax.servlet.Filter interface.

public class ProfilingFilter implements Filter { public void init(FilterConfig fc){} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain){ long startTime = System.currentTimeInMillis(); String servletName = ((HttpServletRequest) req)..getPathInfo(); chain.doFilter(req, resp); long executionTime = System.currentTimeInMillis()-startTime; // Implement the following to store or handle the data. logData(servletName, executionTime); } } 

Now add it to your web.xml

 <filter> <filter-name>profilingFilter</filter-name> <filter-class>com.awesome.ProfilingFilter</filter-class> </filter> 

And finally, the mapping:

 <filter-mapping> <filter-name>profilingFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping> 

Remember that this will filter ALL, so you will profile, html, jsp, servlets, images, and what not.

We found that it was very useful to find that it was in the application too much time to answer or was extremely difficult.

You can even include this in production for a day or two to get real data, the impact on productivity will be as big as your code spent on saving profiling data.

Some ideas. Save the statistics on the map inside the servlet context, and then another servlet will display these statistics. this way you don’t even need to access the disk.

+3
source

Why don't you use JConsole - which provides MBeans . Your container application will probably display some useful fields related to your servlet, and if it is not, you can create your own MBeans.

+1
source

2015. Since the JDK is 7u60 (if I understood correctly), jvm has a built-in tool for profiling Java applications. He said that it is very light weight ~ 1% of CPU consumption. It is called Java Flight Recorder. To enable it, you need the jdk version from 7u60 and run the application with the following flags.

 -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=duration=60s,filename=myrecording.jfr 

Here duration=60s sets the duration of profiling (set as much as you need), and filename=myrecording.jfr sets the file to which the profiling information will be saved.

Then you can view the file using java mission control.

Video here

Docs here

+1
source

Source: https://habr.com/ru/post/1303888/


All Articles