java

Java-性能监控类StopWatch

2019-01-29  本文已影响42人  码农随想录

性能监控代码

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
上一篇下一篇

猜你喜欢

热点阅读