首頁 > 軟體

手寫java效能測試框架的實現範例

2022-07-21 18:02:16

引言

之前寫過一個效能測試框架,只是針對單一的HTTP介面的測試,對於業務介面和非HTTP介面還無非適配,剛好前端時間工作中用到了,就更新了自己的測試框架,這次不再以請求為基礎,而是以方法為基礎,這樣就可以避免了單一性,有一個base類,然後其他的各種單一性請求在單獨寫一個適配類就好了,如果只是臨時用,直接重新實現base即可。

程式碼分享

package com.fun.frame.thead;
import com.fun.frame.SourceCode;
import com.fun.frame.excute.Concurrent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import static com.fun.utils.Time.getTimeStamp;
/**
 * 多執行緒任務基礎類別,可單獨使用
 */
public abstract class ThreadBase<T> extends SourceCode implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(ThreadBase.class);
    /**
     * 任務請求執行次數
     */
    public int times;
    /**
     * 計數鎖
     * <p>
     * 會在concurrent類裡面根據執行緒數自動設定
     * </p>
     */
    CountDownLatch countDownLatch;
    /**
     * 用於設定存取資源
     */
    public T t;
    public ThreadBase(T t) {
        this();
        this.t = t;
    }
    public ThreadBase() {
        super();
    }
    /**
     * groovy無法直接存取t,所以寫了這個方法
     *
     * @return
     */
    public String getT() {
        return t.toString();
    }
    @Override
    public void run() {
        try {
            before();
            List<Long> t = new ArrayList<>();
            long ss = getTimeStamp();
            for (int i = 0; i < times; i++) {
                long s = getTimeStamp();
                doing();
                long e = getTimeStamp();
                t.add(e - s);
            }
            long ee = getTimeStamp();
            logger.info("執行次數:{},總耗時:{}", times, ee - ss);
            Concurrent.allTimes.addAll(t);
        } catch (Exception e) {
            logger.warn("執行任務失敗!", e);
        } finally {
            after();
            if (countDownLatch != null)
                countDownLatch.countDown();
        }
    }
    /**
     * 執行待測方法的之前的準備
     */
    protected abstract void before();
    /**
     * 待測方法
     *
     * @throws Exception
     */
    protected abstract void doing() throws Exception;
    /**
     * 執行待測方法後的處理
     */
    protected abstract void after();
    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
    public void setTimes(int times) {
        this.times = times;
    }
}

基礎類實現

下面是幾個實現過的基礎類:

package com.fun.frame.thead;
import com.fun.httpclient.ClientManage;
import com.fun.httpclient.FanLibrary;
import com.fun.httpclient.GCThread;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
 * http請求多執行緒類
 */
public class RequestThread extends ThreadBase {
    static Logger logger = LoggerFactory.getLogger(RequestThread.class);
    /**
     * 請求
     */
    public HttpRequestBase request;
    /**
     * 單請求多執行緒多次任務構造方法
     *
     * @param request 被執行的請求
     * @param times   每個執行緒執行的次數
     */
    public RequestThread(HttpRequestBase request, int times) {
        this.request = request;
        this.times = times;
    }
    @Override
    public void before() {
        request.setConfig(FanLibrary.requestConfig);
        GCThread.starts();
    }
    @Override
    protected void doing() throws Exception {
        getResponse(request);
    }
    @Override
    protected void after() {
        GCThread.stop();
    }
    /**
     * 多次執行某個請求,但是不記錄紀錄檔,記錄方法用 loglong
     * <p>此方法只適應與單個請求的重複請求,對於有業務聯絡的請求暫時不能適配</p>
     *
     * @param request 請求
     * @throws IOException
     */
    void getResponse(HttpRequestBase request) throws IOException {
        CloseableHttpResponse response = ClientManage.httpsClient.execute(request);
        String content = FanLibrary.getContent(response);
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
            logger.warn("響應狀態碼:{},響應內容:{}", content, response.getStatusLine());
        if (response != null) response.close();
    }
}

資料庫的實現

package com.fun.frame.thead;
import com.fun.interfaces.IMySqlBasic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
/**
 * 資料庫多執行緒類
 */
public class QuerySqlThread extends ThreadBase {
    private static Logger logger = LoggerFactory.getLogger(QuerySqlThread.class);
    String sql;
    IMySqlBasic base;
    public QuerySqlThread(IMySqlBasic base, String sql, int times) {
        this.times = times;
        this.sql = sql;
        this.base = base;
    }
    @Override
    public void before() {
        base.getConnection();
    }
    @Override
    protected void doing() throws SQLException {
        base.excuteQuerySql(sql);
    }
    @Override
    protected void after() {
        base.mySqlOver();
    }
}

