首页 > 代码库 > Java Metrics
Java Metrics
Java Metrics
Java Metrics是一个功能比較强大的java统计库,它的输出组件也非常强大,帮我们做好了:
- 输出到Ganglia
- 输出到控制台
- 输出到JMX
- 输出Json
具体见:dropwizard.github.io/metrics/
依赖
加入?依赖,如gradle:
compile "io.dropwizard.metrics:metrics-core:3.1.0"
compile "io.dropwizard.metrics:metrics-ganglia:3.1.0"
假设须要ganglia输出功能,则须要metrics-ganglia包。我写的自己主动压測工具test-framework主要用失败计数,QPS统计。
统计调用频率
计数型的统计,比方计算失败次数,每次+1,则能够用Meter
public class GetStarted {
static final MetricRegistry metrics = new MetricRegistry();
public static void main(String args[]) {
startReport();
//metrics:事件总数,平均速率,包括1分钟,5分钟,15分钟的速率
Meter requests = metrics.meter("requests");
//计数一次
requests.mark();
wait5Seconds();
}
static void startReport() {
//注冊metrics,每一个1秒打印metrics到控制台
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.SECONDS);
}
static void wait5Seconds() {
try {
Thread.sleep(5*1000);
}
catch(InterruptedException e) {}
}
}
效果:
14-10-14 21:28:53 ==============================================================
-- Meters ----------------------------------------------------------------------
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 ==============================================================
-- Meters ----------------------------------------------------------------------
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 ==============================================================
-- Meters ----------------------------------------------------------------------
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 ==============================================================
-- Meters ----------------------------------------------------------------------
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 ==============================================================
-- Meters ----------------------------------------------------------------------
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
统计QPS
依据时间来计算qps,能够用Timer
public class TimerTest {
static final MetricRegistry metrics = new MetricRegistry();
private static Timer timer = metrics.timer(MetricRegistry.name(TimerTest.class, "calculation-duration"));
public static void main(String[] args) throws InterruptedException {
// TODOAuto-generated method stub
startReport();
Random rn = new Random();
while (true) {
//统计開始
final Timer.Context context = timer.time();
int sleepTime = rn.nextInt(2000);
Thread.sleep(sleepTime);
System.out.println("处理耗时:" + sleepTime);
//统计结束
context.stop();
}
}
static void startReport() {
//注冊metrics,每一个1秒打印metrics到控制台
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.SECONDS);
}
}
结果:
处理耗时:996
14-10-14 22:40:34 ==============================================================
-- Timers ----------------------------------------------------------------------
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 ==============================================================
-- Timers ----------------------------------------------------------------------
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
关于输出
每个输出组件都有一个相应的Reporter主类,比方Ganglia:
GMetric ganglia = new GMetric(address[0].getHostName(), address[0].getPort(), GMetric.UDPAddressingMode.MULTICAST, 1);
GangliaReporter gangliaReporter = GangliaReporter.forRegistry(metricRegistry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build(ganglia);
//開始汇报
gangliaReporter.start(1, TimeUnit.SECONDS);
而输出控制台的Reporter
###
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.SECONDS);
Java Metrics
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。