目的:打印代码块的执行时间
做法:定义工具类 实现 AutoCloseable,重写close方法。初始化时记录开始时间,代码块结束时执行close方法计算时长并打印日志
实现:通过字符串占位符,实现其他日志传入。并且通过重载构建函数,支持传入自定义日志消息,可以将时间打印在日志前面和后面
完整代码如下:(使用方式看 main 方法)
package com.example.demo.utils;
import lombok.extern.slf4j.Slf4j;
/**
* 统计代码块执行时间
* 使用方式看 main 函数
*
* @Author XZ.Tan
* @Date: 2021/1/15 10:44
* @Version 1.0
*/
@Slf4j
public class CountRunTimeCostUtil implements AutoCloseable {/**
* 开始执行时间
*/
private long startTime;/**
* 日志
*/
private String msg;/**
* 时间是否打印在日志开始
*/
private boolean isStart;/**
* 默认耗时加在日志末尾
*
* @param msg
*/
public CountRunTimeCostUtil(String msg, Object... args) {
String parse = parse(msg, args);
this.startTime = System.currentTimeMillis();
this.msg = parse;
this.isStart = false;
}/**
*
*
* @param msg
* @param isStart
*/
public CountRunTimeCostUtil(Boolean isStart, String msg, Object... args) {
String parse = parse(msg, args);
this.startTime = System.currentTimeMillis();
this.msg = parse;
if (isStart) {
this.isStart = true;
} else {
this.isStart = false;
}
}@Override
public void close() {
if (isStart) {
log.info("执行耗时:{}ms, {}", System.currentTimeMillis() - startTime, msg);
} else {
log.info("{}, 执行耗时:{}ms", msg, System.currentTimeMillis() - startTime);
}
}/**
* 将字符串text中由openToken和closeToken组成的占位符依次替换为args数组中的值
*
* @param openToken
* @param closeToken
* @param text
* @param args
* @return
*/
public static String parse(String openToken, String closeToken, String text, Object... args) {
if (args == null || args.length <= 0) {
return text;
}
int argsIndex = 0;if (text == null || text.isEmpty()) {
return "";
}
char[] src = text.toCharArray();
int offset = 0;
// search open token
int start = text.indexOf(openToken, offset);
if (start == -1) {
return text;
}
final StringBuilder builder = new StringBuilder();
StringBuilder expression = null;
while (start > -1) {
if (start > 0 && src[start - 1] == '\\') {
// this open token is escaped. remove the backslash and continue.
builder.append(src, offset, start - offset - 1).append(openToken);
offset = start + openToken.length();
} else {
// found open token. let's search close token.
if (expression == null) {
expression = new StringBuilder();
} else {
expression.setLength(0);
}
builder.append(src, offset, start - offset);
offset = start + openToken.length();
int end = text.indexOf(closeToken, offset);
while (end > -1) {
if (end > offset && src[end - 1] == '\\') {
// this close token is escaped. remove the backslash and continue.
expression.append(src, offset, end - offset - 1).append(closeToken);
offset = end + closeToken.length();
end = text.indexOf(closeToken, offset);
} else {
expression.append(src, offset, end - offset);
offset = end + closeToken.length();
break;
}
}
if (end == -1) {
// close token was not found.
builder.append(src, start, src.length - start);
offset = src.length;
} else {
///仅仅修改了该else分支下的个别行代码String value = (argsIndex <= args.length - 1) ?
(args[argsIndex] == null ? "" : args[argsIndex].toString()) : expression.toString();
builder.append(value);
offset = end + closeToken.length();
argsIndex++;
}
}
start = text.indexOf(openToken, offset);
}
if (offset < src.length) {
builder.append(src, offset, src.length - offset);
}
return builder.toString();
}/**
* 占位符替换
*
* @param text
* @param args
* @return
*/
public static String parse(String text, Object... args) {
return parse("{", "}", text, args);
}/**
* 如何使用
*
* @param args
*/
public static void main(String[] args) {// 耗时
// try (CountRunTimeCostUtil c = new CountRunTimeCostUtil("{} aabb", "耗时测试")) {
try (CountRunTimeCostUtil c = new CountRunTimeCostUtil("耗时测试")) {
// try (CountRunTimeCostUtil c = new CountRunTimeCostUtil(true,"{} aabb", "耗时测试")) {
// try (CountRunTimeCostUtil c = new CountRunTimeCostUtil(false,"耗时测试")) {
// 业务代码块 start
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 业务代码块 start
}
}
}