关于java:这4种统计代码执行耗时才足够优雅

33次阅读

共计 7927 个字符,预计需要花费 20 分钟才能阅读完成。

送大家以下 java 学习材料,文末有支付形式

明天,跟大家分享一下,如何在代码中,统计接口耗时,最优雅,性能最高,接下来我将介绍 4 种统计形式,如果你有更好的形式,欢送文末留言区,交换

一、前言

代码耗时统计在日常开发中算是一个非常常见的需要,特地是在须要找出代码性能瓶颈时。

可能也是受限于 Java 的语言个性,总感觉代码写起来不够优雅,大量的耗时统计代码,烦扰了业务逻辑。特地是开发性能的时候,有个感触就是刚刚开发完代码很清新优雅,后果加了一大堆辅助代码后,整个代码就变得臃肿了,本人看着都挺好受,因而总想着能不能把这块写的更优雅一点,明天本文就尝试探讨下“代码耗时统计”这一块。

在开始注释前,先说下前提,“代码耗时统计”的并不是某个办法的耗时,而是任意代码段之间的耗时。这个代码段,可能是一个办法中的几行代码,也有可能是从这个办法的某一行到另一个被调用办法的某一行,因而通过 AOP 形式是不能实现这个需要的。

二、惯例办法

2.1 时间差统计

这种形式是最简略的办法,记录下开始工夫,再记录下完结工夫,计算时间差即可。

