dropwizard指标和API时间

时间:2016-06-13 18:40:19

标签: dropwizard graphite

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

protected void gvArticle_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = gvArticle.Rows[e.RowIndex];
        FileUpload fu = row.Cells[0].FindControl("fileupload") as FileUpload;
        if (fu != null && fu.HasFile)
        {
            fu.SaveAs(Server.MapPath("~/Uploaded Images" + fu.FileName));
        }
    }

并将以下内容添加到我的主要Application类中:

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
    }
}

我看到每十秒钟:

6/13/16 11:38:51 AM ==================================== ========================

6/13/16 11:39:01 AM ==================================== ========================

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

1 个答案:

答案 0 :(得分: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记录的统计数据。

我希望能解决你的问题。

此致

阿图尔