http://blog.csdn.net/wsscy2004/article/details/40423669
Java Metrics
Java Metrics是一个功能比较强大的java统计库,它的输出组件也很强大,帮我们做好了:
- 输出到Ganglia
- 输出到控制台
- 输出到JMX
- 输出Json
详细见:dropwizard.github.io/metrics/
依赖
添加依赖,如gradle:
<code class="hljs css" style="display: block; padding: 0.5em; background-color: rgb(35, 36, 31); color: rgb(248, 248, 242); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-tag" style="color: rgb(249, 38, 114); margin-top: 0px !important;">compile</span> "<span class="hljs-tag" style="color: rgb(249, 38, 114);">io</span><span class="hljs-class" style="color: rgb(166, 226, 46);">.dropwizard</span><span class="hljs-class" style="color: rgb(166, 226, 46);">.metrics</span><span class="hljs-pseudo" style="color: rgb(230, 219, 116);">:metrics-core</span><span class="hljs-pseudo" style="color: rgb(230, 219, 116);">:3</span><span class="hljs-class" style="color: rgb(166, 226, 46);">.1</span><span class="hljs-class" style="color: rgb(166, 226, 46);">.0</span>" <span class="hljs-tag" style="color: rgb(249, 38, 114);">compile</span> "<span class="hljs-tag" style="color: rgb(249, 38, 114);">io</span><span class="hljs-class" style="color: rgb(166, 226, 46);">.dropwizard</span><span class="hljs-class" style="color: rgb(166, 226, 46);">.metrics</span><span class="hljs-pseudo" style="color: rgb(230, 219, 116);">:metrics-ganglia</span><span class="hljs-pseudo" style="color: rgb(230, 219, 116);">:3</span><span class="hljs-class" style="color: rgb(166, 226, 46);">.1</span><span class="hljs-class" style="color: rgb(166, 226, 46);">.0</span>" </code>
如果需要ganglia输出功能,则需要metrics-ganglia包。我写的自动压测工具test-framework主要用失败计数,QPS统计。
统计调用频率
计数型的统计,比如计算失败次数,每次+1,则可以用Meter
<code class="hljs cs" style="display: block; padding: 0.5em; background-color: rgb(35, 36, 31); color: rgb(248, 248, 242); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(249, 38, 114); margin-top: 0px !important;">public</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">class</span> GetStarted { <span class="hljs-keyword" style="color: rgb(249, 38, 114);">static</span> final MetricRegistry metrics = <span class="hljs-keyword" style="color: rgb(249, 38, 114);">new</span> MetricRegistry(); <span class="hljs-keyword" style="color: rgb(249, 38, 114);">public</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">static</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">void</span> <span class="hljs-title" style="color: rgb(166, 226, 46);">main</span>(String args[]) { startReport(); <span class="hljs-comment" style="color: rgb(117, 113, 94);">//metrics:事件总数,平均速率,包含1分钟,5分钟,15分钟的速率</span> Meter requests = metrics.meter(<span class="hljs-string" style="color: rgb(230, 219, 116);">"requests"</span>); <span class="hljs-comment" style="color: rgb(117, 113, 94);">//计数一次</span> requests.mark(); wait5Seconds(); } <span class="hljs-keyword" style="color: rgb(249, 38, 114);">static</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">void</span> startReport() { <span class="hljs-comment" style="color: rgb(117, 113, 94);">//注册metrics,每个1秒打印metrics到控制台</span> ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .build(); reporter.start(<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>, TimeUnit.SECONDS); } <span class="hljs-keyword" style="color: rgb(249, 38, 114);">static</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">void</span> wait5Seconds() { <span class="hljs-keyword" style="color: rgb(249, 38, 114);">try</span> { Thread.sleep(<span class="hljs-number" style="color: rgb(174, 129, 255);">5</span>*<span class="hljs-number" style="color: rgb(174, 129, 255);">1000</span>); } <span class="hljs-keyword" style="color: rgb(249, 38, 114);">catch</span>(InterruptedException e) {} } } </code>
效果:
<code class="hljs diff" style="display: block; padding: 0.5em; background-color: rgb(35, 36, 31); color: rgb(248, 248, 242); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;">14-10-14 21:28:53 <span class="hljs-header" style="color: rgb(249, 38, 114); font-weight: bold; margin-top: 0px !important;">==============================================================</span> <span class="hljs-deletion" style="background-color: rgb(255, 200, 189); color: rgb(117, 113, 94);">-- Meters ----------------------------------------------------------------------</span> requests count = 1 mean rate = 1.00 events/second 1-minute rate = 0.00 events/second 5-minute rate = 0.00 events/second 15-minute rate = 0.00 events/second 14-10-14 21:28:54 <span class="hljs-header" style="color: rgb(249, 38, 114); font-weight: bold;">==============================================================</span> <span class="hljs-deletion" style="background-color: rgb(255, 200, 189); color: rgb(117, 113, 94);">-- Meters ----------------------------------------------------------------------</span> requests count = 1 mean rate = 0.51 events/second 1-minute rate = 0.00 events/second 5-minute rate = 0.00 events/second 15-minute rate = 0.00 events/second 14-10-14 21:28:55 <span class="hljs-header" style="color: rgb(249, 38, 114); font-weight: bold;">==============================================================</span> <span class="hljs-deletion" style="background-color: rgb(255, 200, 189); color: rgb(117, 113, 94);">-- Meters ----------------------------------------------------------------------</span> requests count = 1 mean rate = 0.33 events/second 1-minute rate = 0.00 events/second 5-minute rate = 0.00 events/second 15-minute rate = 0.00 events/second 14-10-14 21:28:56 <span class="hljs-header" style="color: rgb(249, 38, 114); font-weight: bold;">==============================================================</span> <span class="hljs-deletion" style="background-color: rgb(255, 200, 189); color: rgb(117, 113, 94);">-- Meters ----------------------------------------------------------------------</span> requests count = 1 mean rate = 0.25 events/second 1-minute rate = 0.00 events/second 5-minute rate = 0.00 events/second 15-minute rate = 0.00 events/second 14-10-14 21:28:57 <span class="hljs-header" style="color: rgb(249, 38, 114); font-weight: bold;">==============================================================</span> <span class="hljs-deletion" style="background-color: rgb(255, 200, 189); color: rgb(117, 113, 94);">-- Meters ----------------------------------------------------------------------</span> requests count = 1 mean rate = 0.20 events/second 1-minute rate = 0.00 events/second 5-minute rate = 0.00 events/second 15-minute rate = 0.00 events/second </code>
统计QPS
根据时间来计算qps,可以用Timer
<code class="hljs java" style="display: block; padding: 0.5em; background-color: rgb(35, 36, 31); color: rgb(248, 248, 242); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(249, 38, 114); margin-top: 0px !important;">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(102, 217, 239); margin-top: 0px !important;">class</span> <span class="hljs-title" style="color: rgb(166, 226, 46); font-style: italic;">TimerTest</span> </span>{ <span class="hljs-keyword" style="color: rgb(249, 38, 114);">static</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">final</span> MetricRegistry metrics = <span class="hljs-keyword" style="color: rgb(249, 38, 114);">new</span> MetricRegistry(); <span class="hljs-keyword" style="color: rgb(249, 38, 114);">private</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">static</span> Timer timer = metrics.timer(MetricRegistry.name(TimerTest.class, <span class="hljs-string" style="color: rgb(230, 219, 116);">"calculation-duration"</span>)); <span class="hljs-keyword" style="color: rgb(249, 38, 114);">public</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">static</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">void</span> <span class="hljs-title" style="color: rgb(166, 226, 46);">main</span>(String[] args) <span class="hljs-keyword" style="color: rgb(249, 38, 114);">throws</span> InterruptedException { <span class="hljs-comment" style="color: rgb(117, 113, 94);">// TODOAuto-generated method stub</span> startReport(); Random rn = <span class="hljs-keyword" style="color: rgb(249, 38, 114);">new</span> Random(); <span class="hljs-keyword" style="color: rgb(249, 38, 114);">while</span> (<span class="hljs-keyword" style="color: rgb(249, 38, 114);">true</span>) { <span class="hljs-comment" style="color: rgb(117, 113, 94);">//统计开始</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">final</span> Timer.Context context = timer.time(); <span class="hljs-keyword" style="color: rgb(249, 38, 114);">int</span> sleepTime = rn.nextInt(<span class="hljs-number" style="color: rgb(174, 129, 255);">2000</span>); Thread.sleep(sleepTime); System.out.println(<span class="hljs-string" style="color: rgb(230, 219, 116);">"处理耗时:"</span> + sleepTime); <span class="hljs-comment" style="color: rgb(117, 113, 94);">//统计结束</span> context.stop(); } } <span class="hljs-keyword" style="color: rgb(249, 38, 114);">static</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114);">void</span> startReport() { <span class="hljs-comment" style="color: rgb(117, 113, 94);">//注册metrics,每个1秒打印metrics到控制台</span> ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .build(); reporter.start(<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>, TimeUnit.SECONDS); } } </code>
结果:
<code class="hljs diff" style="display: block; padding: 0.5em; background-color: rgb(35, 36, 31); color: rgb(248, 248, 242); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;">处理耗时:996 14-10-14 22:40:34 <span class="hljs-header" style="color: rgb(249, 38, 114); font-weight: bold; margin-top: 0px !important;">==============================================================</span> <span class="hljs-deletion" style="background-color: rgb(255, 200, 189); color: rgb(117, 113, 94);">-- Timers ----------------------------------------------------------------------</span> com.edwardsbean.test.TimerTest.calculation-duration count = 1 mean rate = 0.91 calls/second 1-minute rate = 0.00 calls/second 5-minute rate = 0.00 calls/second 15-minute rate = 0.00 calls/second min = 995.91 milliseconds max = 995.91 milliseconds mean = 995.91 milliseconds stddev = 0.00 milliseconds median = 995.91 milliseconds 75% <= 995.91 milliseconds 95% <= 995.91 milliseconds 98% <= 995.91 milliseconds 99% <= 995.91 milliseconds 99.9% <= 995.91 milliseconds 14-10-14 22:40:35 <span class="hljs-header" style="color: rgb(249, 38, 114); font-weight: bold;">==============================================================</span> <span class="hljs-deletion" style="background-color: rgb(255, 200, 189); color: rgb(117, 113, 94);">-- Timers ----------------------------------------------------------------------</span> com.edwardsbean.test.TimerTest.calculation-duration count = 1 mean rate = 0.48 calls/second 1-minute rate = 0.00 calls/second 5-minute rate = 0.00 calls/second 15-minute rate = 0.00 calls/second min = 995.91 milliseconds max = 995.91 milliseconds mean = 995.91 milliseconds stddev = 0.00 milliseconds median = 995.91 milliseconds 75% <= 995.91 milliseconds 95% <= 995.91 milliseconds 98% <= 995.91 milliseconds 99% <= 995.91 milliseconds 99.9% <= 995.91 milliseconds </code>
关于输出
每一个输出组件都有一个对应的Reporter主类,比如Ganglia:
<code class="hljs cpp" style="display: block; padding: 0.5em; background-color: rgb(35, 36, 31); color: rgb(248, 248, 242); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;">GMetric ganglia = <span class="hljs-keyword" style="color: rgb(249, 38, 114); margin-top: 0px !important;">new</span> GMetric(address[<span class="hljs-number" style="color: rgb(174, 129, 255);">0</span>].getHostName(), address[<span class="hljs-number" style="color: rgb(174, 129, 255);">0</span>].getPort(), GMetric.UDPAddressingMode.MULTICAST, <span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>); GangliaReporter gangliaReporter = GangliaReporter.forRegistry(metricRegistry) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .build(ganglia); <span class="hljs-comment" style="color: rgb(117, 113, 94);">//开始汇报</span> gangliaReporter.start(<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>, TimeUnit.SECONDS); </code>
而输出控制台的Reporter
<code class="hljs objectivec" style="display: block; padding: 0.5em; background-color: rgb(35, 36, 31); color: rgb(248, 248, 242); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; margin-top: 0px !important; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); margin-top: 0px !important;">###</span> ConsoleReporter reporter = ConsoleReporter<span class="hljs-variable" style="color: rgb(102, 0, 102);">.forRegistry</span>(metrics) <span class="hljs-variable" style="color: rgb(102, 0, 102);">.convertRatesTo</span>(TimeUnit<span class="hljs-variable" style="color: rgb(102, 0, 102);">.SECONDS</span>) <span class="hljs-variable" style="color: rgb(102, 0, 102);">.convertDurationsTo</span>(TimeUnit<span class="hljs-variable" style="color: rgb(102, 0, 102);">.MILLISECONDS</span>) <span class="hljs-variable" style="color: rgb(102, 0, 102);">.build</span>(); reporter<span class="hljs-variable" style="color: rgb(102, 0, 102);">.start</span>(<span class="hljs-number" style="color: rgb(174, 129, 255);">1</span>, TimeUnit<span class="hljs-variable" style="color: rgb(102, 0, 102);">.SECONDS</span>);</code>
<code style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; color: inherit; background-color: transparent; margin-top: 0px !important;"></code>
版权声明:本文为博主原创文章,未经博主允许不得转载。