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);
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值