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

Java Metrics的更多相关文章

  1. JAVA Metrics度量工具 - Metrics Core 翻译

    Metrics核心 翻译自Metrics官方文档: http://metrics.codahale.com/manual/core/ JAVA Metrics是一个用于度量的一个JAVA的类库,使用请 ...

  2. Java Metrics工具介绍

    目录 简介 快速入门 Maven配置 MetricRegistry Gauge Meter Counter Histgram Timer Reporter 更多用法 参考资料 简介 Metric是一个 ...

  3. JAVA Metrics 度量工具使用介绍1

    Java Metric使用介绍1 Metrics是一个给JAVA提供度量工具的包,在JAVA代码中嵌入Metrics代码,可以方便的对业务代码的各个指标进行监控,同一时候,Metrics可以非常好的跟 ...

  4. JAVA Metrics 度量工具使用介绍

    转载: http://blog.csdn.net/scutshuxue/article/details/8350135 http://koven2049.iteye.com/blog/968143 h ...

  5. Metrics.NET 项目

    Metrics.NET(https://github.com/etishor/Metrics.NET)是一个给CLR 提供度量工具的包,它是移植自Java的metrics,在c#代码中嵌入Metric ...

  6. Metrics.NET实践(1)

    起因:对应用的监控和测量是WEB应用的一个重要话题,尤其在监控错误率,并发量,以及框架库中的动态值.于是,在性能优化的时候找到了metrics.net. 简介 开始使用 度量 Gauges Count ...

  7. kafka监控系统

    Metrics-Java版的指标度量工具之一 Metrics-Java版的指标度量工具之二 JAVA Metrics 度量工具使用介绍1 JAVA Metrics度量工具 - Metrics Core ...

  8. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  9. jmeter—PerfMon Metrics Collector(附java.io.IOException: Agent is unreachable via TCP错误解决办法)

    jmeter—PerfMon Metrics Collector(附java.io.IOException: Agent is unreachable via TCP错误解决办法 转自https:// ...

随机推荐

  1. hdu 4034 Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题目分类:图论 题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边 第二组样例 第 ...

  2. Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)

    program WriteStruct; {$APPTYPE CONSOLE} uses SysUtils; //写入结构体 type TCustomer = record ID: ]; Code: ...

  3. 《转》div 中间固定 左右自适应实现

    <转自>:http://www.w3cplus.com/css/layout-column-three 对于我来说,这是一种很少碰到的布局方法,不知道大家有何体会,那么下面我们一起来看这种 ...

  4. [Android学习笔记]使用getIdentifier()获取资源Id

    使用getIdentifier()获取资源Id Android中可以使用getIdentifier()获取资源ID ex: 根据图片名称获取图片Id private int getImageResId ...

  5. [置顶] 页面缓存,cache,设置缓存过期时间,OutputCache

    页面缓存 方法一: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //缓存有数据 if (Cach ...

  6. ie浏览器提交参数和其它浏览器的区别

    场景描述: 用户注册模块(ajax提交方式,post方法),在url后追加了一个参数,如:url+‘btnvalue=中文参数’,如此在非ie浏览器注册时,功能完好,但在ie下注册不成功.调式后发现在 ...

  7. ZOJ 1654 二分匹配基础题

    题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...

  8. Java---25---集合框架共性方法

    集合类 为什么会出现集合类 面向对象语言对事物的体现都是以对象的形式,所以为了方便对较多个对象的操作,就对对象进行存储,集合就是存储对象最经常使用的一种方式 数组和集合类同一时候容器,有何不同? 数组 ...

  9. [Linux]Centos git报错fatal: HTTP request failed

    在使用git pull.git push.git clone会报类似例如以下的错误: error: The requested URL returned error: 401 Unauthorized ...

  10. pan

    百度云盘地址第一季http://pan.baidu.com/share/link?shareid=198342&uk=1191518428第二季http://pan.baidu.com/sha ...