美文网首页java
Java-性能监控类StopWatch

Java-性能监控类StopWatch

作者: 码农随想录 | 来源:发表于2019-01-29 10:20 被阅读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

相关文章

网友评论

    本文标题:Java-性能监控类StopWatch

    本文链接:https://www.haomeiwen.com/subject/lqwtsqtx.html