Metrics is a Java library that can provide you with unmatched code-run insight.
It was developed by Yammer to detect the health of back-end services on the JVM. Metrics provides a powerful toolset for measuring the behavior of key components in your production environment.
Metrics provides a common set of modular libraries for support such as Guice,jetty,log4j,apache httpclient,ehcache,logback,spring, Also provides comparisons such as ganglia and graphite backend reports.
Website of the project: http://metrics.dropwizard.io/
Metrics has five major components
1:counter
Record execution times
2:gauge
Get a value
3:meter
The rate used to calculate events
4:histogram
You can provide statistical data for a data flow. In addition to the maximum, the minimum, the average, it can also measure the median (median), the percentages such as xx% such quantile data
5:timer
Used to measure the rate and time at which a piece of code is invoked. Equal to Meter+hitogram, not only TPS, but also execution time.
Here is the code example
First join the dependency
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactid>metrics-core </artifactId>
<version>3.1.2</version>
</dependency>
Com.codahale.metrics.MetricRegistry is the core of the metrics, which is first preserved with a constant
Package com.lala.core;
Import Com.codahale.metrics.MetricRegistry;
public class Metricconstant
{public
static metricregistry REGISTER = new Metricregistry ();
}
It also says that metrics supports a lot of output, JMX, log4j, Jetty, httpclient, and so on. I'm going to output it directly to the console.
Package com.lala.core;
Import Java.util.concurrent.TimeUnit;
Import Com.codahale.metrics.ConsoleReporter;
/**
* Console printout/public
class Myconsolereport {public
static void Startreport ()
{
final Consolereporter reporter = Consolereporter.forregistry (metricconstant.register)
. Convertratesto ( Timeunit.seconds)
. Convertdurationsto (timeunit.seconds)
. Build ();
Perform one second
reporter.start (1, timeunit.seconds);
}
First of all, come to a basic public class
Package com.lala.core;
Import Java.util.concurrent.TimeUnit;
Import Com.codahale.metrics.MetricRegistry;
public class Base
{
protected static metricregistry metric = Metricconstant.register;
protected static void Secondsleep (Long value)
{
try
{
TimeUnit.SECONDS.sleep (value);
} catch ( Interruptedexception e)
{
e.printstacktrace ();
}
}
protected static void Millisecondsleep (Long value)
{
try
{
TimeUnit.MILLISECONDS.sleep (value);
} catch (interruptedexception e)
{
e.printstacktrace ();
}
}
}
Next, let's demonstrate the basic usage of the five major components
One: Counter
[Java] View plain copy package com.lala.demo; import java.util.random; import com.codahale.metrics.counter; import com.lala.core.base; import com.lala.core.myconsolereport; /** * record execution times */ public class countertest extends base { final static counter exec = metric.counter (" Com.pp.counter.invoke "); public static void main (String[) args) { Myconsolereport.startreport (); new thread (()- >{ for (int i=1;i <=3;i++) &NBsp; { exec.inc (); millisecondsleep (new Random (). Nextint () *2);  }). Start (); secondsleep (3); } }
The output is as follows:
15-11-22 20:38:29 ==============================================================
--Counters--------------------------------------------------------------------
Com.pp.counter.invoke
Count = 3
Two: Gauge
[Java] view plain copy package Com.lala.demo;