public class StopWatchTool implementsAutoCloseable {/*** 任务ID*/

privateString id;/*** Start time of the current task.*/

private longstartMs;/*** Name of the current task.*/@NullableprivateString currentTaskName;/*** Second Name of the current task.*/@NullableprivateString currentTaskChildName;//记录任务信息,同一任务,可以分段计时

private final Map> taskMap = new HashMap<>();public staticStopWatchTool newInstance(String id) {return newStopWatchTool(id);

}

@Overridepublic voidclose() {this.stop();

}publicStopWatchTool(String id) {this.id =id;

}/*** 开始时间差类型指标记录,如果需要终止,请调用 {@link#stop()}

*

*@paramtaskName 指标名*/

public void start(String taskName, String taskChildName) throwsIllegalStateException {if (this.currentTaskName != null) {throw new IllegalStateException("Can't start StopWatchTool: it's already running");

}this.currentTaskName =taskName;this.currentTaskChildName =taskChildName;this.startMs =System.currentTimeMillis();

}/*** 返回this,支持链式调用

*

*@paramtaskName

*@return*@throwsIllegalStateException*/

public StopWatchTool startByChain(String taskName, String taskChildName) throwsIllegalStateException {if (this.currentTaskName != null) {throw new IllegalStateException("Can't start StopWatchTool: it's already running");

}this.currentTaskName =taskName;this.currentTaskChildName =taskChildName;this.startMs =System.currentTimeMillis();return this;

}/*** 终止时间差类型指标记录,调用前请确保已经调用*/

public void stop() throwsIllegalStateException {if (this.currentTaskName == null) {throw new IllegalStateException("Can't stop TraceWatch: it's not running");

}long lastTime = System.currentTimeMillis() - this.startMs;

TaskInfo info= new TaskInfo(this.currentTaskName, this.currentTaskChildName, lastTime);this.taskMap.computeIfAbsent(this.currentTaskName, e -> new LinkedList<>()).add(info);this.currentTaskName = null;

}/*** 直接记录指标数据,不局限于时间差类型

*

*@paramtaskName 指标名

*@paramdata 指标数据*/

public voidrecord(String taskName, String taskChildName, Object data) {

TaskInfo info= newTaskInfo(taskName, taskChildName, data);this.taskMap.computeIfAbsent(taskName, e -> new LinkedList<>()).add(info);

}/*** 打印

*

*@return

*/

publicString prettyPrint() {

String whiteStr= " ";

StringBuilder sb= new StringBuilder(id).append(":").append("\n");if (this.taskMap.isEmpty()) {

sb.append("No task info kept");

}else{

sb.append("-----------------------------------------\n");

sb.append("taskName taskChildName ms \n");

sb.append("-----------------------------------------\n");for (Map.Entry>entry : taskMap.entrySet()) {

sb.append(entry.getKey());

sb.append(" : ");for (int i = 0; i < entry.getValue().size(); i++) {

TaskInfo taskInfo=entry.getValue().get(i);if (i == 0) {

sb.append(taskInfo.getTaskName()).append(whiteStr);

}

sb.append(taskInfo.getTaskChildName()).append(whiteStr);

sb.append(taskInfo.getData()).append(whiteStr);

}

sb.append("\n");

}

}returnsb.toString();

}/*** 任务信息*/

public static final classTaskInfo {private finalString taskName;private finalString taskChildName;private finalObject data;publicTaskInfo(String taskName, String taskChildName, Object data) {this.taskName =taskName;this.taskChildName =taskChildName;this.data =data;

}publicString getTaskName() {returntaskName;

}publicString getTaskChildName() {returntaskChildName;

}publicObject getData() {returndata;

}

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