perf4j 是什么
--------------------
perf4j 是一套简单的服务监控框架,可以用来做一些系统常需要的监控,比如实时系统吞吐量,系统响应时间
perf4j生成监控图表
----------------------------
pef4j可以生成的图表支持 Mean, Min, Max, StdDev, Count and TPS
- Mean 平均响应时间
- Min 最小响应时间
- Max 最大响应时间
- Count 总数统计
- TPS 吞吐量
监控图表样式如下
perf4j 使用与配置
-----------------------
这里只介绍perf4j与log4j配合使用,对于使用log4j之外的系统还是请到官网看介绍http://perf4j.codehaus.org/ ,在log4j.xml中插入下面配置
<!-- 打点记录 -->
<logger name="org.perf4j.TimingLogger" additivity="false">
<level value="INFO"/>
<appender-ref ref="CoalescingStatistics"/>
</logger>
<!-- 具体输出的图表,一个配置对应一张图表 -->
<appender name="ExecutionSlow"
class="org.perf4j.log4j.GraphingStatisticsAppender">
<!-- Possible GraphTypes are Mean, Min, Max, StdDev, Count and TPS <!--
<param name="GraphType" value="Count"/>
<!-- The tags of the timed execution blocks to graph are specified here -->
<!--<param name="TagNamesToGraph" value="[*]"/>-->
<appender-ref ref="graphsFileAppender"/>
</appender>
<appender name="ExecutionTPS"
class="org.perf4j.log4j.GraphingStatisticsAppender">
<param name="GraphType" value="TPS"/>
<param name="TagNamesToGraph" value="firstBlock,/order/commit"/>
<appender-ref ref="graphsFileAppender"/>
</appender>
<appender name="ExecutionMax"
class="org.perf4j.log4j.GraphingStatisticsAppender">
<!-- Possible GraphTypes are Mean, Min, Max, StdDev, Count and TPS -->
<param name="GraphType" value="Max"/>
<!-- The tags of the timed execution blocks to graph are specified here -->
<!--<param name="TagNamesToGraph" value="firstBlock,secondBlock"/>-->
<appender-ref ref="graphsFileAppender"/>
</appender>
<!— 分析打点后得出的结果,用于绘制图表的节点 -->
<appender name="graphsFileAppender" class="org.apache.log4j.FileAppender">
<param name="File" value=“perfGraphs.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<!-- 需要展现的图表-->
<appender name="CoalescingStatistics" class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="30000"/>
<appender-ref ref="ExecutionTPS"></appender-ref>
<appender-ref ref="ExecutionTimes"></appender-ref>
<appender-ref ref="ExecutionCount"></appender-ref>
<appender-ref ref="ExecutionMax"></appender-ref>
<appender-ref ref="ExecutionStdDev"></appender-ref>
<appender-ref ref="ExecutionSlow"></appender-ref>
</appender>
在自己的web项目中的web.xml文件中配置如下信息
<servlet>
<servlet-name>perf4j</servlet-name>
<servlet-class>org.perf4j.log4j.servlet.GraphingServlet</servlet-class>
<!--
The graphNames parameter determines which graphs to expose. The
param-value should be a comma-separated list of the
appender NAMES as defined in the log4j.xml file.
-->
<!--<init-param>
<param-name>TPS</param-name>
<param-value>graphExecutionTPS</param-value>
</init-param>-->
<!-- <init-param>
<param-name>Mean</param-name>
<param-value>graphExecutionTimes</param-value>
</init-param>-->
</servlet>
<servlet-mapping>
<servlet-name>perf4j</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
以springmvc项目为列,加入一个拦截器
public class PerformanceInterceptor extends HandlerInterceptorAdapter {
protected Logger logger = Logger.getLogger(this.getClass());
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
StopWatch stopWatch = new Log4JStopWatch();
request.setAttribute("stopWatch", stopWatch);
stopWatch.lap("firstBlock");
String reqURI = request.getRequestURI();
stopWatch.start(reqURI);
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
StopWatch stopWatch = (StopWatch) request.getAttribute("stopWatch");
long spentTime = stopWatch.getElapsedTime();
String reqURI = request.getRequestURI();
stopWatch.stop(reqURI);
stopWatch.lap("secondBlock");
if (spentTime > 1000) {
stopWatch.lap("["+reqURI+"]");
}
super.afterCompletion(request, response, handler, ex);
}
}
配置完成,然后访问 http://xxxx.com/admin,此时需要有访问google服务器的能力,因为渲染图表是利用google在线chartAPI