分享幾種優雅統計代碼耗時的方式,快速查明程序慢在哪裏!

推薦閱讀:

一、前言

代碼耗時統計在日常開發中算是一個十分常見的需求,特別是在需要找出代碼性能瓶頸時。

可能也是受限於 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 可能有同學沒聽過,但是給大家展示下以下代碼,就會立刻明白是什麼東西了。

// 未使用 AutoCloseable
public 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;
}

// 使用 AutoCloseable
public static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }
}

在 try 後方可以加載一個實現了 AutoCloseable 接口的對象,該對象作用於整個 try 語句塊中,並且在執行完畢後回調 AutoCloseable#close() 方法。

讓我們對 TraceWatch 類進行改造:

「1.實現 AutoCloseable 接口,實現 close() 接口:」

@Override
public void close() {
    this.stop();
}

「2.修改 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

列舉的方案是我目前能想到的方案。當然可能有更加優雅的方案,希望有相關經驗的同學能在評論區一起交流下~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章