Search in sources :

Example 1 with Timer

use of org.springside.modules.metrics.metric.Timer 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();
}
Also used : Timer(org.springside.modules.metrics.metric.Timer) Slf4jReporter(org.springside.modules.metrics.reporter.Slf4jReporter) TimerContext(org.springside.modules.metrics.metric.Timer.TimerContext) Test(org.junit.Test)

Example 2 with Timer

use of org.springside.modules.metrics.metric.Timer 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());
}
Also used : Counter(org.springside.modules.metrics.metric.Counter) Timer(org.springside.modules.metrics.metric.Timer) JmxExporter(org.springside.modules.metrics.exporter.JmxExporter) TimerContext(org.springside.modules.metrics.metric.Timer.TimerContext) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 3 with Timer

use of org.springside.modules.metrics.metric.Timer in project springside4 by springside.

the class ReportScheduler method report.

/**
 * 定期计算所有Metrics, 并调用所有Reporter进行汇报.
 */
public void report() {
    // 取出所有Metrics, 未按名称排序.
    Map<String, Gauge> gaugeMap = metricRegistry.getGauges();
    Map<String, Counter> counterMap = metricRegistry.getCounters();
    Map<String, Histogram> histogramMap = metricRegistry.getHistograms();
    Map<String, Timer> timerMap = metricRegistry.getTimers();
    // 计算单位时间内的metrics值, 存入该Metrics的Snapshot中,并清零原始数据
    for (Gauge gauge : gaugeMap.values()) {
        gauge.calculateMetric();
    }
    for (Counter counter : counterMap.values()) {
        counter.calculateMetric();
    }
    for (Histogram histogram : histogramMap.values()) {
        histogram.calculateMetric();
    }
    for (Timer timer : timerMap.values()) {
        timer.calculateMetric();
    }
    // 调度所有Reporters 输出 metrics值
    for (Reporter reporter : reporters) {
        reporter.report(gaugeMap, counterMap, histogramMap, timerMap);
    }
}
Also used : Histogram(org.springside.modules.metrics.metric.Histogram) Counter(org.springside.modules.metrics.metric.Counter) Timer(org.springside.modules.metrics.metric.Timer) Gauge(org.springside.modules.metrics.metric.Gauge)

Example 4 with Timer

use of org.springside.modules.metrics.metric.Timer in project springside4 by springside.

the class MetricRegistryTest method execution.

@Test
public void execution() {
    MetricRegistry metricRegistry = new MetricRegistry();
    Timer execution = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.execution"));
    assertThat(execution).isNotNull();
    Map<String, Timer> executions = metricRegistry.getTimers();
    Timer execution2 = executions.get("UserService.getUser.execution");
    assertThat(execution2).isNotNull().isSameAs(execution);
    Timer execution3 = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.execution"));
    assertThat(execution3).isNotNull().isSameAs(execution);
}
Also used : Timer(org.springside.modules.metrics.metric.Timer) Test(org.junit.Test)

Example 5 with Timer

use of org.springside.modules.metrics.metric.Timer 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);
}
Also used : TimerMetric(org.springside.modules.metrics.metric.TimerMetric) Timer(org.springside.modules.metrics.metric.Timer) TimerContext(org.springside.modules.metrics.metric.Timer.TimerContext) MockClock(org.springside.modules.metrics.utils.Clock.MockClock) Test(org.junit.Test)

Aggregations

Timer (org.springside.modules.metrics.metric.Timer)6 Test (org.junit.Test)4 TimerContext (org.springside.modules.metrics.metric.Timer.TimerContext)4 Counter (org.springside.modules.metrics.metric.Counter)3 Histogram (org.springside.modules.metrics.metric.Histogram)2 MockClock (org.springside.modules.metrics.utils.Clock.MockClock)2 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 JmxExporter (org.springside.modules.metrics.exporter.JmxExporter)1 Gauge (org.springside.modules.metrics.metric.Gauge)1 TimerMetric (org.springside.modules.metrics.metric.TimerMetric)1 Slf4jReporter (org.springside.modules.metrics.reporter.Slf4jReporter)1