What is perf4j?-------------------- Perf4j is a simple service monitoring framework that can be used to monitor system requirements, such as real-time system throughput and system response time.
Perf4j generates monitoring charts---------------------------- Charts generated by pef4j support mean, Min, Max, stddev, count and TPS.
- Mean average response time
- Min minimum response time
- Max maximum response time
- Count count
- TPS Throughput
The metric chart style is as follows:
Use and configuration of perf4j-----------------------
Here only the use of perf4j and log4j, for the use of systems other than log4j or go to the official website to see the introduction of http://perf4j.codehaus.org/, insert the following configuration in log4j. xml
<! -- Hitting record -->
<Logger name = "org. perf4j. timinglogger "additistics =" false "> <level value =" info "/> <appender-ref =" coalescingstatistics "/> </logger>
<! -- A specific output chart corresponds to a configuration chart --> <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 = "graphsfileappender"/> </appender>
<Appender name = "executiontps" class = "org. perf4j. log4j. graphingstatisticsappender "> <Param name =" graphtype "value =" TPS "/> <Param name =" tagnamestograph "value =" firstblock, /order/commit "/> <appender-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 = "graphsfileappender"/> </appender>
<! -Analyze the result obtained after hitting, used to draw the chart node --> <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>
<! -- Charts to be displayed --> <appender name = "coalescingstatistics" class = "org. perf4j. log4j. asynccoalescingstatisticsappender "> <Param name =" timeslice "value =" 30000 "/> <appender-ref =" executiontps "> </appender-ref> <appender-ref =" executiontimes "> </appender-ref> <appender-ref =" executioncount "> </appender-ref> <appender-ref =" executionmax "> </appender-ref> <appender-ref = "executionstddev"> </appender-ref> <appender-ref = "executionslow"> </appender-ref> </appender>
Configure the following information in the web. xml file of your web project:
<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 shocould 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>
Add an interceptor in the springmvc project Column
Public class performanceinterceptor extends handlerinterceptoradapter {
Protected logger = logger. getlogger (this. getclass ());
@ Override public Boolean prehandle (httpservletrequest request, httpservletresponse response, Object Handler) throws exception {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) throws exception {super. posthandle (request, response, handler, modelandview );}
@ Override public void aftercompletion (httpservletrequest request, httpservletresponse response, Object Handler, exception ex) throws exception {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 );}}
After the configuration is complete, visit http://xxxx.com/admin. the googleserver has the capability, because the graphic table uses the googleon-line chartapi
Use perf4j for service monitoring