Java-性能监控类StopWatch

性能监控代码

package com.jd.app.server.test.service;

import org.springframework.util.StopWatch;

public class Test {

    @org.junit.Test
    public void test(){
        StopWatch stopWatch = new StopWatch("方法性能测试");
        try {
            stopWatch.start("任务1");
            Thread.sleep(1000);
            stopWatch.stop();
            stopWatch.start("任务2");
            Thread.sleep(3000);
            stopWatch.stop();
            stopWatch.start("任务3");
            Thread.sleep(500);
            stopWatch.stop();
            stopWatch.start("任务4");
            Thread.sleep(2000);
            stopWatch.stop();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (stopWatch.isRunning()) {
                stopWatch.stop();
            }
            System.out.println(stopWatch.prettyPrint());
        }
    }
}

执行结果

image.png

你可能感兴趣的:(Java-性能监控类StopWatch)