zoukankan      html  css  js  c++  java
  • Java Metrics系统性能监控工具

    Metrics是一个Java库,可以对系统进行监控,统计一些系统的性能指标。

    比如一个系统后台服务,我们可能需要了解一下下面的一些情况:
    1、每秒钟的请求数是多少(TPS)?
    2、平均每个请求处理的时间?
    3、请求处理的最长耗时?
    4、等待处理的请求队列长度?
    5、又或者一个缓存服务:缓存的命中率?平均查询缓存的时间?

    基本上每一个服务、应用都需要做一个监控系统,这需要尽量以少量的代码,实现统计某类数据的功能。

    Metric Registries

    MetricRegistry类是Metrics的核心,它是存放应用中所有metrics的容器,也是我们使用 Metrics 库的起点。

    MetricRegistry registry = new MetricRegistry();
    

    Metrics 数据展示

    Metrics 提供了 Report 接口,用于展示 metrics 获取到的统计数据。metrics-core中主要实现了四种 reporter: JMX ,console, SLF4J, 和 CSV。 在本文的例子中,我们使用 ConsoleReporter 。

    Metrics的五种类型

    Gauges

    最简单的度量指标,只有一个简单的返回值,例如,我们想衡量一个待处理队列中任务的个数,代码如下:

    package com.zyh.maven.metricsdemo;
    
    import com.codahale.metrics.ConsoleReporter;
    import com.codahale.metrics.Gauge;
    import com.codahale.metrics.MetricRegistry;
    
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.concurrent.TimeUnit;
    
    public class GaugeTest {
    
        public static Queue<String> q = new LinkedList<String>();
    
        public static void main(String[] args) throws InterruptedException {
    
            MetricRegistry metricRegistry = new MetricRegistry();
            ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();
            reporter.start(1, TimeUnit.SECONDS);
    
            metricRegistry.register(MetricRegistry.name(GaugeTest.class, "queue", "size"),
                    new Gauge<Integer>(){
                        @Override
                        public Integer getValue() {
                            return q.size();
                        }
                    });
    
            while (true)
            {
                Thread.sleep(1000);
                q.add("张永辉");
            }
        }
    }
    

    运行结果

    18-2-5 14:36:28 ================================================================
    
    -- Gauges ----------------------------------------------------------------------
    com.zyh.maven.metricsdemo.GaugeTest.queue.size
                 value = 1
    
    
    18-2-5 14:36:29 ================================================================
    
    -- Gauges ----------------------------------------------------------------------
    com.zyh.maven.metricsdemo.GaugeTest.queue.size
                 value = 1
    
    
    18-2-5 14:36:30 ================================================================
    
    -- Gauges ----------------------------------------------------------------------
    com.zyh.maven.metricsdemo.GaugeTest.queue.size
                 value = 2
    
    
    18-2-5 14:36:31 ================================================================
    
    -- Gauges ----------------------------------------------------------------------
    com.zyh.maven.metricsdemo.GaugeTest.queue.size
                 value = 3
    

    Counters

    Counter 就是计数器,Counter 只是用 Gauge 封装了 AtomicLong ,我们可以使用如下的方法获得队列大小,代码如下:

    package com.zyh.maven.metricsdemo;
    
    import com.codahale.metrics.ConsoleReporter;
    import com.codahale.metrics.Counter;
    import com.codahale.metrics.MetricRegistry;
    
    import java.util.Queue;
    import java.util.Random;
    import java.util.concurrent.LinkedBlockingDeque;
    import java.util.concurrent.TimeUnit;
    
    public class CounterTest {
    
        public static Queue<String> q = new LinkedBlockingDeque<String>();
    
        public static Counter pendingJobs;
    
        public static Random random = new Random();
    
        public static void addJob(String job)
        {
            pendingJobs.inc();
            q.offer(job);
        }
    
        public static String takeJob()
        {
            pendingJobs.dec();
            return q.poll();
        }
    
        public static void main(String[] args) throws InterruptedException {
    
            MetricRegistry registry = new MetricRegistry();
            ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
            reporter.start(1, TimeUnit.SECONDS);
    
            pendingJobs = registry.counter(MetricRegistry.name(Queue.class, "pending-jobs", "size"));
    
            int num = 1;
            while(true)
            {
                Thread.sleep(200);
                if(random.nextDouble() > 0.7)
                {
                    String job = takeJob();
                    System.out.println("take job :" + job);
                }else{
                    String job = "Job-" + num;
                    addJob(job);
                    System.out.println("add Job :" + job);
                }
                num++;
            }
        }
    }
    

    运行结果

    take job :Job-14
    add Job :Job-26
    add Job :Job-27
    add Job :Job-28
    add Job :Job-29
    18-2-5 14:39:58 ================================================================
    
    -- Counters --------------------------------------------------------------------
    java.util.Queue.pending-jobs.size
                 count = 11
    
    
    take job :Job-16
    add Job :Job-31
    add Job :Job-32
    take job :Job-17
    take job :Job-18
    18-2-5 14:39:59 ================================================================
    
    -- Counters --------------------------------------------------------------------
    java.util.Queue.pending-jobs.size
                 count = 10
    

    Meters

    Meter度量一系列事件发生的速率(rate),例如TPS。Meters会统计最近1分钟,5分钟,15分钟,还有全部时间的速率。

    package com.zyh.maven.metricsdemo;
    
    import com.codahale.metrics.ConsoleReporter;
    import com.codahale.metrics.Meter;
    import com.codahale.metrics.MetricRegistry;
    
    import java.util.Random;
    import java.util.concurrent.TimeUnit;
    
    public class MeterTest {
    
        public static Random random = new Random();
    
        public static void request(Meter meter)
        {
            System.out.println("request");
            meter.mark();
        }
    
        public static void request(Meter meter, int n)
        {
            while(n > 0)
            {
                request(meter);
                n--;
            }
        }
        public static void main(String[] args) throws InterruptedException {
    
            MetricRegistry registry = new MetricRegistry();
            ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
            reporter.start(1, TimeUnit.SECONDS);
    
            Meter meterTps = registry.meter(MetricRegistry.name(MeterTest.class, "request", "tps"));
    
            while(true)
            {
                request(meterTps, random.nextInt(5));
                Thread.sleep(1000);
            }
        }
    }
    

    运行结果

    18-2-5 14:42:44 ================================================================
    
    -- Meters ----------------------------------------------------------------------
    com.zyh.maven.metricsdemo.MeterTest.request.tps
                 count = 16
             mean rate = 2.67 events/second
         1-minute rate = 3.20 events/second
         5-minute rate = 3.20 events/second
        15-minute rate = 3.20 events/second
    
    
    request
    request
    request
    request
    18-2-5 14:42:45 ================================================================
    
    -- Meters ----------------------------------------------------------------------
    com.zyh.maven.metricsdemo.MeterTest.request.tps
                 count = 20
             mean rate = 2.86 events/second
         1-minute rate = 3.20 events/second
         5-minute rate = 3.20 events/second
        15-minute rate = 3.20 events/second
    

    Histograms

    Histogram统计数据的分布情况。比如最小值,最大值,中间值,还有中位数,75百分位,90百分位,95百分位,98百分位,99百分位,和 99.9百分位的值(percentiles)。

    package com.zyh.maven.metricsdemo;
    
    import com.codahale.metrics.ConsoleReporter;
    import com.codahale.metrics.ExponentiallyDecayingReservoir;
    import com.codahale.metrics.Histogram;
    import com.codahale.metrics.MetricRegistry;
    
    import java.util.Random;
    import java.util.concurrent.TimeUnit;
    
    public class HistogramsTest {
    
        public static Random random = new Random();
    
        public static void main(String[] args) throws InterruptedException {
    
            MetricRegistry registry = new MetricRegistry();
            ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
            reporter.start(1, TimeUnit.SECONDS);
    
            Histogram histogram = new Histogram(new ExponentiallyDecayingReservoir());
            registry.register(MetricRegistry.name(HistogramsTest.class, "request", "histogram"), histogram);
    
            while (true)
            {
                Thread.sleep(1000);
                histogram.update(random.nextInt(100000));
            }
        }
    }
    

    运行结果

    18-2-5 14:45:45 ================================================================
    
    -- Histograms ------------------------------------------------------------------
    com.zyh.maven.metricsdemo.HistogramsTest.request.histogram
                 count = 8
                   min = 8676
                   max = 94954
                  mean = 36405.28
                stddev = 27543.74
                median = 28243.00
                  75% <= 58814.00
                  95% <= 94954.00
                  98% <= 94954.00
                  99% <= 94954.00
                99.9% <= 94954.00
    
    
    18-2-5 14:45:46 ================================================================
    
    -- Histograms ------------------------------------------------------------------
    com.zyh.maven.metricsdemo.HistogramsTest.request.histogram
                 count = 9
                   min = 8676
                   max = 94954
                  mean = 39131.65
                stddev = 26922.72
                median = 28243.00
                  75% <= 58814.00
                  95% <= 94954.00
                  98% <= 94954.00
                  99% <= 94954.00
                99.9% <= 94954.00
    

    Timers

    Timer其实是 Histogram 和 Meter 的结合, histogram 某部分代码/调用的耗时, meter统计TPS。

    package com.zyh.maven.metricsdemo;
    
    import com.codahale.metrics.ConsoleReporter;
    import com.codahale.metrics.MetricRegistry;
    import com.codahale.metrics.Timer;
    
    import java.util.Random;
    import java.util.concurrent.TimeUnit;
    
    public class TimerTest {
    
        public static Random random = new Random();
    
        public static void main(String[] args) throws InterruptedException {
    
            MetricRegistry registry = new MetricRegistry();
            ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
            reporter.start(1, TimeUnit.SECONDS);
    
            Timer timer = registry.timer(MetricRegistry.name(TimerTest.class, "get-latency"));
    
            Timer.Context ctx;
    
            while (true)
            {
                ctx = timer.time();
                Thread.sleep(random.nextInt(1000));
                ctx.stop();
            }
        }
    }
    

    运行结果

    18-2-5 14:48:30 ================================================================
    
    -- Timers ----------------------------------------------------------------------
    com.zyh.maven.metricsdemo.TimerTest.get-latency
                 count = 30
             mean rate = 2.15 calls/second
         1-minute rate = 2.02 calls/second
         5-minute rate = 2.00 calls/second
        15-minute rate = 2.00 calls/second
                   min = 22.82 milliseconds
                   max = 987.23 milliseconds
                  mean = 439.66 milliseconds
                stddev = 263.14 milliseconds
                median = 421.99 milliseconds
                  75% <= 582.73 milliseconds
                  95% <= 926.66 milliseconds
                  98% <= 987.23 milliseconds
                  99% <= 987.23 milliseconds
                99.9% <= 987.23 milliseconds
    

    上面写了几个demo尝试用了一下Metrics,在这里记录一下!



    作者:雨林木风博客
    链接:https://www.jianshu.com/p/e5bba03fd64f
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    finally语句
    安卓模拟器一直黑屏
    Failed to load C:SDKandroid-sdk-windowsuild-tools27.0.2libdx.jar
    eclipse不能自动生成R文件经验总结
    空间复杂度
    Japplet与Applet的区别
    二叉树有关公式
    子程序调用使用的数据结构
    IIC
    按键消抖
  • 原文地址:https://www.cnblogs.com/sidesky/p/13177242.html
Copyright © 2011-2022 走看看