TimeWatch程序耗时统计

调用方法:

/**
 *
 * @title: 程序时间统计类TimeWatchUtil
 * @time: 2019年5月28日
 * 
 */
public class TimeWatchTest {
	public static void main(String[] args) throws InterruptedException {
		TimeWatchUtil tw = TimeWatchUtil.createTimeWatch("TtimeWatch====:");
			Thread.sleep(100);
			tw.addStep("步骤1");
			Thread.sleep(200);
			tw.addStep("步骤2");
			Thread.sleep(300);
			tw.addStep("步骤3");
			Thread.sleep(400);
			tw.addStep("步骤4");
			if (tw != null) {
				System.out.println(tw.outputTimeList());
			}
			//[TIMEWATCH] TtimeWatch====::1000 [DETAILS] 步骤1:100 步骤2:200 步骤3:300 步骤4:400 	
		}
}

工具类:

package com.tt;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * description:主要用来调试程序,观察程序运行每一步所用的时间
 * 
 */
public class TimeWatchUtil {
	/**
	 * 创建开始时间
	 */
	private long start;
	/**
	 * 节点数据(一般保存结束时间数据)
	 */
	private List<Object> sections;
	/**
	 * 节点名称
	 */
	private List<Object> sectionNames;
	/**
	 * 观察名称,下面包括很多的sectionNames,watchName属于sectionNames的整体,便于观察定位
	 */
	private String watchName;

	public static TimeWatchUtil createTimeWatch(String watchName) {
		TimeWatchUtil timeWatch = new TimeWatchUtil();
		timeWatch.watchName = watchName;
		return timeWatch;
	}

	protected TimeWatchUtil() {
		sections = new ArrayList<>();// 初始化sections
		sectionNames = new ArrayList<>();// 初始化sectionNames
		start = System.currentTimeMillis();// 当前系统开始时间
	}

	public void addStep(String stepName) {
		sectionNames.add(stepName);// 每一步节点的名称
		sections.add(Long.valueOf(System.currentTimeMillis()));// 节点开始时间
	}

	public String outputTimeList() {
		watchName = watchName.trim();
		StringBuffer outStr = new StringBuffer();// 整体观察名称

		outStr.append("[TIMEWATCH] ");
		outStr.append(watchName);
		outStr.append(":");
		outStr.append(" [DETAILS] ");

		// 输出每一个节点的名称和花费时间
		long last = start;
		for (int i = 0; i < sections.size(); i++) {
			long temp = ((Long) sections.get(i)).longValue();
			outStr.append("" + (String) sectionNames.get(i) + ":");
			outStr.append((double) (temp - last));
			outStr.append(" ");
			last = temp;
		}
		// 总体花费时间
		long totalWaste = 0;
		if (sections != null && sections.size() > 0) {
			totalWaste = ((Long) sections.get(sections.size() - 1)).longValue() - start;
		}
		// 将总体花费时间插入到名称watchName的后面。"[TIMEWATCH] "和":"长度是13
		outStr.insert(watchName.length() + 13, totalWaste);
		return outStr.toString();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_36521848/article/details/106204350
今日推荐