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<String,Meter> 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还有健康检查,可以用来检测连接数据库是否可用等,感兴趣的同学可以多了解了解。

作者:247609107a39
链接:https://www.jianshu.com/p/f9b911a651ff
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

使用metrics统计接口tps的更多相关文章

  1. 使用awk根据多维度统计系统tps

    业务简单日志格式: [2017-08-28 01:00:00.523]transfer_search,7001045,1708271100083774377,20170827,,,,,,,,,,001 ...

  2. 齐博x1万能数据统计接口

    为何叫万能数据统计接口呢?因为可以调用全站任何数据表的数据总条数,并且可以设置查询条件http://qb.net/index.php/index/wxapp.count.html?table=memb ...

  3. 通信统计接口字段填充内容shell脚本

    现在通信行业上班,最麻烦的就是计算各个协议接口字段填充的内容是否正确.经历了小白似的一个个字段统计在到单个接口所有字段统计,然后到现在的多个接口一次统计.在大大节省了时间的同时,让自己有更充足的时间来 ...

  4. nginx 日志统计接口每个小时访问量

    指定时间段增量统计nginx日志不同接口的访问量: #!/bin/bash#此脚本用于统计nginx日志当前时间15分钟之内不同接口(URL)的访问量统计LOG=/usr/local/nginx/lo ...

  5. awk 按小时 统计接口调用次数

    #统计所有接口总量awk -F ' ' '{a[$7]++} END{for(i in a){print i,a[i] | "sort -r -k 7"}}' accesslog/ ...

  6. 从线上日志统计接口访问量QPS

    这一阵子在面试,连续遇到好几家(大小厂都有)问我的项目线上qps的情况了,说实话,我作为一个大头兵,本来没关注过这个数据,只能含混地给个"大概.也许"的回答. 回来之后,我决定对业 ...

  7. 百度统计接口demo中错误

    百度统计接口中的demo(PHP版本)下载下来配置后运行出错,应该是编写demo时用的php版本比较低吧,作如下几处修改就好了 一:把CURLOPT_SSL_VERIFYHOST的值改为2,因为1版本 ...

  8. springMVC Aspect AOP 接口耗时统计

    在接口开发中,我们通常需要统计接口耗时,为后续接口性能做统计.在springMVC中可以用它的aop来记录日志. 1.在spring配置文件中开启AOP <!--*************** ...

  9. 在网页中运用统计Web Service接口

    (2017-02-10 银河统计) 在"统计随机数及临界值Web Service接口"一文中介绍了常用统计分布四类Web Service接口(随机数.分位数.密度函数和累积分布函数 ...

随机推荐

  1. .NET Core 2.1 源码学习:看 SocketsHttpHandler 如何在异步方法中连接 Socket

    在 .NET Core 2.1 中,System.Net.Sockets 的性能有了很大的提升,最好的证明是 Kestrel 与 HttpClient 都改为使用 System.Net.Sockets ...

  2. Codeforces 1114 - A/B/C/D/E/F - (Undone)

    链接:http://codeforces.com/contest/1114 A - Got Any Grapes? 题意:甲乙丙三个人吃葡萄,总共有三种葡萄:绿葡萄.紫葡萄和黑葡萄,甲乙丙三个人至少要 ...

  3. InnoDB中锁的模式

    Ⅰ.总览 S行级共享锁 lock in share mode X行级排它锁 增删改 IS意向共享锁 IX意向排他锁 AI自增锁 Ⅱ.锁之间的兼容性 兼 X IX S IS X × × × × IX × ...

  4. Cordova打包Apk

    ======================== apk打包 ======================== keytool -genkey -v -keystore release-key.key ...

  5. Torchvision 源码安装[Ubuntu]

    git clone https://github.com/pytorch/vision.git pip install pillow cd vision python setup.py install

  6. 关于 spring MVC 配置自动扫描中 use-default-filters 属性

    1.springMVC 设置扫描 Bean 的两种常见写法 1.1.先看第一种常见的配置:默认 <!-- 配置Controller扫描 --> <context:component- ...

  7. java框架之SpringBoot(1)-入门

    简介 Spring Boot 用来简化 Spring 应用开发,约定大于配置,去繁从简,just run 就能创建一个独立的.产品级别的应用. 背景: J2EE 笨重的开发.繁多的配置.低下的开发效率 ...

  8. C++的默认构造函数

    待看文章:C++ 合成默认构造函数的真相 默认构造函数指不带参数或者所有参数都有缺省值的构造函数!!! 类的默认构造函数可以使得在实例化该类的对象时不用提供参数,但是类也可以不含默认构造函数,这样在实 ...

  9. 【Java】NO.83.Tool.1.GlassFish.1.001-【GlassFish 5 安装使用】-

    1.0.0 Summary Tittle:[Java]NO.83.Tool.1.GlassFish.1.001-[GlassFish 5 安装使用]- Style:EBook Series:Java ...

  10. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...