Java Metrics

简介: <h2 class="note-title" style="font-family:'Helvetica Neue',Arial,'Hiragino Sans GB',STHeiti,'Microsoft YaHei','WenQuanYi Micro Hei',SimSun,Song,sans-serif; line-height:1.1; color:rgb(22,32,41); ma

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);
目录
相关文章
|
8月前
|
存储 监控 Java
【深度挖掘Java性能调优】「底层技术原理体系」深入探索Java服务器性能监控Metrics框架的实现原理分析(Counter篇)
【深度挖掘Java性能调优】「底层技术原理体系」深入探索Java服务器性能监控Metrics框架的实现原理分析(Counter篇)
185 0
|
8月前
|
监控 算法 Java
【深度挖掘Java性能调优】「底层技术原理体系」深入探索Java服务器性能监控Metrics框架的实现原理分析(Gauge和Histogram篇)
【深度挖掘Java性能调优】「底层技术原理体系」深入探索Java服务器性能监控Metrics框架的实现原理分析(Gauge和Histogram篇)
103 0
|
7月前
|
运维 监控 Java
性能监控之 Java Metrics 度量包
【6月更文挑战10天】标题性能监控之 Java Metrics 度量包
168 2
java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
591 0
|
15天前
|
监控 Java
java异步判断线程池所有任务是否执行完
通过上述步骤,您可以在Java中实现异步判断线程池所有任务是否执行完毕。这种方法使用了 `CompletionService`来监控任务的完成情况,并通过一个独立线程异步检查所有任务的执行状态。这种设计不仅简洁高效,还能确保在大量任务处理时程序的稳定性和可维护性。希望本文能为您的开发工作提供实用的指导和帮助。
72 17
|
26天前
|
Java
Java—多线程实现生产消费者
本文介绍了多线程实现生产消费者模式的三个版本。Version1包含四个类:`Producer`(生产者)、`Consumer`(消费者)、`Resource`(公共资源)和`TestMain`(测试类)。通过`synchronized`和`wait/notify`机制控制线程同步,但存在多个生产者或消费者时可能出现多次生产和消费的问题。 Version2将`if`改为`while`,解决了多次生产和消费的问题,但仍可能因`notify()`随机唤醒线程而导致死锁。因此,引入了`notifyAll()`来唤醒所有等待线程,但这会带来性能问题。
Java—多线程实现生产消费者
|
11天前
|
缓存 安全 算法
Java 多线程 面试题
Java 多线程 相关基础面试题
|
28天前
|
安全 Java Kotlin
Java多线程——synchronized、volatile 保障可见性
Java多线程中,`synchronized` 和 `volatile` 关键字用于保障可见性。`synchronized` 保证原子性、可见性和有序性,通过锁机制确保线程安全;`volatile` 仅保证可见性和有序性,不保证原子性。代码示例展示了如何使用 `synchronized` 和 `volatile` 解决主线程无法感知子线程修改共享变量的问题。总结:`volatile` 确保不同线程对共享变量操作的可见性,使一个线程修改后,其他线程能立即看到最新值。
|
28天前
|
消息中间件 缓存 安全
Java多线程是什么
Java多线程简介:本文介绍了Java中常见的线程池类型,包括`newCachedThreadPool`(适用于短期异步任务)、`newFixedThreadPool`(适用于固定数量的长期任务)、`newScheduledThreadPool`(支持定时和周期性任务)以及`newSingleThreadExecutor`(保证任务顺序执行)。同时,文章还讲解了Java中的锁机制,如`synchronized`关键字、CAS操作及其实现方式,并详细描述了可重入锁`ReentrantLock`和读写锁`ReadWriteLock`的工作原理与应用场景。