dropwizard 指标和 API 计时
dropwizard metrics and API timing
我正在尝试对我的一些 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
}
}
并将以下内容添加到我的主应用程序 class:
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
reporter.start(10, TimeUnit.SECONDS);
每十秒我看到:
6/13/16 11:38:51 上午 ================================== ==========================
6/13/16 11:39:01 上午 ================================== ==========================
但是没有提供关于我创建的 "test_responses" 指标的信息。非常感谢任何帮助!
您的问题是您使用了 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记录的统计数据。
希望能解决您的问题。
此致,
阿图尔
我正在尝试对我的一些 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
}
}
并将以下内容添加到我的主应用程序 class:
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
reporter.start(10, TimeUnit.SECONDS);
每十秒我看到:
6/13/16 11:38:51 上午 ================================== ==========================
6/13/16 11:39:01 上午 ================================== ==========================
但是没有提供关于我创建的 "test_responses" 指标的信息。非常感谢任何帮助!
您的问题是您使用了 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记录的统计数据。
希望能解决您的问题。
此致,
阿图尔