2016-06-13 38 views
0

我正在嘗試對我的一些API進行非常簡單的性能測量,以確定它們需要多長時間。 我添加了一個測試資源:dropwizard指標和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 
    } 
} 

,並添加了folllowing到我的主應用程序類:

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

每隔十秒我看到:

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

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

但沒有提供關於我創建的「test_responses」指標的信息。任何幫助是極大的讚賞!

+0

你調用你的測試方法是什麼? – pandaadb

回答

1

你的問題是,你使用的度量註冊表的兩個實例。看看這個示例:

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還包括碼頭統計。

我的資源:

@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記錄的統計數據。

我希望可以解決您的問題。

問候,

阿圖爾