首页 > Java

java压力测试例子

发表于2014-07-09 15:42:50| --次阅读| 来源webkfa| 作者小豆豆

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


运行结果不贴出来了太多了,自己写个简单的测试吧

相关文章

猜你喜欢

学到老在线代码浏览器 关闭浏览
友情链接: hao123 360导航 搜狗网址导航 114啦网址导航 博客大全
Copyright © 1999-2014, WEBKFA.COM, All Rights Reserved  京ICP备14034497号-1