简体   繁体   English

如何为单个流程实现dropwizard指标?

[英]how to implement dropwizard metrics for a single process?

Hi I'm new to java and am trying to learn how to use the Dropwizard Metrics library to measure performance of a single process. 嗨,我是Java新手,正在尝试学习如何使用Dropwizard Metrics库来衡量单个进程的性能。 I've looked at their getting started and have run what they wrote for the MetricsRegistry but don't understand how to combine both the process and the metricsRegistry into one (where it measures the time it takes my process and not the time it takes itself to run). 我查看了他们的入门知识,并运行了他们为MetricsRegistry编写的内容,但不了解如何将流程和metricsRegistry合并为一个(在这里它衡量的是我的流程所花费的时间,而不是它所花费的时间)跑步)。

I could be wording a lot of things wrong but hopefully my question is clear enough. 我可能措辞错了很多,但希望我的问题很清楚。 Thanks in advance for any help/clarification I can get! 在此先感谢您可以获得任何帮助/说明! I pasted the code I want to measure below along (pi digits to nth number) with what Dropwizard Metrics gave for their getting started: 我将以下要测量的代码(pi位数到n位数)与Dropwizard Metrics入门的内容粘贴在一起:

package decimals;

import java.util.Scanner;
import java.math.BigDecimal;


public class Decimals {


    public static void main(String[] args) {

        BigDecimal seven = new BigDecimal(7.0);
        Scanner input = new Scanner(System.in);
        System.out.println("to what nth?");
        int i = input.nextInt();
        BigDecimal pi = new BigDecimal(22.0).divide(seven, i, BigDecimal.ROUND_UP);

        if ( i < 0) {
            System.out.println("can't be less than 0");
        } else {
            System.out.println(pi);
        }
    }
}

//dropwizardmetrics:


package com.***.***;
import com.codahale.metrics.*;
import java.util.concurrent.TimeUnit;

public class MetricsRegistry {
    static final MetricRegistry metrics = new MetricRegistry();
    public static void main(String args[]) {
        startReport();
        Meter requests = metrics.meter("requests");
        requests.mark();
        wait5Seconds();
    }

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

    private static void wait5Seconds() {
        try {
            Thread.sleep(5*1000);
        }
        catch(InterruptedException e) {}
    }



}

Put all your Pi computation logic into a method called computePi(), and try this in your main method: 将您所有的Pi计算逻辑放到一个名为computePi()的方法中,然后在您的main方法中尝试:

public static void main(String args[]) {
    startReport();
    Timer timer = metricRegistry.timer("computePi");    
    Timer.Context context = timer.time();
    try {
        computePi();
    } finally {
        context.stop();
    }
}

In your example, you were using a Meter, which does not measure the time it takes to execute (it measures the rate of occurrence and overall count). 在您的示例中,您使用的是仪表,它不测量执行时间(它测量发生率和总计数)。 I changed it be a Timer, which measures time as well. 我将其更改为计时器,它也可以测量时间。 The timer begins when I call timer.time(). 当我调用timer.time()时,计时器开始计时。 Then I execute your computePi() process in a try block. 然后,我在try块中执行您的computePi()进程。 I use a "finally" block to stop the timer no matter what happens inside of computePi(). 无论computePi()内部发生什么情况,我都使用“ finally”块来停止计时器。 This is how you get the exact time that it takes to execute your process. 这是您获得执行过程所需的确切时间的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM