JDK1.8 - lamda 接口耗時統計

前言

由於平時開發測試中,經常需要查看目標方法的耗時情況,每次都要在目標方法前後寫相同的時間計算代碼,於是呢,正好利用1.8的新特性 lamda 實現一個常用的util。
順便記錄分享一下。

實現方式

修改前耗時統計方法

先來看看常見的統計接口方法執行耗時的代碼:

@Slf4j
public class TestTimer {
    public static void task1(){
        try {
            log.info("執行任務開始..");
            Thread.sleep(1000);
            log.info("執行任務結束..");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test1(){
        long start = System.currentTimeMillis();
        task1();
        long end = System.currentTimeMillis();
        log.info("線程:{}, 運行時間:{} ms"
                , Thread.currentThread().getName(), (end - start));
    }
}

執行結果:
[INFO ][20:56:05 TestTimer:16 line] 執行任務開始..
[INFO ][20:56:06 TestTimer:18 line] 執行任務結束..
[INFO ][20:56:06 TestTimer:28 line] 線程:main, 運行時間:1002 ms

修改後耗時統計方法

定義一個工具類 HandleTimer, 用於統計方法執行耗時。
這裏重載了四種形式的耗時統計:

import lombok.extern.slf4j.Slf4j;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @Date: 2019/12/28 18:36
 * @Author: swotXu
 */
@Slf4j
public class HandleTimer {
    /**
     * 獲取方法執行時間
     *  -- 執行任務
     * @param handle    監聽的方法
     */
    public static void runTime(Runnable handle){
        long start = System.currentTimeMillis();
        handle.run();
        long end = System.currentTimeMillis();
        log.info("線程:{}, 運行時間:{} ms"
                , Thread.currentThread().getName(), (end - start));
    }
    /**
     * 獲取方法執行時間
     *  -- 消費者接口
     * @param handle    監聽的方法
     * @param t    監聽的方法所需參數
     * @param <T>   參數類型
     */
    public static <T> void runTime(Consumer<T> handle, T t){
        long start = System.currentTimeMillis();
        handle.accept(t);
        long end = System.currentTimeMillis();
        log.info("線程:{}, 計算:{}, 運行時間:{} ms"
                , Thread.currentThread().getName(), t, (end - start));
    }
    /**
     * 獲取方法執行時間
     *  -- 生產者接口
     * @param handle    監聽的方法
     * @param <R>       返回值類型
     */
    public static <R> R runTime(Supplier<R> handle){
        long start = System.currentTimeMillis();
        R r = handle.get();
        long end = System.currentTimeMillis();
        log.info("線程:{}, 結果:{}, 運行時間:{} ms"
                , Thread.currentThread().getName(), r, (end - start));
        return r;
    }
    /**
     * 獲取方法執行時間
     *   -- 轉換接口
     * @param handle    監聽的方法
     * @param t    監聽的方法所需參數
     * @param <T>       參數類型
     * @param <R>       返回值類型
     */
    public static <T, R> R runTime(Function<T, R> handle, T t){
        long start = System.currentTimeMillis();
        R apply = handle.apply(t);
        long end = System.currentTimeMillis();
        log.info("線程:{}, 計算:{} = {}, 運行時間:{} ms"
                , Thread.currentThread().getName(), t, apply, (end - start));
        return apply;
    }
}

代碼演示

  1. void runTime(Runnable handle)
    用於無參數無返回值的方法統計
public static void task2(){
    try {
        log.info("執行任務開始..");
        Thread.sleep(1000);
        log.info("執行任務結束..");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
@Test
public void test2(){
    HandleTimer.runTime(TestTimer::task2);
}

結果:

[INFO ][22:32:51 TestTimer:16 line] 執行任務開始..
[INFO ][22:32:52 TestTimer:18 line] 執行任務結束..
[INFO ][22:32:52 HandleTimer:26 line] 線程:main, 運行時間:1006 ms
  1. <T> void runTime(Consumer<T> handle, T t)
    用於有參無返回值的方法統計
public static void task3(int i){
    try {
        log.info("執行任務開始.. i: {}", i);
        Thread.sleep(1000);
        log.info("執行任務結束..");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
@Test
public void test3(){
    HandleTimer.runTime(TestTimer::task3, 666);
}

結果:

[INFO ][22:37:11 TestTimer:48 line] 執行任務開始.. i: 666
[INFO ][22:37:12 TestTimer:50 line] 執行任務結束..
[INFO ][22:37:12 HandleTimer:40 line] 線程:main, 計算:666, 運行時間:1009 ms
  1. <R> R runTime(Supplier<R> handle)
    用於無參有返回值的方法統計
public static int task4(){
    try {
        log.info("執行任務開始..");
        Thread.sleep(1000);
        log.info("執行任務結束..");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return 666;
}
@Test
public void test4(){
    Integer result = HandleTimer.runTime(TestTimer::task4);
}

結果:

[INFO ][22:39:12 TestTimer:62 line] 執行任務開始..
[INFO ][22:39:13 TestTimer:64 line] 執行任務結束..
[INFO ][22:39:13 HandleTimer:53 line] 線程:main, 結果:666, 運行時間:1004 ms
  1. <T, R> R runTime(Function<T, R> handle, T t)
    用於有參有返回值的方法統計
public static String task5(int t){
    try {
        log.info("執行任務開始.. t: {}", t);
        Thread.sleep(1000);
        log.info("執行任務結束..");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "233";
}
@Test
public void test5(){
    String result = HandleTimer.runTime(TestTimer::task5, 666);
}

結果:

[INFO ][22:41:41 TestTimer:77 line] 執行任務開始.. t: 666
[INFO ][22:41:42 TestTimer:79 line] 執行任務結束..
[INFO ][22:41:42 HandleTimer:69 line] 線程:main, 計算:666 = 233, 運行時間:1005 ms

大家如果有什麼疑問,歡迎評論留言!別忘了收藏關注~

發佈了6 篇原創文章 · 獲贊 5 · 訪問量 771
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章