Java Metrics是一个功能比较强大的java统计库,它的输出组件也很强大,帮我们做好了:
详细见: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,可以用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);