使用Perf4J来检查方法性能

2009-09-17 15:56使用Perf4J来检查方法性能

首页
http://perf4j.codehaus.org/index.html

下载地址
http://repository.codehaus.org/org/perf4j/perf4j/

得到jar包
perf4j-0.9.12.jar

放置到WEB-INF/lib下面

我的UserController.java 片段如下:
StopWatch watch1 = new Log4JStopWatch();//定义时钟
UserQuery query = new UserQuery();
if (!"all".equalsIgnoreCase(select)) {
List<String> companies = this.getCurrentUserCompanyIds(request);
query.setCompanyIds(StringUtil.changeList2String(companies));
   List<String> workgroups = this.getCurrentUserWorkgroupIds(request);
   query.setWorkgroupIds(StringUtil.changeList2String(workgroups)); //1,
}
query.setStart(start);
query.setPageSize(pageSize);
if (StringUtil.isNotBlank(queryLoginId)) {
query.setLoginId(queryLoginId);
}
watch1.lap("queryPrepare"); //记录时间,查询前的准备
List<UserDTO> users = userManager.queryUsersWithWorkgroupName(query);
watch1.lap("query");//查询耗时
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("success", Boolean.TRUE);
if (users != null && !users.isEmpty()) {
hm.put("users", users);
} else {
hm.put("users", new ArrayList<User>());
}
hm.put("paging", query);
JSONObject jo = JSONObject.fromObject(hm, this.configJson());
PrintWriter out = null;
watch1.lap("transJSON"); //JAVA对象转JSON耗时

修改web.xml,增加如下内容:
<servlet>
        <servlet-name>perf4j</servlet-name>
        <servlet-class>org.perf4j.log4j.servlet.GraphingServlet</servlet-class>
        <init-param>
            <param-name>graphNames</param-name>
            <param-value>graphExecutionTimes,graphExecutionTPS</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>perf4j</servlet-name>
        <url-pattern>/perf4j</url-pattern>
    </servlet-mapping>

修改log4j.xml,唉,其实我不会配置log4j.properties和log4j.xml,只能参考着官方文档上修改,我的log4j.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
   </layout>
</appender>
<!-- Perf4J appenders -->
<appender name="CoalescingStatistics"
   class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
   <param name="TimeSlice" value="10000"/>
   <appender-ref ref="fileAppender"/>
   <appender-ref ref="graphExecutionTimes"/>
   <appender-ref ref="graphExecutionTPS"/>
</appender>

<!-- This file appender is used to output aggregated performance statistics -->
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
   <param name="File" value="log/perfStats.log"/>
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%m%n"/>
   </layout>
</appender>
<appender name="graphExecutionTimes"
   class="org.perf4j.log4j.GraphingStatisticsAppender">
   <!-- Possible GraphTypes are Mean, Min, Max, StdDev, Count and TPS -->
   <param name="GraphType" value="Mean"/>
   <!-- The tags of the timed execution blocks to graph are specified here -->
   <!-- 这几个名字和UserController里面的对应 -->
   <param name="TagNamesToGraph" value="queryPrepare,query,transJSON"/>
   <appender-ref ref="graphsFileAppender"/>
</appender>
<appender name="graphExecutionTPS"
   class="org.perf4j.log4j.GraphingStatisticsAppender">
   <param name="GraphType" value="TPS"/>
   <param name="TagNamesToGraph" value="queryPrepare,query,transJSON"/>
   <appender-ref ref="graphsFileAppender"/>
</appender>
<appender name="graphsFileAppender" class="org.apache.log4j.FileAppender">
   <param name="File" value="log/perfGraphs.log"/>
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%m%n"/>
   </layout>
</appender>
<!-- Loggers -->
<logger name="org.perf4j.TimingLogger" additivity="false">
   <level value="DEBUG"/>
   <appender-ref ref="CoalescingStatistics"/>
</logger>
<root>
   <level value="ERROR"/>
   <appender-ref ref="console"/>
</root>
</log4j:configuration>

启动应用服务器,点击用户查询的功能,然后访问地址:
http://localhost:8088/icpmis/perf4j
就能看到这几个方法的实时的图像了。经过验证发现,转JSON的时间比较长。

你可能感兴趣的:(Web,log4j,json,xml,应用服务器)