java http 并发测试_HTTP高并发测试

1 packagehttp;2 importjava.util.concurrent.ExecutorService;3 importjava.util.concurrent.Executors;4

5 public classHttpTester {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_URL13 + "/sun/pick?userId=#&indexNum=#";14 static final String DROP_SUN_URL = BASE_URL + "/sun/drop?userId=#";15 static final String CROP_URL =BASE_URL16 + "/farmland/crop?userId=#&landId=#&vegetableType=#";17 static final String GAIN_URL =BASE_URL18 + "/farmland/gain?userId=#&landId=#";19 static final String CLEAR_URL =BASE_URL20 + "/farmland/clearRot?userId=#&landId=#";21 static final String RIPENING_URL =BASE_URL22 + "/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 *@paramargs49 *@throwsException50 */

51 public static void main(String[] args) throwsException {52

53 //并发数量

54 final int concurrentNum = 2000;55 ExecutorService pool =Executors.newCachedThreadPool();56 for (int i = 0; i < concurrentNum; i++) {57 pool.execute(newRunnable() {58 @Override59 public voidrun() {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 *@throwsException76 */

77 public static final void randomRequest() throwsException {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() throwsException {105 System.out.println(HttpUtil.get(StringUtils.format(LOAD_URL, '#',106 userIdArr[getRandom(userIdArr.length)])));107 }108

109 public static final void pickSun() throwsException {110 print(HttpUtil.get(StringUtils.format(PICK_SUN_URL, '#',111 userIdArr[getRandom(userIdArr.length)], getRandom(210))));112 }113

114 public static final void dropSun() throwsException {115 print(HttpUtil.get(StringUtils.format(DROP_SUN_URL, '#',116 userIdArr[getRandom(userIdArr.length)])));117 }118

119 public static final void crop() throwsException {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() throwsException {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() throwsException {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() throwsException {139 print(HttpUtil.get(StringUtils.format(RIPENING_URL, '#',140 userIdArr[getRandom(userIdArr.length)],141 1780)));142 }143

144 public static final int getRandom(intmaxNum) {145 return (int) Math.floor(Math.random() *maxNum);146 }147

148 public static final voidprint(String str) {149 System.out.println(str);150 }151 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值