接口压力测试(get请求测试)

package httpTest;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;


public class ConcurrentTest {
private static int thread_num = 200;
private static int client_num = 2000;
private static Map<Integer ,String> keywordMap = new HashMap();
static {
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\df\\account_password.csv")), "GBK");
BufferedReader buffer = new BufferedReader(isr);
String line = "";
int i = 1;
while ((line = buffer.readLine()) != null) {
keywordMap.put(i,line.split(",")[0]);
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}


public static void main(String[] args) {
int size = keywordMap.size();
// TODO Auto-generated method stub
ExecutorService exec = Executors.newCachedThreadPool();
// 50个线程可以同时访问
final Semaphore semp = new Semaphore(thread_num);
// 模拟2000个客户端访问
for (int index = 0; index < client_num; index++) {
final int NO = index;
Runnable run = new Runnable() {
public void run() {
try {
// 获取许可
semp.acquire();
System.out.println("Thread:" + NO);
String member = getRandomId(NO);
String phone =  getRandomSearchKey(NO);
String host = "http://localhost:8080/data_plat/countInterface?member="+member+"&&phone="+phone;

get(host);
// 释放
System.out.println("第:" + NO + " 个");
semp.release();
} catch (Exception e) {
e.printStackTrace();
}
}
};
exec.execute(run);
}
// 退出线程池
exec.shutdown();
}


private static String getRandomSearchKey(final int no) {
int size = keywordMap.size();
  Random rand = new Random();
  int wanna =rand.nextInt(size);

return (String) keywordMap.get(wanna+1);
}
private static String getRandomId(final int no) {
Map <Integer ,String>idMap = new HashMap();
idMap.put(1, "1");
idMap.put(2, "18e12a40f30144ba8aba1e7508bba237");
idMap.put(3, "2");
idMap.put(4, "2c2a0c5b36414f4ab6862a2d613a7e42");
idMap.put(5, "327a06ddb8fc4f4eb70201c225fb5af1");
idMap.put(6, "c7396e6267bf4084ae3a0e28912daf74");
idMap.put(7, "e3c4c759cec54402afc0ae9e4982901b");

int size = idMap.size();

  Random rand = new Random();
  int wanna =rand.nextInt(size);


return (String) idMap.get(wanna+1);
}
 
public static String get(String url) {  
        BufferedReader in = null;  
        try {  
            URL realUrl = new URL(url);  
            // 打开和URL之间的连接  
            URLConnection connection = realUrl.openConnection();  
            // 设置通用的请求属性  
            connection.setRequestProperty("accept", "*/*");  
            connection.setRequestProperty("connection", "Keep-Alive");  
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
            connection.setConnectTimeout(5000);  
            connection.setReadTimeout(5000);  
            // 建立实际的连接  
            connection.connect();  
            // 定义 BufferedReader输入流来读取URL的响应  
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));  
            StringBuffer sb = new StringBuffer();  
            String line;  
            while ((line = in.readLine()) != null) {  
                sb.append(line);  
            }  
            return sb.toString();  
        } catch (Exception e) {  
        }  
        // 使用finally块来关闭输入流  
        finally {  
            try {  
                if (in != null) {  
                    in.close();  
                }  
            } catch (Exception e2) {  
                e2.printStackTrace();  
            }  
        }  
        return null;  
    }  


}

猜你喜欢

转载自blog.csdn.net/Living_and_learning/article/details/80652729