1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        如何優(yōu)雅地統(tǒng)計代碼耗時?

        共 15983字,需瀏覽 32分鐘

         ·

        2021-03-03 23:43

        公眾號關(guān)注 “GitHub今日熱榜
        設(shè)為 “星標(biāo)”,帶你挖掘更多開發(fā)神器!





        一、前言


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


        可能也是受限于 Java 的語言特性,總覺得代碼寫起來不夠優(yōu)雅,大量的耗時統(tǒng)計代碼,干擾了業(yè)務(wù)邏輯。特別是開發(fā)功能的時候,有個感受就是剛剛開發(fā)完代碼很清爽優(yōu)雅,結(jié)果加了一大堆輔助代碼后,整個代碼就變得臃腫了,自己看著都挺難受。因此總想著能不能把這塊寫的更優(yōu)雅一點,今天本文就嘗試探討下“代碼耗時統(tǒng)計”這一塊。


        在開始正文前,先說下前提,“代碼耗時統(tǒng)計”的并不是某個方法的耗時,而是任意代碼段之間的耗時。這個代碼段,可能是一個方法中的幾行代碼,也有可能是從這個方法的某一行到另一個被調(diào)用方法的某一行,因此通過 AOP 方式是不能實現(xiàn)這個需求的。


        二、常規(guī)方法


        2.1 時間差統(tǒng)計


        這種方式是最簡單的方法,記錄下開始時間,再記錄下結(jié)束時間,計算時間差即可。


        public class TimeDiffTest {
            public static void main(String[] args) throws InterruptedException {
                final long startMs = TimeUtils.nowMs();

                TimeUnit.SECONDS.sleep(5); // 模擬業(yè)務(wù)代碼

                System.out.println("timeCost: " + TimeUtils.diffMs(startMs));
            }
        }

        /* output:
        timeCost: 5005
        */


        public class TimeUtils {
            /**
             * @return 當(dāng)前毫秒數(shù)
             */

            public static long nowMs() {
                return System.currentTimeMillis();
            }

            /**
             * 當(dāng)前毫秒與起始毫秒差
             * @param startMillis 開始納秒數(shù)
             * @return 時間差
             */

            public static long diffMs(long startMillis) {
               return diffMs(startMillis, nowMs());
            }
        }


        這種方式的優(yōu)點是實現(xiàn)簡單,利于理解;缺點就是對代碼的侵入性較大,看著很傻瓜,不優(yōu)雅。


        2.2 StopWatch


        第二種方式是參考 StopWatch ,StopWatch 通常被用作統(tǒng)計代碼耗時,各個框架和 Common 包都有自己的實現(xiàn)。


        public class TraceWatchTest {
            public static void main(String[] args) throws InterruptedException {
                TraceWatch traceWatch = new TraceWatch();

                traceWatch.start("function1");
                TimeUnit.SECONDS.sleep(1); // 模擬業(yè)務(wù)代碼
                traceWatch.stop();

                traceWatch.start("function2");
                TimeUnit.SECONDS.sleep(1); // 模擬業(yè)務(wù)代碼
                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<>();

            /**
             * 開始時間差類型指標(biāo)記錄,如果需要終止,請調(diào)用 {@link #stop()}
             *
             * @param taskName 指標(biāo)名
             */

            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();
            }

            /**
             * 終止時間差類型指標(biāo)記錄,調(diào)用前請確保已經(jīng)調(diào)用
             */

            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;
            }

            /**
             * 直接記錄指標(biāo)數(shù)據(jù),不局限于時間差類型
             * @param taskName 指標(biāo)名
             * @param data 指標(biāo)數(shù)據(jù)
             */

            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 的實現(xiàn),寫了 TraceWatch 類,這個方法提供了兩種耗時統(tǒng)計的方法:


        • 通過調(diào)用 Start(name) 和 Stop() 方法,進(jìn)行耗時統(tǒng)計。

        • 通過調(diào)用 Record(name, timeCost),方法,直接記錄耗時信息。


        這種方式本質(zhì)上和“時間差統(tǒng)計”是一致的,只是抽取了一層,稍微優(yōu)雅了一點。


        注:你可以根據(jù)自己的業(yè)務(wù)需要,自行修改 TraceWatch 內(nèi)部的數(shù)據(jù)結(jié)構(gòu),我這里簡單起見,內(nèi)部的數(shù)據(jù)結(jié)構(gòu)只是隨便舉了個例子。


        三、高級方法


        第二節(jié)提到的兩種方法,用大白話來說都是“直來直去”的感覺,我們還可以嘗試把代碼寫的更簡便一點。


        3.1 Function


        在 jdk 1.8 中,引入了 java.util.function 包,通過該類提供的接口,能夠?qū)崿F(xiàn)在指定代碼段的上下文執(zhí)行額外代碼的功能。


        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<>();

            /**
             * 開始時間差類型指標(biāo)記錄,如果需要終止,請調(diào)用 {@link #stop()}
             *
             * @param taskName 指標(biāo)名
             */

            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();
            }

            /**
             * 終止時間差類型指標(biāo)記錄,調(diào)用前請確保已經(jīng)調(diào)用
             */

            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;
            }

            /**
             * 直接記錄指標(biāo)數(shù)據(jù),不局限于時間差類型
             * @param taskName 指標(biāo)名
             * @param data 指標(biāo)數(shù)據(jù)
             */

            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;
            }
        }


        public class TraceHolder {
            /**
             * 有返回值調(diào)用
             */

            public static <T> T run(TraceWatch traceWatch, String taskName, Supplier<T> supplier) {
                try {
                    traceWatch.start(taskName);

                    return supplier.get();
                } finally {
                    traceWatch.stop();
                }
            }

            /**
             * 無返回值調(diào)用
             */

            public static void run(TraceWatch traceWatch, String taskName, IntConsumer function) {
                try {
                    traceWatch.start(taskName);

                    function.accept(0);
                } finally {
                    traceWatch.stop();
                }
            }
        }


        這里我利用了 Supplier 和 IntConsumer 接口,對外提供了有返回值和無返回值得調(diào)用,在 TraceHolder 類中,在核心代碼塊的前后,分別調(diào)用了前文的 TraceWatch 的方法,實現(xiàn)了耗時統(tǒng)計的功能。


        3.2 AutoCloseable


        除了利用 Function 的特性,我們還可以使用 jdk 1.7 的 AutoCloseable 特性。說 AutoCloseable 可能有同學(xué)沒聽過,但是給大家展示下以下代碼,就會立刻明白是什么東西了。


        // 未使用 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 后方可以加載一個實現(xiàn)了 AutoCloseable 接口的對象,該對象作用于整個 try 語句塊中,并且在執(zhí)行完畢后回調(diào) AutoCloseable#close() 方法。


        讓我們對 TraceWatch 類進(jìn)行改造:


        1.實現(xiàn) AutoCloseable 接口,實現(xiàn) close() 接口:


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


        2.修改 start() 方法,使其支持鏈?zhǔn)秸{(diào)用:


        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); // 模擬業(yè)務(wù)代碼
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                try(TraceWatch ignored = traceWatch.start("function2")) {
                    try {
                        TimeUnit.SECONDS.sleep(1); // 模擬業(yè)務(wù)代碼
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                try(TraceWatch ignored = traceWatch.start("function1")) {
                    try {
                        TimeUnit.SECONDS.sleep(1); // 模擬業(yè)務(wù)代碼
                    } 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"}]}
        */


        四、總結(jié)


        本文列舉了四種統(tǒng)計代碼耗時的方法:


        1.時間差統(tǒng)計

        2.StopWatch

        3.Function

        4.AutoCloseable


        列舉的方案是我目前能想到的方案。當(dāng)然可能有更加優(yōu)雅的方案,希望有相關(guān)經(jīng)驗的同學(xué)能在評論區(qū)一起交流下~


        出處: https://jitwxs.cn/5aa91d10.html








        關(guān)注GitHub今日熱榜,專注挖掘好用的開發(fā)工具,致力于分享優(yōu)質(zhì)高效的工具、資源、插件等,助力開發(fā)者成長!







        點個在看 你最好看









        瀏覽 73
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            受被很多人c屁股在外面被c | 露娜用孙悟空的金箍棒戳哪 | 三年大片观看哔哩哔哩 | 99热r| 九色蝌蚪9l视频蝌蚪9l视频开放 | 天天想夜夜操 | 韩日毛片| 女生被操的视频网站 | 91黄免费 | 日日躁夜夜摸月月添添添 |