public class TimeDiffTest {public static void main(String[] args) throws InterruptedException {final long startMs = TimeUtils.nowMs();        TimeUnit.SECONDS.sleep(5); // 模仿业务代码        System.out.println("timeCost:" + TimeUtils.diffMs(startMs));    }}/* output: timeCost: 5005*/
``````
public class TimeUtils {/**     * @return 以后毫秒数     */    public static long nowMs() {return System.currentTimeMillis();    }    /**     * 以后毫秒与起始毫秒差     * @param startMillis 开始纳秒数     * @return 时间差     */    public static long diffMs(long startMillis) {return diffMs(startMillis, nowMs());    }}

这种形式的长处是实现简略,利于了解;毛病就是对代码的侵入性较大,看着很傻瓜,不优雅。

2.2 StopWatch

第二种形式是参考 StopWatch,StopWatch 通常被用作统计代码耗时,各个框架和 Common 包都有本人的实现。

public class TraceWatchTest {public static void main(String[] args) throws InterruptedException {TraceWatch traceWatch = new TraceWatch();        traceWatch.start("function1");        TimeUnit.SECONDS.sleep(1); // 模仿业务代码        traceWatch.stop();        traceWatch.start("function2");        TimeUnit.SECONDS.sleep(1); // 模仿业务代码        traceWatch.stop();        traceWatch.record("function1", 1); // 间接记录耗时        System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));    }}/* output: {"function2":[{"data":1000,"taskName":"function2"}],"function1":[{"data":1000,"taskName":"function1"},{"data":1,"taskName":"function1"}]}*/
``````
public class TraceWatch {/** Start time of the current task. */    private long startMs;    /** Name of the current task. */    @Nullable    private String currentTaskName;    @Getter    private final Map<String, List<TaskInfo>> taskMap = new HashMap<>();    /**     * 开始时间差类型指标记录,如果须要终止,请调用 {@link #stop()}     *     * @param taskName 指标名     */    public void start(String taskName) throws IllegalStateException {if (this.currentTaskName != null) {throw new IllegalStateException("Can't start TraceWatch: it's already running");        }        this.currentTaskName = taskName;        this.startMs = TimeUtils.nowMs();}    /**     * 终止时间差类型指标记录,调用前请确保曾经调用     */    public void stop() throws IllegalStateException {        if (this.currentTaskName == null) {throw new IllegalStateException("Can't stop TraceWatch: it's not running");        }        long lastTime = TimeUtils.nowMs() - this.startMs;        TaskInfo info = new TaskInfo(this.currentTaskName, lastTime);        this.taskMap.computeIfAbsent(this.currentTaskName, e -> new LinkedList<>()).add(info);        this.currentTaskName = null;    }    /**     * 间接记录指标数据,不局限于时间差类型     *  @param taskName 指标名     * @param data 指标数据     */    public void record(String taskName, Object data) {TaskInfo info = new TaskInfo(taskName, data);        this.taskMap.computeIfAbsent(taskName, e -> new LinkedList<>()).add(info);    }    @Getter    @AllArgsConstructor    public static final class TaskInfo {private final String taskName;        private final Object data;}}

我是仿照 org.springframework.util.StopWatch 的实现,写了 TraceWatch 类,这个办法提供了两种耗时统计的办法:

通过调用 Start(name) 和 Stop() 办法,进行耗时统计。通过调用 Record(name, timeCost),办法,间接记录耗时信息。这种形式实质上和“时间差统计”是统一的,只是抽取了一层,略微优雅了一点。

注:你能够依据本人的业务须要,自行批改 TraceWatch 外部的数据结构,我这里简略起见,外部的数据结构只是轻易举了个例子。

三、高级办法

第二节提到的两种办法,用大白话来说都是“直来直去”的感觉,咱们还能够尝试把代码写的更简便一点。

3.1 Function

在 jdk 1.8 中,引入了 java.util.function 包,通过该类提供的接口,可能实现在指定代码段的上下文执行额定代码的性能。

public class TraceHolderTest {public static void main(String[] args) {TraceWatch traceWatch = new TraceWatch();        TraceHolder.run(traceWatch, "function1", i -> {            try {                TimeUnit.SECONDS.sleep(1); // 模仿业务代码            } catch (InterruptedException e) {e.printStackTrace();            }        });        String result = TraceHolder.run(traceWatch, "function2", () -> {try {                TimeUnit.SECONDS.sleep(1); // 模仿业务代码                return "YES";            } catch (InterruptedException e) {e.printStackTrace();                return "NO";            }        });        TraceHolder.run(traceWatch, "function1", i -> {            try {                TimeUnit.SECONDS.sleep(1); // 模仿业务代码            } catch (InterruptedException e) {e.printStackTrace();            }        });        System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));    }}/* output: {"function2":[{"data":1004,"taskName":"function2"}],"function1":[{"data":1001,"taskName":"function1"},{"data":1002,"taskName":"function1"}]}*/
``````
public class TraceHolder {/**     * 有返回值调用     */    public static <T> T run(TraceWatch traceWatch, String taskName, Supplier<T> supplier) {try {            traceWatch.start(taskName);            return supplier.get();} finally {traceWatch.stop();        }    }    /**     * 无返回值调用     */    public static void run(TraceWatch traceWatch, String taskName, IntConsumer function) {try {            traceWatch.start(taskName);            function.accept(0);        } finally {traceWatch.stop();        }    }}

这里我利用了 Supplier 和 IntConsumer 接口,对外提供了有返回值和无返回值得调用,在 TraceHolder 类中,在外围代码块的前后,别离调用了前文的 TraceWatch 的办法,实现了耗时统计的性能。

3.2 AutoCloseable

除了利用 Function 的个性,咱们还能够应用 jdk 1.7 的 AutoCloseable 个性。说 AutoCloseable 可能有同学没听过,然而给大家展现下以下代码,就会立即明确是什么货色了。

// 未应用 AutoCloseablepublic static String readFirstLingFromFile(String path) throws IOException {BufferedReader br = null;    try {        br = new BufferedReader(new FileReader(path));        return br.readLine();} catch (IOException e) {e.printStackTrace();    } finally {if (br != null) {br.close();        }    }    return null;}// 应用 AutoCloseablepublic static String readFirstLineFromFile(String path) throws IOException {try (BufferedReader br = new BufferedReader(new FileReader(path))) {return br.readLine();    }}

在 try 前方能够加载一个实现了 AutoCloseable 接口的对象,该对象作用于整个 try 语句块中,并且在执行结束后回调 AutoCloseable#close() 办法。

让咱们对 TraceWatch 类进行革新:

实现 AutoCloseable 接口,实现 close() 接口:

@Overridepublic void close() {    this.stop();}

批改 start() 办法,使其反对链式调用:

public TraceWatch start(String taskName) throws IllegalStateException {if (this.currentTaskName != null) {throw new IllegalStateException("Can't start TraceWatch: it's already running");    }    this.currentTaskName = taskName;    this.startMs = TimeUtils.nowMs();        return this;}
``````
public class AutoCloseableTest {public static void main(String[] args) {TraceWatch traceWatch = new TraceWatch();        try(TraceWatch ignored = traceWatch.start("function1")) {try {                TimeUnit.SECONDS.sleep(1); // 模仿业务代码            } catch (InterruptedException e) {e.printStackTrace();            }        }        try(TraceWatch ignored = traceWatch.start("function2")) {try {                TimeUnit.SECONDS.sleep(1); // 模仿业务代码            } catch (InterruptedException e) {e.printStackTrace();            }        }        try(TraceWatch ignored = traceWatch.start("function1")) {try {                TimeUnit.SECONDS.sleep(1); // 模仿业务代码            } catch (InterruptedException e) {e.printStackTrace();            }        }        System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));    }}/* output: {"function2":[{"data":1001,"taskName":"function2"}],"function1":[{"data":1002,"taskName":"function1"},{"data":1002,"taskName":"function1"}]}*/

四、总结

本文列举了四种统计代码耗时的办法:

  • 时间差统计
  • StopWatch
  • Function
  • AutoCloseable

列举的计划是我目前能想到的计划。

正文完
 0