模擬高併發的測試(不準確哦)
阿新 • • 發佈:2018-12-29
模擬高併發的原理就是:利用執行緒池去執行多個執行緒通過httpclient訪問controller
package com.xtzn.utils; import com.alibaba.fastjson.JSONObject; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.http.HttpStatus; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * Create by PSY * * @date: 2018/10/22 上午9:30 * @version:4.0 * @Description:模擬高併發訪問controller */ public class MyTest { final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws Exception { ThreadPoolExecutor executor = new ThreadPoolExecutor(2000, 5000, 2, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(5000)); //模擬100人併發請求 CountDownLatch latch = new CountDownLatch(1); //模擬100個使用者 for (int i = 0; i < 100; i++) { AnalogUser analogUser = new AnalogUser(latch); executor.execute(analogUser); } //計數器減一 所有執行緒釋放 併發訪問。 latch.countDown(); } static class AnalogUser implements Runnable { CountDownLatch latch; public AnalogUser(CountDownLatch latch) { this.latch = latch; } @Override public void run() { long starTime = 0; try { starTime = System.currentTimeMillis(); latch.await(); System.out.println("請求開始了"); JSONObject jsonObject = httpPost("http://192.168.100.43:8080/lua/requestIdfa", "userId=3&bundleName=xxxxxx&country=US&thirdParty=sdfsfs"); System.out.println("返回的狀態碼為:"+jsonObject.get("code")); System.out.println("返回的狀態資訊:"+jsonObject.get("desc")); System.out.println("返回的內容為:"+jsonObject.get("content")); } catch (InterruptedException e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); Long t = endTime - starTime; System.out.println(t / 1000F + "秒"); } } static JSONObject httpPost(String url, String strParam) { // post請求返回結果 CloseableHttpClient httpClient = HttpClients.createDefault(); JSONObject jsonResult = null; HttpPost httpPost = new HttpPost(url); // 設定請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(2000).setConnectTimeout(2000).build(); httpPost.setConfig(requestConfig); try { if (null != strParam) { // 解決中文亂碼問題 StringEntity entity = new StringEntity(strParam, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(entity); } CloseableHttpResponse result = httpClient.execute(httpPost); //請求傳送成功,並得到響應 if (result.getStatusLine().getStatusCode() == HttpStatus.OK.value()) { String str; try { //讀取伺服器返回過來的json字串資料 str = EntityUtils.toString(result.getEntity(), "utf-8"); //把json字串轉換成json物件 jsonResult = JSONObject.parseObject(str); } catch (Exception e) { } } } catch (IOException e) { } finally { httpPost.releaseConnection(); } return jsonResult; } }
模擬的結返回結果如下圖: