天天看點

定時器 Stop Watch

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;

@Slf4j
public class StopWatchMain {

    public static void main(String[] args) throws InterruptedException {
        StopWatch sw = new StopWatch();

        sw.start("接口開始請求");

        Thread.sleep(1000);

        sw.stop();

        sw.start("處理資料");

        Thread.sleep(100);

        sw.stop();

        sw.start("寫入資料庫");

        Thread.sleep(100);

        sw.stop();

        log.info("階段性耗時統計: {}", sw.prettyPrint());
        log.info("總耗時(毫秒): {}", sw.getTotalTimeMillis());
    }

}           
階段性耗時統計: StopWatch '': running time = 1204345200 ns
---------------------------------------------
ns         %     Task name
---------------------------------------------
999568200  083%  接口開始請求
100111200  008%  處理資料
104665800  009%  寫入資料庫

總耗時(毫秒): 1204

Process finished with exit code 0           
int EXEC_MAX = 3000;
for (int idCount = 0; idCount <= EXEC_MAX; idCount++) {
    System.out.println(idCount * 100 / EXEC_MAX);
}           

繼續閱讀