您的位置:首页 > 理论基础 > 计算机网络

java 写http压测

2016-06-27 10:40 459 查看
本文只是写个简单的小程序

第一个类制作自己的实例,并在自己的实例中创建线程和第二个类的实例,线程中不断循环调用第二个类的方法

第二个类负责访问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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: