【问题标题】:dropwizard metrics and API timingdropwizard 指标和 API 计时
【发布时间】:2016-06-13 18:40:19
【问题描述】:

我正在尝试对我的一些 API 进行非常简单的性能测量,以确定它们需要多长时间。 我添加了一个测试资源:

private static final MetricRegistry metrics = new MetricRegistry();
private final Timer responses = metrics.timer("test_responses");

@GET
public void test() {
    final Timer.Context context = responses.time();
    try {
        log.info("sleeping...");
        Thread.sleep(10*1000);
    } catch (InterruptedException e) {
    } finally {
        context.stop();//2
    }
}

并将以下内容添加到我的主应用程序类中:

ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    reporter.start(10, TimeUnit.SECONDS);

每十秒我看到一次:

2016 年 6 月 13 日上午 11:38:51 ==================================== =========================

6/13/16 上午 11:39:01 ==================================== =========================

但没有提供关于我创建的“test_responses”指标的信息。非常感谢任何帮助!

【问题讨论】:

  • 你调用了你的测试方法吗?

标签: dropwizard graphite


【解决方案1】:

您的问题是您使用了 2 个指标注册表实例。看这个例子:

public class Application extends io.dropwizard.Application<Configuration>{

    @Override
    public void run(Configuration configuration, Environment environment) throws Exception {
        MetricRegistry metrics = environment.metrics();
        environment.jersey().register(new HelloResource(metrics));

        ConsoleReporter.forRegistry(metrics).build().start(1, TimeUnit.SECONDS);;
    }

    public static void main(String[] args) throws Exception {
        new Application().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml");
    }
}

我正在使用 DW 为您创建的指标注册表。此 MR 还包括 Jetty 统计信息。

我的资源:

@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class HelloResource {

    private MetricRegistry service;

    public HelloResource(MetricRegistry service) {
        this.service = service;
    }

    @GET
    public String hello() {

        Timer timer = service.timer("test");

        try(Context t = timer.time()) {
            return "Hello World";
        }

    }
}

然后输出:

test
             count = 3
         mean rate = 0.89 calls/second
     1-minute rate = 0.00 calls/second
     5-minute rate = 0.00 calls/second
    15-minute rate = 0.00 calls/second
               min = 0.00 milliseconds
               max = 0.01 milliseconds
              mean = 0.00 milliseconds
            stddev = 0.00 milliseconds
            median = 0.00 milliseconds
              75% <= 0.01 milliseconds
              95% <= 0.01 milliseconds
              98% <= 0.01 milliseconds
              99% <= 0.01 milliseconds
            99.9% <= 0.01 milliseconds

我已经调用了3次测试方法,你可以看到MetricRegistry记录的统计数据。

希望能解决你的问题。

问候,

艺术

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-09
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多