concurrent類

package com.fun.frame.excute;
import com.fun.bean.PerformanceResultBean;
import com.fun.frame.Save;
import com.fun.frame.SourceCode;
import com.fun.frame.thead.ThreadBase;
import com.fun.profile.Constant;
import com.fun.utils.Time;
import com.fun.utils.WriteRead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Concurrent {
    private static Logger logger = LoggerFactory.getLogger(Concurrent.class);
    /**
     * 執行緒任務
     */
    public ThreadBase thread;
    public List<ThreadBase> threads;
    public int num;
    public static Vector<Long> allTimes = new Vector<>();
    ExecutorService executorService;
    CountDownLatch countDownLatch;
    /**
     * @param thread 執行緒任務
     * @param num    執行緒數
     */
    public Concurrent(ThreadBase thread, int num) {
        this(num);
        this.thread = thread;
    }
    /**
     * @param threads 執行緒組
     */
    public Concurrent(List<ThreadBase> threads) {
        this(threads.size());
        this.threads = threads;
    }
    public Concurrent(int num) {
        this.num = num;
        executorService = Executors.newFixedThreadPool(num);
        countDownLatch = new CountDownLatch(num);
    }
    /**
     * 執行多執行緒任務
     */
    public PerformanceResultBean start() {
        long start = Time.getTimeStamp();
        for (int i = 0; i < num; i++) {
            ThreadBase thread = getThread(i);
            thread.setCountDownLatch(countDownLatch);
            executorService.execute(thread);
        }
        shutdownService(executorService, countDownLatch);
        long end = Time.getTimeStamp();
        logger.info("總計" + num + "個執行緒,共用時:" + Time.getTimeDiffer(start, end) + "秒!");
        return over();
    }
    private static void shutdownService(ExecutorService executorService, CountDownLatch countDownLatch) {
        try {
            countDownLatch.await();
            executorService.shutdown();
        } catch (InterruptedException e) {
            logger.warn("執行緒池關閉失敗!", e);
        }
    }
    private PerformanceResultBean over() {
        Save.saveLongList(allTimes, num);
        return countQPS(num);
    }
    ThreadBase getThread(int i) {
        if (threads == null) return thread;
        return threads.get(i);
    }
    /**
     * 計算結果
     * <p>此結果僅供參考</p>
     *
     * @param name 執行緒數
     */
    public static PerformanceResultBean countQPS(int name) {
        List<String> strings = WriteRead.readTxtFileByLine(Constant.LONG_Path + name + Constant.FILE_TYPE_LOG);
        int size = strings.size();
        int sum = 0;
        for (int i = 0; i < size; i++) {
            int time = SourceCode.changeStringToInt(strings.get(i));
            sum += time;
        }
        double v = 1000.0 * size * name / sum;
        PerformanceResultBean performanceResultBean = new PerformanceResultBean(name, size, sum / size, v);
        performanceResultBean.print();
        return performanceResultBean;
    }
}

redis實現類缺失,因為沒有遇到需要單獨實現的需求。

關於用程式碼還是用工具實現並行,我個人看法所有所長,單究其根本,必然是程式碼勝於工具,原因如下:門檻高,適應性強;貼近開發,利於調優。

效能測試,並行只是開始,只有一個好的開始才能進行效能資料分析,效能引數調優。所以不必拘泥於到底使用哪個工具那種語言,據我經驗來說:基本的測試需求都是能滿足的,只是實現的代價不同。

groovy是一種基於JVM的動態語言,我覺得最大的優勢有兩點

  • 第一:於java相容性非常好,大部分時候吧groovy的檔案字尾改成java直接可以用,反之亦然。java的絕大部分庫,groovy都是可以直接拿來就用的。這還帶來了另外一個有點,學習成本低,非常低,直接上手沒問題,可以慢慢學習groovy不同於Java的語法;
  • 第二:編譯器支援變得更好,現在用的intellij的ide,總體來說已經比較好的支援groovy語言了,寫起程式碼來也是比較順滑了,各種基於groovy的框架工具也比較溜,特別是Gradle構建工具,比Maven爽很多。

以上就是java效能測試框架手寫實現範例的詳細內容,更多關於java 效能測試框架的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com