use of org.springside.modules.metrics.metric.Timer.TimerContext in project springside4 by springside.
the class MetricExamples method timerExample.
@Test
public void timerExample() throws InterruptedException {
MetricRegistry metricRegistry = new MetricRegistry();
// 使用slf4j reporter,并使用自定义logger名字
Slf4jReporter slf4jReporter = new Slf4jReporter("mymetrics");
ReportScheduler scheduler = new ReportScheduler(metricRegistry);
scheduler.addReporter(slf4jReporter);
scheduler.start(1, TimeUnit.SECONDS);
Timer timer = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.timer"), new Double[] { 99d, 99.99d });
// 写法1
TimerContext timerContext = timer.start();
Thread.sleep(100);
timerContext.stop();
timerContext = timer.start();
Thread.sleep(200);
timerContext.stop();
Thread.sleep(750);
// 用法2
long start = System.currentTimeMillis();
Thread.sleep(150);
timer.update(start);
start = System.currentTimeMillis();
Thread.sleep(250);
timer.update(start);
Thread.sleep(650);
scheduler.stop();
}
use of org.springside.modules.metrics.metric.Timer.TimerContext in project springside4 by springside.
the class MetricExamples method jmxExample.
@Test
public void jmxExample() throws InterruptedException, Exception {
MetricRegistry metricRegistry = new MetricRegistry();
Counter counter = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.counter"));
Timer timer = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.timer"), new Double[] { 0.99d, 0.999d });
// 无reporter,only exporter
JmxExporter jmxExporter = new JmxExporter("metrics-example", metricRegistry);
jmxExporter.initMBeans();
ReportScheduler scheduler = new ReportScheduler(metricRegistry);
scheduler.start(1, TimeUnit.SECONDS);
counter.inc();
TimerContext timerContext = timer.start();
Thread.sleep(100);
timerContext.stop();
Thread.sleep(950);
counter.inc(2);
Thread.sleep(1050);
scheduler.stop();
// 校验MBean中的值
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
System.out.println("TotalCount from MBean:" + mBeanServer.getAttribute(new ObjectName("metrics-example", "name", "UserService.getUser.counter"), "TotalCount").toString());
}
use of org.springside.modules.metrics.metric.Timer.TimerContext in project springside4 by springside.
the class TimerTest method normal.
@Test
public void normal() {
MockClock clock = new MockClock();
Timer.clock = clock;
Counter.clock = clock;
Timer timer = new Timer(new Double[] { 90d });
TimerContext timerContext = timer.start();
clock.increaseTime(200);
timerContext.stop();
TimerContext timer2 = timer.start();
clock.increaseTime(300);
timer2.stop();
TimerMetric metric = timer.calculateMetric();
assertThat(metric.counterMetric.totalCount).isEqualTo(2);
assertThat(metric.counterMetric.avgRate).isEqualTo(4);
assertThat(metric.counterMetric.latestCount).isEqualTo(2);
assertThat(metric.counterMetric.latestRate).isEqualTo(4);
assertThat(metric.histogramMetric.min).isEqualTo(200);
assertThat(metric.histogramMetric.avg).isEqualTo(250);
assertThat(metric.histogramMetric.pcts.get(90d)).isEqualTo(300);
}
use of org.springside.modules.metrics.metric.Timer.TimerContext in project springside4 by springside.
the class ReporterTest method runReport.
private void runReport(Reporter reporter) {
MetricRegistry metricRegistry = new MetricRegistry();
MockClock clock = new MockClock();
Counter.clock = clock;
Timer.clock = clock;
// counter
Counter counter = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.counter"));
counter.inc(4);
Counter counter2 = metricRegistry.counter(MetricRegistry.name("UserService", "setUser.counter"));
counter2.inc(6);
clock.increaseTime(1000);
// histogram
Histogram histogram = metricRegistry.histogram(MetricRegistry.name("UserService", "getUser.latency"));
for (int i = 1; i <= 100; i++) {
histogram.update(i);
}
Histogram histogram2 = metricRegistry.histogram(MetricRegistry.name("UserService", "setUser.latency"));
for (int i = 1; i <= 100; i++) {
histogram2.update(i * 2);
}
// timer
Timer timer = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.timer"));
for (int i = 1; i <= 10; i++) {
TimerContext timerContext = timer.start();
clock.increaseTime(25);
timerContext.stop();
}
Timer timer2 = metricRegistry.timer(MetricRegistry.name("UserService", "setUser.timer"));
for (int i = 1; i <= 10; i++) {
TimerContext timerContext = timer2.start();
clock.increaseTime(75);
timerContext.stop();
}
// totally 2 seconds past
ReportScheduler scheduler = new ReportScheduler(metricRegistry, reporter);
scheduler.report();
}
Aggregations