#524 增加使用示例

This commit is contained in:
calvin.xiao
2016-12-29 14:31:55 +08:00
parent dd613bccfa
commit 1ebba4f26a
4 changed files with 97 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
see src/test/java/org/springside/modules/metrics/FullExampleTest.java for Usage
@@ -24,8 +24,9 @@ public class Histogram {
private LinkedList<Long> measurements; //统计周期内的原始数据
public Histogram(Double... pcts) {
this.pcts = pcts;
measurements = new LinkedList<Long>();
this.pcts = pcts;
latestMetric = createEmptyMetric();
}
@@ -110,6 +111,10 @@ public class Histogram {
return snapshotList.get((int) pos - 1);
}
public void setPcts(Double[] pcts) {
this.pcts = pcts;
}
@Override
public String toString() {
return "Histogram [latestMetric=" + latestMetric + ", measurements=" + measurements + ", pcts="
@@ -0,0 +1,89 @@
package org.springside.modules.metrics;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springside.modules.metrics.metric.Counter;
import org.springside.modules.metrics.metric.Histogram;
import org.springside.modules.metrics.metric.Timer;
import org.springside.modules.metrics.metric.Timer.TimerContext;
import org.springside.modules.metrics.reporter.ConsoleReporter;
import org.springside.modules.metrics.reporter.Slf4jReporter;
/**
* 示例分别使用ConsoleReporter与Slf4jReporter, 演示Counter, Histogram 和 Timer的用法.
*/
public class FullExampleTest {
@Test
public void counterExample() throws InterruptedException {
MetricRegistry metricRegistry = new MetricRegistry();
ConsoleReporter consoleReporter = new ConsoleReporter();
ReportScheduler scheduler = new ReportScheduler(metricRegistry, consoleReporter);
scheduler.start(1, TimeUnit.SECONDS);
Counter counter = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.counter"));
counter.inc();
Thread.sleep(1000);
counter.inc(2);
Thread.sleep(1000);
}
@Test
public void histogramExample() throws InterruptedException {
MetricRegistry metricRegistry = new MetricRegistry();
ConsoleReporter consoleReporter = new ConsoleReporter();
ReportScheduler scheduler = new ReportScheduler(metricRegistry, consoleReporter);
scheduler.start(1, TimeUnit.SECONDS);
Histogram histogram = metricRegistry.histogram(MetricRegistry.name("UserService", "getUser.latency"));
histogram.update(1);
histogram.update(100);
Thread.sleep(1000);
// 增加百分位
histogram.setPcts(new Double[] { 0.99d, 0.999d });
histogram.update(2);
histogram.update(200);
Thread.sleep(1000);
}
@Test
public void timerExample() throws InterruptedException {
MetricRegistry metricRegistry = new MetricRegistry();
Slf4jReporter slf4jReporter = new Slf4jReporter();
ReportScheduler scheduler = new ReportScheduler(metricRegistry, slf4jReporter);
scheduler.start(1, TimeUnit.SECONDS);
Timer timer = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.timer"),
new Double[] { 0.99d, 0.999d });
// 写法1
TimerContext timerContext = timer.start();
Thread.sleep(100);
timerContext.stop();
timerContext = timer.start();
Thread.sleep(200);
timerContext.stop();
Thread.sleep(700);
// 用法2
long start = System.currentTimeMillis();
Thread.sleep(150);
timer.update(start);
start = System.currentTimeMillis();
Thread.sleep(250);
timer.update(start);
Thread.sleep(700);
}
}
@@ -7,6 +7,7 @@
</appender>
<logger name="org.springside.modules" level="INFO"/>
<logger name="metrics" level="INFO"/>
<root level="WARN">