Guava Stopwatch source code analysis

We can use the following code for program calculation and performance debugging:

Stopwatch stopwatch = Stopwatch.createStarted();
doSomething();
stopwatch.stop(); // optional
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("time: " + stopwatch); // formatted string like "12.3 ms"

Next, we understand it by interpreting its source code:

Source code analysis:

public final class Stopwatch {
  private final Ticker ticker;//计时器,用于获取当前时间
  private boolean isRunning;//计时器是否运行中的状态标记
  private long elapsedNanos;//用于标记从计时器开启到调用统计的方法时过去的时间
  private long startTick;//计时器开启的时刻时间
}

By combining elapsedNanos and startTick with the current time, we can calculate the elapsed time for the program we need to run.

First look at the Ticker tool class:

public static Stopwatch createStarted() {
    return new Stopwatch().start();
}

Stopwatch() {
    this.ticker = Ticker.systemTicker();
  }


private static final Ticker SYSTEM_TICKER =
      new Ticker() {
        @Override
        public long read() {
          //ticker工具类read方法,直接获取机器的 纳秒 时间
          return Platform.systemNanoTime();
        }
      };


static long systemNanoTime() {
    return System.nanoTime();
  }

Several key methods of StopWatch:

public Stopwatch start() {
    checkState(!isRunning, "This stopwatch is already running.");
    isRunning = true;
    startTick = ticker.read();//设置startTick时间为stopwatch开始启动的时刻时间
    return this;
  }
public Stopwatch stop() {
    long tick = ticker.read();
    checkState(isRunning, "This stopwatch is already stopped.");
    isRunning = false;
    //设置elapsedNanos时间为方法调用时间-stopwatch开启时间+上次程序stopwatch的elapsedNanos历史时间 
    elapsedNanos += tick - startTick;
    return this;
  }
public long elapsed(TimeUnit desiredUnit) {
    return desiredUnit.convert(elapsedNanos(), NANOSECONDS);
  }


private long elapsedNanos() {
    //如果stopwatch仍在运行中,返回当前时刻时间-stopwatch开启时刻时间+历史elapsedNanos时间(elapsedNanos只在stop和reset时会更新)
    //如果stopwatch已停止运行,则直接返回elapsedNanos,详见stop()
    return isRunning ? ticker.read() - startTick + elapsedNanos : elapsedNanos;
  }

in conclusion

Stopwatch stopwatch = Stopwatch.createStarted();
doSomething();
stopwatch.stop(); // optional
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("time: " + stopwatch); // formatted string like "12.3 ms"

Use stopwatch to debug the running time of the program. First, call StopWatch.createStarted() to create and start a stopwatch instance, and call stopwatch.stop() to stop the timing. At this time, the elapsedNanos time of stopwatch will be updated, which is the time from start to finish timing of stopwatch , call stopwatch.elapsed() again to get the length of time elapsed during the start-stop period of stopwatch.

Stopwatch stopwatch = Stopwatch.createStarted();
doSomething();
//stopwatch.stop();
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("time: " + stopwatch); // formatted string like "12.3 ms"

createStarted starts a stopwatch instance, the time of stopwatch continues to elapse, calls the elapsed method, returns isRunning ? ticker.read() - startTick + elapsedNanos : elapsedNanos;, the return value obtained at this time is the current time and the time of stopwatch.start() The time difference, so it is a continuously increasing time.

If you need to continuously measure each step of the key steps in the program, you need to use the following calling method

Stopwatch stopwatch = Stopwatch.createStarted();
doSomething();
stopwatch.stop();
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("time: " + stopwatch); // formatted string like "12.3 ms"

stopwatch.reset().start();
doSomething();
stopwatch.stop();
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("time: " + stopwatch); // formatted string like "12.3 ms"

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711718&siteId=291194637