如何在drop向导中使用timer?

时间:2018-03-13 08:22:19

标签: java dropwizard metrics

我需要测量方法执行持续时间统计 我决定使用drop wizard。

我要遵循以下文章:
https://dzone.com/articles/dropwizard-part-3-measuring-your-application-metri

看起来这适合我的情况:

final Timer.Context context = responses.time();//1
        try {
            String userOneName = userOne.get();
            String userTwoName = userTwo.get();
            return new ChatroomView(userOneName, userTwoName, chats.chatBetween(userOneName, userTwoName),
                chats.belongingTo(userOneName)
                    .stream()
                    .map(c -> new ChatView(userOneName, c))
                    .collect(toList()));
        } finally {
            context.stop();//2
        }

但在文章中我找不到什么是responses对象;

Plaese澄清

1 个答案:

答案 0 :(得分:0)

static final MetricRegistry metrics = new MetricRegistry();
static final Timer timer = metrics.timer("MyTimer");

static void startReport() {
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();
    reporter.start(1, TimeUnit.MINUTES);
}

@BeforeClass
public static void init() {
    startReport();
}

然后:

final Timer.Context context = timer.time();//1
try {
    String userOneName = userOne.get();
    String userTwoName = userTwo.get();
    return new ChatroomView(userOneName, userTwoName, chats.chatBetween(userOneName, userTwoName),
        chats.belongingTo(userOneName)
            .stream()
            .map(c -> new ChatView(userOneName, c))
            .collect(toList()));
} finally {
    context.stop();//2
}