public class CallHttpRequest implements Runnable { private static Logger log = Logger.getLogger(CallHttpRequest.class); public static int successRequest = 0; public static int failRequest = 0; public static int timeOutRequest = 0; private final String hostString = "http://localhost:"; private String port; private String puductPartURL;
public static void main(String[] args) throws Exception { CountDownLatch begin = new CountDownLatch(1);
StringBuilder stringBuilder = new StringBuilder(); String filePath = args[0]; FileReader fr = new FileReader(filePath); BufferedReader br = new BufferedReader(fr); while (br.ready()) { stringBuilder.append(br.readLine()); } String stringAll = stringBuilder.toString(); ArrayList<String> arrayList = fetchUrlFromFile(stringAll); int allRequestSize = arrayList.size(); log.info("all request size is " + allRequestSize); //设置最大的并发数量为60 ExecutorService exec = Executors.newFixedThreadPool(60);
CountDownLatch end = new CountDownLatch(allRequestSize); // int i = 0; for (String str : arrayList) { exec.execute(new CallHttpRequest(portArray[0], str, begin, end));
/*如果想测试60个线程并发的访问,发配到同一台服务器上的两个tomcat,就用下面注释掉的代码 * if (i % 2 == 0) { exec.execute(new CallHttpRequest(portArray[0], str, begin, end)); } else if (i % 2 == 1) { exec.execute(new CallHttpRequest(portArray[1], str, begin, end)); } */ // i++; } long startTime = System.currentTimeMillis(); //当60个线程,初始化完成后,解锁,让六十个线程在4个双核的cpu服务器上一起竞争着跑,来模拟60个并发线程访问tomcat begin.countDown();
try { end.await(); } catch (InterruptedException e) { e.printStackTrace(); } finally { log.info("all url requests is done!"); log.info("the success size: " + CallHttpRequest.successRequest); log.info("the fail size: " + CallHttpRequest.failRequest); log.info("the timeout size: " + CallHttpRequest.timeOutRequest); double successRate = (double)CallHttpRequest.successRequest / allRequestSize; log.info("the success rate is: " + successRate*100+"%"); long endTime = System.currentTimeMillis(); long costTime = endTime - startTime; log.info("the total time cost is: " + costTime + " ms"); log.info("every request time cost is: " + costTime / allRequestSize + " ms"); } exec.shutdown(); log.info("main method end");