博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTTP高并发测试
阅读量:2237 次
发布时间:2019-05-09

本文共 10504 字,大约阅读时间需要 35 分钟。

Http测试主程序

1 package http;  2 import java.util.concurrent.ExecutorService;  3 import java.util.concurrent.Executors;  4   5 public class HttpTester {  6   7     /**  8      * 各个HTTP请求的URL,参数通过StringUtils.format填充  9      */ 10     static final String BASE_URL = "http://10.10.8.35:82"; 11     static final String LOAD_URL = BASE_URL + "/user/loadInitData?userId=#"; 12     static final String PICK_SUN_URL = BASE_URL 13             + "/sun/pick?userId=#&indexNum=#"; 14     static final String DROP_SUN_URL = BASE_URL + "/sun/drop?userId=#"; 15     static final String CROP_URL = BASE_URL 16             + "/farmland/crop?userId=#&landId=#&vegetableType=#"; 17     static final String GAIN_URL = BASE_URL 18             + "/farmland/gain?userId=#&landId=#"; 19     static final String CLEAR_URL = BASE_URL 20     + "/farmland/clearRot?userId=#&landId=#"; 21     static final String RIPENING_URL = BASE_URL 22     + "/farmland/ripening?userId=#&propsId=#"; 23  24     /** 25      * 以下几个为预设的HTTP请求参数 26      */ 27     static final String[] userIdArr = { "79", "100", "101", "10011", "10012", 28             "10013", "10014", "10015", "10016", "10017", "10018", "10019", 29             "10020", "10021", "10022", "10023", "10024", "10025", "10026", 30             "10027", "10028", "10029", "10030", "10031", "10032", "10033", 31             "10034", "10035", "10036", "10038", "10039", "10040", "10041", 32             "10042", "10043", "10044", "10045", "10046", "10047", "10048", 33             "10049", "10050", "10051", "10052", "10053", "10054", "10055", 34             "10056", "10057", "10058", "10059", "10060", "10061", "10062", 35             "10063", "10064", "10065", "10066", "10067", "10068", "10069", 36             "10070", "10071", "10072", "10073", "10074", "10075", "10076", 37             "10077", "10078", "10079", "10080", "10081", "10082", "10083", 38             "10084", "10085", "10086", "10087", "10088", "10089", "10090", 39             "10091", "10092", "10093", "10094", "10095", "10096", "10097", 40             "10098", "10099", "10100", "10101", "10102", "10103", "10104", 41             "10105", "10106", "10107", "10108" }; 42  43     static final int[] farmlandArr = { 1, 2, 3, 4, 5 ,6,7,8,9,10}; 44  45     static final int[] vegetableArr = { 1, 2, 3, 4, 5, 6 }; 46  47     /** 48      * @param args 49      * @throws Exception 50      */ 51     public static void main(String[] args) throws Exception { 52          53         // 并发数量 54         final int concurrentNum = 2000; 55         ExecutorService pool = Executors.newCachedThreadPool(); 56         for (int i = 0; i < concurrentNum; i++) { 57             pool.execute(new Runnable() { 58                 @Override 59                 public void run() { 60                     while (true) { 61                         try { 62                             randomRequest(); 63                         } catch (Exception ex) { 64                             ex.printStackTrace(); 65                         } 66                     } 67  68                 } 69             }); 70         } 71     } 72  73     /** 74      * 随机发出HTTP请求 75      * @throws Exception 76      */ 77     public static final void randomRequest() throws Exception { 78         int x = getRandom(7); 79         switch (x) { 80         case 0: 81             loadUser(); 82             break; 83         case 1: 84             pickSun(); 85             break; 86         case 2: 87             dropSun(); 88             break; 89         case 3: 90             crop(); 91             break; 92         case 4: 93             gain(); 94             break; 95         case 5: 96             clearRot(); 97             break; 98         case 6: 99             ripending();100             break;101         }102     }103 104     public static final void loadUser() throws Exception {105         System.out.println(HttpUtil.get(StringUtils.format(LOAD_URL, '#',106                 userIdArr[getRandom(userIdArr.length)])));107     }108 109     public static final void pickSun() throws Exception {110         print(HttpUtil.get(StringUtils.format(PICK_SUN_URL, '#',111                 userIdArr[getRandom(userIdArr.length)], getRandom(210))));112     }113 114     public static final void dropSun() throws Exception {115         print(HttpUtil.get(StringUtils.format(DROP_SUN_URL, '#',116                 userIdArr[getRandom(userIdArr.length)])));117     }118 119     public static final void crop() throws Exception {120         print(HttpUtil.get(StringUtils.format(CROP_URL, '#',121                 userIdArr[getRandom(userIdArr.length)],122                 farmlandArr[getRandom(farmlandArr.length)],123                 vegetableArr[getRandom(vegetableArr.length)])));124     }125 126     public static final void gain() throws Exception {127         print(HttpUtil.get(StringUtils.format(GAIN_URL, '#',128                 userIdArr[getRandom(userIdArr.length)],129                 farmlandArr[getRandom(farmlandArr.length)])));130     }131     132     public static final void clearRot() throws Exception {133         print(HttpUtil.get(StringUtils.format(CLEAR_URL, '#',134                 userIdArr[getRandom(userIdArr.length)],135                 farmlandArr[getRandom(farmlandArr.length)])));136     }137     138     public static final void ripending() throws Exception {139         print(HttpUtil.get(StringUtils.format(RIPENING_URL, '#',140                 userIdArr[getRandom(userIdArr.length)],141                 1780)));142     }143 144     public static final int getRandom(int maxNum) {145         return (int) Math.floor(Math.random() * maxNum);146     }147 148     public static final void print(String str) {149         System.out.println(str);150     }151 }

 

Http请求使用apache的httpclient

HttpUtil.java
1 package http; 2  3 import java.util.Iterator; 4 import java.util.Map; 5  6 import org.apache.commons.httpclient.HttpClient; 7 import org.apache.commons.httpclient.HttpMethod; 8 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 9 import org.apache.commons.httpclient.NameValuePair;10 import org.apache.commons.httpclient.methods.GetMethod;11 import org.apache.commons.httpclient.methods.PostMethod;12 13 /**14  * HTTP工具类15  * 
16  * Class Name : HttpUtil.java17  * Modifications:18  * @author watchdb19  * @since 2013-5-3上午09:24:2120  * 
21 */22 public class HttpUtil {23 24 protected static HttpClient m_client = null;25 26 static {27 MultiThreadedHttpConnectionManager httpConnectManager = new MultiThreadedHttpConnectionManager();28 /**29 * 每台主机默认的最大允许连接数30 */31 httpConnectManager.getParams().setDefaultMaxConnectionsPerHost(1000);32 /**33 * 最大允许连接数34 */35 httpConnectManager.getParams().setMaxTotalConnections(1000);36 37 m_client = new HttpClient(httpConnectManager);38 }39 40 /**41 * GET mehtod42 * 43 * @param url44 * URL地址45 * @return 返回响应结果46 */47 public static String get(String url) {48 String msg = null;49 HttpMethod method = new GetMethod(url);50 try {51 m_client.executeMethod(method);52 msg = method.getResponseBodyAsString();53 } catch (Exception e) {54 e.printStackTrace();55 } finally {56 method.releaseConnection();57 }58 return msg;59 }60 61 /**62 * POST method63 * 64 * @param url65 * URL地址66 * @param params67 * 待提交的参数68 * @return 返回响应结果69 */70 public static String post(String url, Map
params) {71 String msg = null;72 PostMethod post = new PostMethod(url);73 try {74 if (params != null && params.size() > 0) {75 int paramSize = params.size();76 NameValuePair[] order = new NameValuePair[paramSize];77 78 int n = 0;79 for (Iterator ite = params.entrySet().iterator(); ite.hasNext();) {80 Map.Entry
entry = (Map.Entry
) ite81 .next();82 order[n++] = new NameValuePair(entry.getKey(),83 entry.getValue());84 }85 post.setRequestBody(order);86 }87 88 m_client.executeMethod(post);89 msg = post.getResponseBodyAsString();90 } catch (Exception e) {91 e.printStackTrace();92 } finally {93 post.releaseConnection();94 }95 return msg;96 }97 }

重写了String.format,原有的效率比较低,我们这里不需要那么多功能

StringUtils.java
1 package http; 2  3 /** 4  * 字符串工具类 5  * 
 6  * Class Name : StringUtils.java 7  * Modifications: 8  * @author watchdb 9  * @since 2013-5-3上午09:29:4110  * 
11 */12 public class StringUtils {13 14 final static char PLACEHOLDER = '?';15 16 /**17 * 字符串格式化 占位符为 ?18 * @param fmt 待格式化字符串19 * @param args 参数20 * @return21 */22 public final static String format(String fmt, Object... args) {23 return format(fmt, PLACEHOLDER, args);24 }25 26 /**27 * 字符串格式化 28 * @param fmt 待格式化字符串29 * @param placeholder 占位符30 * @param args 参数31 * @return32 */33 public final static String format(String fmt,char placeholder, Object... args) {34 if (args == null) {35 return fmt;36 }37 int l = fmt.length();38 int n = 0;39 StringBuilder sb = new StringBuilder(l+10);40 char c;41 for (int i = 0; i < l; i++) {42 c = fmt.charAt(i);43 if (c == placeholder) {44 sb.append(args[n++]);45 } else {46 sb.append(c);47 }48 }49 return sb.toString();50 }51 52 /**53 * 判断字符串是否为空54 * 55 * @param str56 * @return57 */58 public final static boolean isBlank(String str) {59 if (str == null || "".equals(str)) {60 return true;61 }62 return false;63 }64 65 /**66 * 判断字符串是否不为空67 * 68 * @param str69 * @return70 */71 public final static boolean isNotBlank(String str) {72 return !isBlank(str);73 }74 }

参考资料:

1、

2、

3、

转载于:https://www.cnblogs.com/yimu/archive/2013/05/03/HCJ_0503.html

你可能感兴趣的文章
深入分析java线程池的实现原理
查看>>
mybatis中"#"和"$"的区别
查看>>
Hibernate与MyBatis区别
查看>>
如何禁用Eclipse的Validating
查看>>
据说看完这21个故事的人,30岁前都成了亿万富翁。你是下一个吗?
查看>>
SpringMVC学习笔记2
查看>>
Oracle知识点连载(一)
查看>>
Oracle知识点连载(二)
查看>>
Oracle知识点连载(三)
查看>>
Oracle知识点连载(五)
查看>>
关于三元运算符的类型转换问题
查看>>
笔记本怎么设置WIfi热点
查看>>
如何实现字符串的反转及替换?
查看>>
Java面试题全集(上)
查看>>
Java面试题全集(中)
查看>>
值传递和引用传递
查看>>
什么情况下用+运算符进行字符串连接比调用StringBuilder对象的append方法连接字符串性能更好?
查看>>
怎么根据Comparable方法中的compareTo方法的返回值的正负 判断升序 还是 降序?
查看>>
理解事务的4种隔离级别
查看>>
常用正则匹配符号
查看>>