Servlet 分析

发布于 2024-08-24 16:13:04 字数 176 浏览 3 评论 0原文

进行 Servlet 分析的最佳方法是什么?

特别是,我正在寻找一种可以显示 servlet 中方法调用的执行时间的解决方案。

我正在 Tomcat 环境中运行 Servlet。

我尝试过 VisualVM,但它只允许我看到来自 Tomcat 线程的方法调用,而不是 Servlet 本身。

What is the best way to undertake Servlet profiling?

In particular I am looking for a solution that can show the execution times of the method calls within the servlet.

I am running the Servlet within a Tomcat environment.

I have tried VisualVM but it only allows me to see the method calls from the Tomcat thread rather than the Servlets themselves.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

季末如歌 2024-08-31 16:13:04

我使用 JProfiler 进行分析,它们支持许多不同的服务器并提供非常好的控制和良好的报告。然而,它是一个商业应用程序,尽管您可以获得评估版本。你也可以看看开源分析器,尽管我没有使用过任何这些并不能说它们有多好或多坏。

编辑
我假设您了解分析器的工作原理,分析器将执行一些检测并将代理附加到 JVM,所有这些都会影响性能。所以永远不要在生产环境中使用它。

I have used JProfiler for profiling, they have support for many different servers and gives very good control and good reports. However it's a commercial application, though you can get an evaluation version. You can take a look at open source profilers as well, though I have not used any of these and can't say how good or bad they are.

Edit
I assume you understand how profiling works, the profiler will do some instrumentation and attach an agent to the JVM, all this affects performance. So never ever use it in production environment.

划一舟意中人 2024-08-31 16:13:04

手动方法可以是为相关 servlet 创建一个过滤器,然后您可以测量所有执行完成所需的时间并将它们存储在某处。
这很好,因为分析调用的开销与您在过滤器中编写的代码一样多,我还发现查找需要较长时间才能完全加载的页面和 servlet 非常有用。

这就是过滤器的工作

  • 原理 URL 被调用
  • 过滤器输入并存储被调用的 url 和当前时间 (startTime)
  • 过滤器调用目标 servlet(s)
  • Servlet 执行(servlet、页面或 jsp)
  • 控制返回到过滤器执行时间为: currentTime - startTime
  • 日志或存储获得的数据以供将来比较(即将其存储到 csv 文件)
  • 过滤器结束

为此,创建一个实现 javax.servlet.Filter 接口的类。

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);
  }
}

现在将其添加到您的 web.xml

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

最后是映射:

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

请注意,这将过滤所有内容,以便您分析、html、jsp、servlet、图像等。

我们发现这对于查找应用程序中究竟哪些内容花费了太多时间来响应或非常繁重非常有用。

您甚至可以在生产中启用此功能一两天以获得真实数据,对性能的影响将与您用于保存分析数据的代码一样大。

一些想法:将统计信息存储在 servlet-context 内的地图中,然后让另一个 servlet 显示这些统计信息。这样你甚至不需要访问磁盘。

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

This is how filters work

  • Url is called
  • Filter enters and stores called url and current time (startTime)
  • Filter invokes destination servlet(s)
  • Servlet executes (servlet, page or jsp)
  • Control returns to the filter execution time is: currentTime - startTime
  • Log or store the data obtained for future comparisson (ie store it to a csv file)
  • Filter ends

To do so 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>

Be aware that this will filter EVERYTHING so you will profile, html, jsp, servlets, images, and what not.

We found this very useful to find what exactly in the application was taking too much time to respond or was extremely heavy.

You can even enable this in production for a day or two to get real data, the punch on performance will be just as big as your code spent to save the profiling data.

Some ideas: Store your statistics in a map inside servlet-context and then have another servlet display those statistics. that way you dont even need to access the disk.

家住魔仙堡 2024-08-31 16:13:04

为什么不使用 JConsole - 它公开 MBean。您的容器应用程序可能会公开一些与您的 servlet 相关的有用字段 - 如果没有,您可以创建自己的 MBean。

Why don't you use JConsole - which exposes MBeans. Your container application will probably expose some useful fields relavent to your servlet - and if it doesn't you can create your own MBeans.

眼角的笑意。 2024-08-31 16:13:04

2015年。
从 JDK 7u60 开始(如果我没记错的话),jvm 有一个内置工具来分析 java 应用程序。据说它非常轻量级~CPU 消耗的 1%。
它称为 Java 飞行记录器。
要启用它,您需要 7u60 版本的 jdk 并使用以下标志启动您的应用程序。

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

这里 duration=60s 设置分析的持续时间(根据需要设置),filename=myrecording.jfr 设置将保存分析信息的文件。

然后您可以使用java Mission Control查看该文件。

视频

此处的文档

2015.
Since JDK 7u60(if I got it right) jvm has a builtin tool to profile java apps. It's said to be very lightweight ~1% of CPU consumption.
It's called Java Flight Recorder.
To enable it you need jdk version from 7u60 and start your app with 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 profiling information will be saved.

Then you can view the file using java mission control.

Video here

Docs here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文