今天寫了一個可以測試併發數和執行次數的壓力測試程式碼。(Java)

dawa大娃bigbaby發表於2022-04-12

今天寫了一個可以測試併發數和執行次數的壓力測試程式碼

  1. 介紹一下為什麼會寫這麼一個工具。
  2. 介紹一個這個工具怎麼用的。

背景

最近在開發CoapServer端,以及模擬裝置側傳送資料呼叫開發好的CoapServer的效能,進行壓力測試。

自己沒有找到合適的壓力測試的工具,但是測試訴求相對比較簡單,覺得用Java可以來控制測試。
測試維度:

  1. 一共模擬1W臺裝置,共計傳送資料100W次
  2. 模擬多臺裝置同時傳送資料。

程式碼和使用

import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.Utils;
import org.eclipse.californium.elements.exception.ConnectorException;

import java.io.IOException;
import java.text.NumberFormat;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PressTestClient {

    static int count = 0;
    //總訪問量是client_num,併發量是thread_num
    int thread_num = 10;
    int client_num = 1000;

    float avg_exec_time = 0;
    float sum_exec_time = 0;
    long first_exec_time = Long.MAX_VALUE;
    long last_done_time = Long.MIN_VALUE;
    float total_exec_time = 0;

    String url = "";
    String postData = "";

    public PressTestClient(int thread_num, int client_num, String url, String postData) {

        this.thread_num = thread_num;
        this.client_num = client_num;
        this.url = url;
        this.postData = postData;
    }


    public void run() {

        final PressTestClient currentObj = this;

        final ConcurrentHashMap<Integer, ClientThreadRecord> records = new ConcurrentHashMap<Integer, ClientThreadRecord>();

        // 建立ExecutorService執行緒池
        ExecutorService exec = Executors.newFixedThreadPool(thread_num);
        // thread_num個執行緒可以同時訪問
        // 模擬client_num個客戶端訪問
        final CountDownLatch doneSignal = new CountDownLatch(client_num);

        for (int i = 0; i < client_num; i++) {

            Runnable run = new Runnable() {

                public void run() {

                    int index = getIndex();
                    long st = System.currentTimeMillis();

                    try {
                        //測試的邏輯程式碼
                        TlsCoAPClient example = new TlsCoAPClient();
                        CoapClient coapClient = example.getClient("device_service");
                        CoapResponse response = null;
                        try {
                            System.out.println("start client request:" +index );
                            response = coapClient.get();
                            System.out.println("device_service: " + Utils.prettyPrint(response));
                            Thread.sleep(100);
                        } catch (ConnectorException | IOException e) {
                            e.printStackTrace();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    records.put(index, new ClientThreadRecord(st, System.currentTimeMillis()));
                    doneSignal.countDown();//每呼叫一次countDown()方法,計數器減1
                }
            };
            exec.execute(run);
        }

        try {
            //計數器大於0 時,await()方法會阻塞程式繼續執行
            doneSignal.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        /**
         * 獲取每個執行緒的開始時間和結束時間
         */
        for (int i : records.keySet()) {
            ClientThreadRecord r = records.get(i);
            sum_exec_time += ((double) (r.et - r.st)) / 1000;

            if (r.st < first_exec_time) {
                first_exec_time = r.st;
            }
            if (r.et > last_done_time) {
                this.last_done_time = r.et;
            }
        }

        this.avg_exec_time = this.sum_exec_time / records.size();
        this.total_exec_time = ((float) (this.last_done_time - this.first_exec_time)) / 1000;
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(4);


        System.out.println("======================================================");
        System.out.println("Thread Num: " + thread_num + ", Client Count: " + client_num + ".");
        System.out.println("Avg Exec Time:   " + nf.format(this.avg_exec_time) + " s");
        System.out.println("Total Exec Time: " + nf.format(this.total_exec_time) + " s");
        System.out.println("Throughput:      " + nf.format(this.client_num / this.total_exec_time) + " /s");
    }

    public static int getIndex() {
        return ++count;
    }

    public static void main(String[] args) {
        //總訪問量和併發量兩重迴圈,依次增大訪問
        //訪問量
        for (int j = 500; j < 501; j += 100) {
            //併發量
            for (int i = 500; i < 501; i += 1) {
                //要測試的URL
                String url = "http://www.baidu.com/";
                new PressTestClient(i, j, url, "").run();
            }
        }
        System.out.println("finished!");
    }
}

class ClientThreadRecord {
    long st;
    long et;

    public ClientThreadRecord(long st, long et) {
        this.st = st;
        this.et = et;
    }
}

如何使用?

  1. main方法中的迴圈此時是控制 執行數和併發數的
  2. 上面run方法,是控制你要測試的程式碼的。可以自定義。

效果展示

執行圖

執行圖

相關文章