java 写http压测

本文只是写个简单的小程序
第一个类制作自己的实例,并在自己的实例中创建线程和第二个类的实例,线程中不断循环调用第二个类的方法

第二个类负责访问http连接,判断返回状态

public class HttpTest {


    private ConcurrentLinkedQueue<String>  concurrentLinkedQueue = new ConcurrentLinkedQueue<String>();
    private static int count_up = 0;
    private static int count_get = 0;
    private static boolean isRun = true;
    private static final int threadSize = 2;

    public static void main(String[] arg){

        for(int i = 0; i<threadSize ;i++){
            HttpTest httpTest = new HttpTest();
            httpTest.input();
            httpTest.output();
        }
    }

    private void input(){
            getInputThread().start();
    }

    private void output(){
            getOutputThread().start();
    }

    private Thread getInputThread(){

        return new Thread(new Runnable() {
            public void run() {

                while (isRun){

                    long start = System.currentTimeMillis();

                    String key = Test.getkey();

                    new PYDeepLink().callUp(key);

//                    inputList.add(key);
                    concurrentLinkedQueue.add(key);
                    count_up++;

                    if(count_up%1000 == 0){

                        System.out.println("up:" + count_up);
                        System.out.println("------------ 输入 : " + (System.currentTimeMillis() - start));

                    }
                }
            }
        });
    }

    private Thread getOutputThread(){

        return new Thread(new Runnable() {
            public void run() {
            while (isRun){

                long start = System.currentTimeMillis();

                String key = concurrentLinkedQueue.poll();
                if(null != key){
                    new PYDeepLink().callGet(key);
                    count_get++;
                    if(count_get%1000 == 0) {
                        System.out.println("gt : " + count_get);
                        System.out.println("------------ 获取 : " + (System.currentTimeMillis() - start));
                    }
                }

            }
            }
        });
    }
}

第二个类 :

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

class PYDeepLink {

    private static final String URL_BASE_UP = "http://.../up?v=";
    private static final String URL_BASE_GET = "http://.../get?v=";
    private static final int TIME_OUT = 2000;


    void callUp(String key){
        httpUp(URL_BASE_UP + key);
    }

    void callGet(String key){
        httpGet(URL_BASE_GET + key);
    }

    private void httpUp(final String httpUrl) {
        HttpURLConnection httpURLConnection = null;
        InputStream inputStream = null;
        try {
            URL url = new URL(httpUrl);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(TIME_OUT);
            inputStream = httpURLConnection.getInputStream();

            byte[]  str = new byte[1];
            int readValue = inputStream.read(str);
                        // 
            if (readValue == -1 || !new String(str).equals("t") || httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("Up server return error");
            }

        } catch (Exception e) {
            System.out.println("connect error");
        } finally {
            if(null != inputStream)
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (null != httpURLConnection)
                httpURLConnection.disconnect();

        }
    }

    private void httpGet(final String httpUrl) {

        HttpURLConnection httpURLConnection = null;
        InputStream inputStream = null;
        try {
            URL url = new URL(httpUrl);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(false);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(TIME_OUT);

            inputStream = httpURLConnection.getInputStream();

            byte[]  str = new byte[18];
            int readValue = inputStream.read(str);
                        // 
            if (readValue == -1 || !new String(str).equals("t") || httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("Get server return error");
            }


        } catch (Exception e) {
            System.out.println("connect error");
        } finally {
            if (null != inputStream)
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (null != httpURLConnection)
                httpURLConnection.disconnect();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值