java压力测试例子
摘要:java压力测试例子
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class Test { //200个线程可以同时访问 private static int thread_num = 200; //模拟1000个客户端访问 private static int client_num = 1000; //要测试的地址 private static String test_url="http://115.28.89.134/"; public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(); final Semaphore semp = new Semaphore(thread_num); 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 host = test_url; System.out.println(host); URL url = new URL(host); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoOutput(true); connection.setDoInput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); out.flush(); out.close(); BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String line = ""; String result = ""; while ((line = in.readLine()) != null) { result += line; } System.out.println("线程"+ NO+":"+result+"test"); // 释放 semp.release(); } catch (Exception e) { e.printStackTrace(); } } }; exec.execute(run); } // 退出线程池 exec.shutdown(); } }
运行结果不贴出来了太多了,自己写个简单的测试吧
相关文章
最新发布
阅读排行
热门文章
猜你喜欢