Java Simon--性能瓶颈分析工具

有了AOP以及Java instrument之后,Java有很多很好的性能监控工具可以很有效的帮助我们分析系统瓶颈。例如使用jvisualvm的gui连接JVM应用之后可以监控应用的各种状态,可以看到每个函数的执行时间,不过很多时候我们没有jvisualvm的gui帮忙调试,这个时候我比较喜欢使用javasimon,可以通过AOP配置监控service layer方法调用时间,监控jdbc调用时间,还可以监控Web http request所需的时间,同时提供了web dashboard,完美无缺。下面说说怎么在spring相关项目中使用Java Simon。

1. 在Web应用中加入Java Simon的包,如果是maven项目,通过加入如下dependenncy:

<span style="font-size:14px;">            <dependency>
                <groupId>org.javasimon</groupId>
                <artifactId>javasimon-spring</artifactId>
                <version>${<span style="font-family: Arial, Helvetica, sans-serif;">simon-version</span>}</version>
            </dependency>
            <dependency>
                <groupId>org.javasimon</groupId>
                <artifactId>javasimon-console-embed</artifactId>
                <version><span style="font-family: Arial, Helvetica, sans-serif;">${simon-version}</span></version>
            </dependency></span>

2. 在web.xml里面加入以下配置:

2.1 打开web dashboard,注意dashboard在simon-filter之前可以不用monitor web dashboard。

2.2 加入simon-filter监控所有web请求耗时

<span style="font-size:14px;"><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <filter>
                <filter-name>simon-filter</filter-name>
                <filter-class>org.javasimon.javaee.SimonServletFilter</filter-class>
                <!-- optional -->
                <init-param>
                        <param-name>prefix</param-name>
                        <param-value>com.my.app.web</param-value>
                </init-param>
                <!-- optional basic plain-text console -->
                <init-param>
                        <param-name>console-path</param-name>
                        <param-value>/simon-filter</param-value>
                </init-param>
                <!-- optional, necessary when long requests should be reported -->
                <init-param>
                        <param-name>report-threshold-ms</param-name>
                        <param-value>1000</param-value>
                </init-param>
                <!-- optional, used by reporting mechanism - default implementation uses Manager.message
                        to log stuff, this goes to stdout, you can override it to report to file -->
                <init-param>
                        <param-name>request-reporter-class</param-name>
                        <param-value>org.javasimon.javaee.reqreporter.StandardRequestReporter</param-value>
                </init-param>
                <!-- optional, used to specify parameters for StopwatchSource instance -->
                <init-param>
                        <param-name>stopwatch-source-props</param-name>
                        <param-value>includeHttpMethodName=ALWAYS</param-value>
                </init-param>
                <!-- see Javadoc for org.javasimon.javaee.SimonServletFilter for more about available parameters -->
        </filter>

        <!-- this is related to optional Web Console (not the basic ones included in the filter above) - NOT NECESSARY
                for monitoring itself if you use different way how to get the results (JMX, own mechanism, ...) -->
        <filter>
                <filter-name>simon-console-filter</filter-name>
                <filter-class>org.javasimon.console.SimonConsoleFilter</filter-class>
                <init-param>
                        <param-name>url-prefix</param-name>
                        <param-value>/simon-console</param-value>
                </init-param>
        </filter>

        <!-- in case Console is used it is recommended to put it in front of the monitoring filter,
                so it is not monitored -->
        <filter-mapping>
                <filter-name>simon-console-filter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>

        <filter-mapping>
                <filter-name>simon-filter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>
</web-app></span>
3. 在spring application context xm文件中,通过aop的方式monitor所有spring初始化的bean

<span style="font-size:14px;"><bean id="monitoringInterceptor" class="org.javasimon.spring.MonitoringInterceptor" /></span>

<span style="font-size:14px;">  <aop:config>
        <aop:advisor advice-ref="monitoringInterceptor" pointcut="execution(public * com...service.*.*(..))" />
    </aop:config></span>

4. 启动应用,查看http://ip/app/simon-console就可以看到所有被监控service方法的执行时间还有web请求耗时,还可加入对jdbc的监控(通过包装jdbc driver或者jdbc datasource/connection。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。