技术开发 频道

使用Perf4J进行性能分析和监控

  集成Log4J

  Perf4J的扩展功能大部分通过一套定制的log4j appender提供。这样开发人员就能在部署阶段通过非常熟悉的日志框架来零零散散的添加分析和监控功能(未来的Perf4J将提供定制logback appender和java.util.logging处理器)。其中一个重要的功能是允许Perf4J作为JMX MBeans的属性展示性能数据,同时在性能低于可接受的阈值时发送JMX通知。因为JMX已经成为处理和监控Java应用的标准接口,提供Perf4J MBean开启了广泛的由第三方监控应用提供的功能。举例来说,我们Homeaway的IT部门标准化了Nagios和Cacti用于系统监控,这两个工具都支持把MBeans作为数据源查询。

  下面的log4j.xml文件示例显示了如何启用用于JMX的Perf4J appender:  

<?xml version="1.0" encoding="UTF-8"  ?>
    
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">


    
<log4j:configuration debug="false"  xmlns:log4j="http://jakarta.apache.org/log4j/">


        
<!-- Perf4J appenders -->
        
<!--
          This AsyncCoalescingStatisticsAppender groups  StopWatch
log messages
          into GroupedTimingStatistics messages which it  sends
on the
          file appender
and perf4jJmxAppender defined  below
        
-->
        
<appender name="CoalescingStatistics"
                   class
="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
            
<!--
              The TimeSlice
option  means timing logs are aggregated every 10 secs.
            
-->
            
<param name="TimeSlice"  value="10000"/>
            
<appender-ref  ref="fileAppender"/>
            
<appender-ref  ref="perf4jJmxAppender"/>
        
</appender>


        
<!--
          This file appender
is used to output aggregated  performance statistics
          in a format identical
to that produced by the LogParser.
        
-->
        
<appender name="fileAppender"  class="org.apache.log4j.FileAppender">
            
<param name="File"  value="perfStats.log"/>
            
<layout  class="org.apache.log4j.PatternLayout">
                
<param  name="ConversionPattern" value="%m%n"/>
            
</layout>
        
</appender>


        
<!--
          This JMX appender creates an MBean
and publishes  it to the platform MBean
server by
          default.
        
-->
        
<appender name="perf4jJmxAppender"
class
="org.perf4j.log4j.JmxAttributeStatisticsAppender">
            
<!-- The tag names whose  statistics should be exposed as MBean attributes. -->
            
<param name="TagNamesToExpose"  value="firstBlock,secondBlock"/>
            
<!--
              The  NotificationThresholds param configures the sending of JMX notifications
              when statistic values  exceed specified thresholds. This config states that
              the firstBlock max value  should be between
0 and 800ms, and the secondBlock max
              value should be less  than
1500 ms. You can also set thresholds on the Min,
              Mean, StdDev, Count
and  TPS statistics - e.g. firstBlockMean(<600).
            
-->
            
<param name="NotificationThresholds"  value="firstBlockMax(0-800),secondBlockMax(<1500)"/>
        
</appender>


        
<!-- Loggers -->
        
<!-- The Perf4J logger. -->
        
<logger name="org.perf4j.TimingLogger"  additivity="false">
            
<level  value="INFO"/>
            
<appender-ref  ref="CoalescingStatistics"/>
        
</logger>


        
<!--
          The root logger sends all
log statements EXCEPT  those sent to the perf4j
          logger
to System.out.
        
-->
        
<root>
            
<level  value="INFO"/>
            
<appender  name="console" class="org.apache.log4j.ConsoleAppender">
                
<layout  class="org.apache.log4j.SimpleLayout"/>
            
</appender>
        
</root>
    
</log4j:configuration>

  除了JMX插件,Perf4J也支持生成性能图表的画图appender,使用前端的Perf4J画图Servlet。定制的csv布局可以轻松的导入Excel或者其他电子表格应用。

0
相关文章