- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.atomic.AtomicInteger;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PutMethod;
- import org.apache.commons.pool.impl.GenericObjectPool.Config;
- /**
- * A simple example Java client which uses the built-in java.net.URLConnection.
- *
- * @author BryantR
- * @author Greg Luck
- */
- public class BenchMark {
- private static String TABLE_COLUMN_ELEMENT =
- "http://x.x.x.x:7001/ehcache-server-1.0.0/rest/tableColumn/1";
- /**
- * Creates a new instance of EHCacheREST
- */
- public static void main(String[] args) {
- URL url;
- HttpURLConnection connection = null;
- InputStream is = null;
- OutputStream os = null;
- int result = 0;
- try {
- withPool();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (os != null) try {
- os.close();
- } catch (Exception ignore) {
- }
- if (is != null) try {
- is.close();
- } catch (Exception ignore) {
- }
- if (connection != null) connection.disconnect();
- }
- }
- private static final int TOTAL_OPERATIONS = 100000;
- private static void withPool() throws Exception {
- // wang.hailong 2012-4-24上午11:37:55 创建连接池
- MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
- connectionManager.setMaxTotalConnections(100);
- connectionManager.setMaxConnectionsPerHost(100);
- final HttpClient client = new HttpClient(connectionManager);// 在某个线程中。
- long begin = System.currentTimeMillis();
- List<Thread> tds = new ArrayList<Thread>();
- final AtomicInteger ind = new AtomicInteger();
- for (int i = 0; i < 100; i++) {
- Thread hj = new Thread(new Runnable() {
- boolean print = false;
- public void run() {
- for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) {
- try {
- PutMethod put = new PutMethod(TABLE_COLUMN_ELEMENT+i);
- put.setRequestBody("bar"+i);
- client.executeMethod(put);
- if(print)
- System.out.println(" put statust:"
- + put.getStatusCode());
- put.releaseConnection();
- GetMethod get = new GetMethod(TABLE_COLUMN_ELEMENT+i);
- client.executeMethod(get);
- InputStream is = get.getResponseBodyAsStream();
- boolean readResponse = false;
- if(readResponse)
- {
- byte[] response1 = new byte[4096];
- int result = 0;
- result = is.read(response1);
- while (result != -1) {
- // wang.hailong 2012-4-16下午04:48:42 从返回读出4096字节,关于缓存tableColumn对象的服务器配置
- if(print)
- {
- System.out.write(response1, 0, result);
- result = is.read(response1);
- }
- }
- if (is != null) try {
- is.close();
- } catch (Exception ignore) {
- }
- }
- get.releaseConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- });
- tds.add(hj);
- hj.start();
- }
- for (Thread t : tds)
- t.join();
- long elapsed = System.currentTimeMillis() - begin;
- System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
- //pool.destroy();
- connectionManager.deleteClosedConnections();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/220720134735.html
来源: http://www.codesnippet.cn/detail/220720134735.html