Bootstrap

用java 实现一个压测工具

阅读本文章之前,需要有juc相关基础知识

  • 压测工具的核心就是多线程处理并发

  • 首先我们创建一个类 HpRequest 用于处理网络的请求
package com.HpmTools.core;

import com.sun.net.httpserver.Headers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author yh
 * 用于对目标域进行请求
 */
public class HpRequest {

	/**
	* 返回压测的结果现在还未写时间测量
	*/
    static class ResponBody{
        /**
         * 是否请求成功
         */
        boolean IsSucess;
        /**
         * 请求时间
         */
        int TimeLen;

        public boolean isSucess() {
            return IsSucess;
        }

        public int getTimeLen() {
            return TimeLen;
        }

        public ResponBody(boolean isSucess, int timeLen) {
            IsSucess = isSucess;
            TimeLen = timeLen;
        }
    }
    /**
     * 开始进行网络请求
     * @param urlConnection
     */
    public static ResponBody StartRequst(HttpURLConnection urlConnection) throws IOException {

        urlConnection.setConnectTimeout(3000);
        urlConnection.setReadTimeout(4000);
        urlConnection.connect();
        return new ResponBody(urlConnection.getResponseCode()==200,100);
    }
}

  • 编写完网络请求模块 ,现在我们需要编写核心 线程处理类 HpmCore

package com.HpmTools.core;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author yh
 * 实现线程开辟等功能
 */
public class HpmCore {

    private  int PMCount=100; //默认100个请求
    private  int ConConut=10; //默认并发
    private URL url;
    private String Method;

    private static final List<String> RequstMethod   = Arrays.asList("POST","GET","PUT","DELETE") ;

    /**
     *
     * @param PMCount 总请求数量
     * @param conConut 并发数量
     * @param url 请求目标地址
     */
    public HpmCore(int PMCount, int conConut,URL url) {
        this.PMCount = PMCount;
        ConConut = conConut;
    }

    public HpmCore() {

    }
    public void SetUri(URL _url){
        url=_url;
    }

    /**
     * 添加请求头
     * @param method
     * @throws IOException
     */
    public void  AddMethod(String method) throws IOException {


        if (!RequstMethod.contains(method)){
            throw new RuntimeException("参数错误,请填写正确的参数");
        }

        this.Method=method;
    }

	/**
	* 每个线程都需要一个新的HttpURLConnection的对象
	*/
    public HttpURLConnection GetUrlConnection(){
        try {
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(Method);
            return connection;
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
    /**
     * 开始测压
     */
    public void StartManometry() throws InterruptedException {
        List<HpRequest.ResponBody> list=new LinkedList<>();

        for (int i=0;i<PMCount/ConConut;i++) {
            CountDownLatch Latch = new CountDownLatch(ConConut);
            for (int j = 0; j < ConConut; j++) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            HpRequest.ResponBody body = HpRequest.StartRequst(GetUrlConnection());
                            Latch.countDown();
                            synchronized (list) {
                                list.add(body);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();

            }
            Latch.await();
            System.out.println("第"+i+"轮并发");
        }
        System.out.println("执行完毕");
    }

}


现在我们编写测量类 目前只之前get请求

package com.HpmTools.core;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

/**
 * @author yh
 * 程序入口 处理命令
 */
public class HpCommandLine {


    public static void main(String[] args) throws IOException, InterruptedException {
        HpmCore core = new HpmCore();
        core.SetUri(new URL("http://127.0.0.1:5000/test"));
        core.AddMethod("GET");
        core.StartManometry();
    }

}

欢迎大家指正 项目地址:

github

;