1. 程式人生 > >java 寫http壓測

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();
        }
    }
}