From d7051e152231209b748acefd92b72e32911aaaa6 Mon Sep 17 00:00:00 2001 From: liufuqiang Date: Mon, 11 Dec 2023 17:26:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E9=97=B4=E7=BB=9F=E8=AE=A1=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/core/date/CodeWatchUtil.java | 144 ++++++++++++++++++ .../hutool/core/date/CodeWatchUtilTest.java | 51 +++++++ 2 files changed, 195 insertions(+) create mode 100644 hutool-core/src/main/java/org/dromara/hutool/core/date/CodeWatchUtil.java create mode 100644 hutool-core/src/test/java/org/dromara/hutool/core/date/CodeWatchUtilTest.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/CodeWatchUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/CodeWatchUtil.java new file mode 100644 index 000000000..5275d6da5 --- /dev/null +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/CodeWatchUtil.java @@ -0,0 +1,144 @@ +package org.dromara.hutool.core.date; + +import org.dromara.hutool.core.text.StrUtil; +import org.dromara.hutool.core.thread.ThreadUtil; + +import java.util.concurrent.TimeUnit; + +/** + * 统计代码运行时间工具
+ * 此工具用于存储一组任务的耗时时间,并一次性打印对比,也可以打印每个任务运行时间
+ * + *

+ * 使用方法如下: + *

{@code
+ * CodeWatchUtil.init("任务id");
+ *
+ * // 任务1
+ * CodeWatchUtil.start("任务一");
+ * Thread.sleep(1000);
+ *
+ * // 任务2
+ * CodeWatchUtil.start("任务二");
+ * Thread.sleep(2000);
+ * Console.log(CodeWatchUtil.printCurrentTask());
+ *
+ * // 打印出耗时
+ * Console.log(CodeWatchUtil.prettyPrint());
+ *
+ * }
+ * + * @author liufuqiang + */ +public class CodeWatchUtil { + + private static ThreadLocal threadLocal = null; + + /** + * 初始化计时任务 + * + * @param id 用于标识秒表的唯一ID + */ + public static void init(String id) { + threadLocal = ThreadUtil.createThreadLocal(() -> StopWatch.of(id)); + } + + /** + * 创建计时任务
+ * 如果上一个任务未停止,可自动停止 + * + * @param taskName 任务名称 + */ + public static void start(String taskName) { + if (threadLocal == null) { + return; + } + + StopWatch stopWatch = threadLocal.get(); + if (stopWatch == null) { + return; + } + if (stopWatch.isRunning()) { + stopWatch.stop(); + } + stopWatch.start(taskName); + } + + /** + * 停止当前任务 + */ + public static void stop() { + if (threadLocal == null) { + return; + } + + StopWatch stopWatch = threadLocal.get(); + if (stopWatch == null) { + return; + } + if (stopWatch.isRunning()) { + stopWatch.stop(); + } + } + + /** + * 生成所有任务的一个任务花费时间表,单位纳秒 + * + * @return 任务时间表 + */ + public static String prettyPrint() { + return prettyPrint(null); + } + + /** + * 生成所有任务的一个任务花费时间表 + * + * @param unit 时间单位,{@code null}则默认{@link TimeUnit#NANOSECONDS} 纳秒 + * @return 任务时间表 + */ + public static String prettyPrint(TimeUnit unit) { + if (threadLocal == null) { + return null; + } + + StopWatch stopWatch = threadLocal.get(); + if (stopWatch == null) { + return null; + } + + if (stopWatch.isRunning()) { + stopWatch.stop(); + } + threadLocal.remove(); + return stopWatch.prettyPrint(unit); + } + + /** + * 打印当前任务执行时间 + * + * @return 当前任务运行时间 + */ + public static String printCurrentTask() { + return printCurrentTask(TimeUnit.NANOSECONDS); + } + + /** + * 打印当前任务执行的时间 + * + * @param unit 时间单位 + * @return 当前任务运行时间 + */ + public static String printCurrentTask(TimeUnit unit) { + if (threadLocal == null) { + return null; + } + + StopWatch stopWatch = threadLocal.get(); + if (stopWatch == null) { + return null; + } + StopWatch.TaskInfo taskInfo = stopWatch.getLastTaskInfo(); + return StrUtil.format("StopWatch '{}:' taskName:{} running time = {} {}", stopWatch.getId(), + taskInfo.getTaskName(), taskInfo.getTime(unit), DateUtil.getShortName(unit)); + } +} diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/date/CodeWatchUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/date/CodeWatchUtilTest.java new file mode 100644 index 000000000..0102f8d02 --- /dev/null +++ b/hutool-core/src/test/java/org/dromara/hutool/core/date/CodeWatchUtilTest.java @@ -0,0 +1,51 @@ +package org.dromara.hutool.core.date; + +import org.dromara.hutool.core.lang.Console; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.TimeUnit; + +/** + * @author: liufuqiang + * @date: 2023-12-11 17:04 + */ +public class CodeWatchUtilTest { + + @Test + public void printDefault() throws InterruptedException { + CodeWatchUtil.init("任务id"); + + // 任务1 + CodeWatchUtil.start("任务一"); + Thread.sleep(1000); + + // 任务2 + CodeWatchUtil.start("任务二"); + Thread.sleep(2000); + Console.log(CodeWatchUtil.printCurrentTask()); + + + // 打印出耗时 + Console.log(CodeWatchUtil.prettyPrint()); + } + + @Test + public void printUnit() throws InterruptedException { + CodeWatchUtil.init("任务id"); + method01(); + method02(); + Console.log(CodeWatchUtil.prettyPrint(TimeUnit.MILLISECONDS)); + } + + private static void method01() throws InterruptedException { + CodeWatchUtil.start("方法一"); + Thread.sleep(1000); + } + + private static void method02() throws InterruptedException { + CodeWatchUtil.start("方法二"); + Thread.sleep(2000); + CodeWatchUtil.stop(); + Console.log(CodeWatchUtil.printCurrentTask(TimeUnit.MILLISECONDS)); + } +}