使用metrics统计接口tps

metrics的简单介绍

metrics是一种性能指标工具,有很多开源工具使用之来来作为其性能监控,如Hadoop,Kafka,Spark,JStorm等。

metrics使用最主要有三个东西:

  1. Metric Registries
  2. Metrics 度量指标的类型
  3. Metrics Report 数据展示方案

metricsRegistry

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

每一个 metric 都有它独一无二的名字,Metrics 中使用句点名字,如 com.example.Queue.size。当你在 com.example.Queue 下有两个 metric 实例,可以指定地更具体:com.example.Queue.requests.size 和 com.example.Queue.response.size 。使用MetricRegistry类,可以非常方便地生成名字。

五种Metrics类型

guages

最简单的度量指标,只有一个简单的返回值,或者叫瞬时状态,例如,我们想衡量一个待处理队列中任务的个数。

Counters

Counter 就是计数器,Counter 只是用 Gauge 封装了 AtomicLong 。

Meters

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

Histograms

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

Timers

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

怎么使用暂时不给出代码了,先来看看如何统计接口的tps

Metrics Report

metrics提供了丰富的Report,如JMX、Console、CSV、SLF4J以及图形、HTTP等等

统计接口tps

我采取的办法是使用spring的interceptor与Meters ,Report使用的是ConsoleReport,代码如下

package lew.bing.exp.spr;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by 刘国兵 on 2018/7/14.
 */
public class MetricsInterceptor implements HandlerInterceptor {

    private Map metricMap = new HashMap<>();
    private MetricRegistry metrics = new MetricRegistry();

    public MetricsInterceptor() {
        metricMap.put("/",metrics.meter("/"));
        ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
        reporter.start(1, TimeUnit.SECONDS);// 1s报道一次
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String servletPath = request.getServletPath();
        if (metricMap.containsKey(servletPath)) {
            metricMap.get(servletPath).mark();// 统计tps
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 可以统计失败以及成功次数
    }
}

连续点击浏览器请求接口,控制台打印如下:

18-7-14 18:36:08 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 3
         mean rate = 0.60 events/second
     1-minute rate = 0.60 events/second
     5-minute rate = 0.60 events/second
    15-minute rate = 0.60 events/second


18-7-14 18:36:09 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 10
         mean rate = 1.67 events/second
     1-minute rate = 0.60 events/second
     5-minute rate = 0.60 events/second
    15-minute rate = 0.60 events/second


18-7-14 18:36:10 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 15
         mean rate = 2.14 events/second
     1-minute rate = 0.60 events/second
     5-minute rate = 0.60 events/second
    15-minute rate = 0.60 events/second


18-7-14 18:36:11 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 21
         mean rate = 2.62 events/second
     1-minute rate = 0.60 events/second
     5-minute rate = 0.60 events/second
    15-minute rate = 0.60 events/second


18-7-14 18:36:12 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 27
         mean rate = 3.00 events/second
     1-minute rate = 0.60 events/second
     5-minute rate = 0.60 events/second
    15-minute rate = 0.60 events/second


18-7-14 18:36:13 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 33
         mean rate = 3.30 events/second
     1-minute rate = 1.03 events/second
     5-minute rate = 0.69 events/second
    15-minute rate = 0.63 events/second


18-7-14 18:36:14 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 38
         mean rate = 3.45 events/second
     1-minute rate = 1.03 events/second
     5-minute rate = 0.69 events/second
    15-minute rate = 0.63 events/second


18-7-14 18:36:15 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 44
         mean rate = 3.66 events/second
     1-minute rate = 1.03 events/second
     5-minute rate = 0.69 events/second
    15-minute rate = 0.63 events/second


18-7-14 18:36:16 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 50
         mean rate = 3.84 events/second
     1-minute rate = 1.03 events/second
     5-minute rate = 0.69 events/second
    15-minute rate = 0.63 events/second


18-7-14 18:36:17 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 55
         mean rate = 3.93 events/second
     1-minute rate = 1.03 events/second
     5-minute rate = 0.69 events/second
    15-minute rate = 0.63 events/second


18-7-14 18:36:18 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 61
         mean rate = 4.06 events/second
     1-minute rate = 1.40 events/second
     5-minute rate = 0.77 events/second
    15-minute rate = 0.66 events/second


18-7-14 18:36:19 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 67
         mean rate = 4.19 events/second
     1-minute rate = 1.40 events/second
     5-minute rate = 0.77 events/second
    15-minute rate = 0.66 events/second


18-7-14 18:36:20 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 72
         mean rate = 4.23 events/second
     1-minute rate = 1.40 events/second
     5-minute rate = 0.77 events/second
    15-minute rate = 0.66 events/second


18-7-14 18:36:21 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 78
         mean rate = 4.33 events/second
     1-minute rate = 1.40 events/second
     5-minute rate = 0.77 events/second
    15-minute rate = 0.66 events/second


18-7-14 18:36:22 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 83
         mean rate = 4.37 events/second
     1-minute rate = 1.40 events/second
     5-minute rate = 0.77 events/second
    15-minute rate = 0.66 events/second


18-7-14 18:36:23 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 89
         mean rate = 4.45 events/second
     1-minute rate = 1.73 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:24 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 4.38 events/second
     1-minute rate = 1.73 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:25 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 4.18 events/second
     1-minute rate = 1.73 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:26 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 4.00 events/second
     1-minute rate = 1.73 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:27 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 3.83 events/second
     1-minute rate = 1.73 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:28 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 3.68 events/second
     1-minute rate = 1.64 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:29 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 3.54 events/second
     1-minute rate = 1.64 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:30 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 3.41 events/second
     1-minute rate = 1.64 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:31 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 3.29 events/second
     1-minute rate = 1.64 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:32 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 3.17 events/second
     1-minute rate = 1.64 events/second
     5-minute rate = 0.85 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:33 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 3.07 events/second
     1-minute rate = 1.51 events/second
     5-minute rate = 0.83 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:34 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 2.97 events/second
     1-minute rate = 1.51 events/second
     5-minute rate = 0.83 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:35 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 2.87 events/second
     1-minute rate = 1.51 events/second
     5-minute rate = 0.83 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:36 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 2.79 events/second
     1-minute rate = 1.51 events/second
     5-minute rate = 0.83 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:37 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 2.71 events/second
     1-minute rate = 1.51 events/second
     5-minute rate = 0.83 events/second
    15-minute rate = 0.68 events/second


18-7-14 18:36:38 ===============================================================

-- Meters ----------------------------------------------------------------------
/
             count = 92
         mean rate = 2.63 events/second
     1-minute rate = 1.39 events/second
     5-minute rate = 0.82 events/second
    15-minute rate = 0.68 events/second

简单的使用metrics统计tps就介绍到这,metrics还有健康检查,可以用来检测连接数据库是否可用等,感兴趣的同学可以多了解了解。

你可能感兴趣的:(使用metrics统计接口tps)