Java Metrics

Java Metrics

Java Metrics是一個功能比較強大的java統計庫,它的輸出組件也很是強大,幫咱們作好了:css

  • 輸出到Ganglia
  • 輸出到控制檯
  • 輸出到JMX
  • 輸出Json

具體見:dropwizard.github.io/metrics/java

依賴

加入�依賴,如gradle:git

compile "io.dropwizard.metrics:metrics-core:3.1.0"
    compile "io.dropwizard.metrics:metrics-ganglia:3.1.0"

假設需要ganglia輸出功能,則需要metrics-ganglia包。我寫的本身主動壓測工具test-framework主要用失敗計數,QPS統計。github

統計調用頻率

計數型的統計,比方計算失敗次數,每次+1,則可以用Meterdom

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,可以用Timergradle

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);
    }

}

結果:ui

處理耗時: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:spa

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);

而輸出控制檯的Reportercode

###
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
reporter.start(1, TimeUnit.SECONDS);
相關文章
相關標籤/搜索