loadrunner vuser http通訊壓測
loadrunner壓測基礎腳本
導入相應jar包後,關鍵action代碼如下:
public int action() throws Throwable {
URL url = new URL("http://57.25.2.87:8084/");
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true); // 設置該連接是可以輸出的
httpURLConnection.setRequestMethod("POST"); // 設置請求方式
httpURLConnection.setRequestProperty("Content-Type",
"application/xml;charset=utf-8");
httpURLConnection.setRequestProperty("User-Agent", "Apache-HttpClient/4.5.2
(Java/1.8.0_101)");
httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate");
httpURLConnection.setRequestProperty("Connection", "Keep-alive");
PrintWriter pw = new PrintWriter(new OutputStreamWriter(
httpURLConnection.getOutputStream(),"utf-8"));
lr.start_transaction("往賬付款交易http");
pw.write("{\\"APP_HEAD\\":{},\\"SYS_HEAD\\":{\\"SCENE_ID\\":\\"01\\",\\"RUN_DATE\\":\\"20200427\\",\\"USER_ID\\":\\"907006\\",\\"TRAN_TIMESTAMP\\":\\"140000\\",\\"SYSTEM_ID\\":\\"301907\\",\\"TRAN_MODE\\":\\"ONLINE\\",\\"SOURCE_TYPE\\":\\"IP\\",\\"SEQ_NO\\":\\"\<seq\>\<random\>\<random\>\\",\\"USER_LANG\\":\\"CHINESE\\",\\"SERVICE_ID\\":\\"300420021\\",\\"MAC_VALUE\\":\\"UPPS\|uppsToesb\|RZAK\|6276A776CB468656EB28B1F2F29E782C\\",\\"TRAN_DATE\\":\\"20200427\\",\\"BRANCH_ID\\":\\"00001\\"},\\"BODY\\":{\\"TERMINAL_NO\\":\\"123456\\",\\"BUSI_CLASS_CODE\\":\\"120004\\",\\"PAYER_ACCT_TYPE\\":\\"00\\",\\"PAYER_ACCT_NO\\":\\"623166000000682244\\",\\"PAYER_NAME\\":\\"測試\\",\\"MERCHANT_NO\\":\\"\\",\\"MERCHANT_NAME\\":\\"\\",\\"MERCHANT_SHORT\\":\\"\\",\\"PAYEE_ACCT_BELONG_BRANCH\\":\\"C1081621000014\\",\\"PAYEE_ACCT_BRANCH_NM\\":\\"XX銀行\\",\\"PAYEE_ACCT_TYPE\\":\\"00\\",\\"PAYEE_SIGN_PROTOCOL\\":\\"\\",\\"PAYEE_ACCT_NO\\":\\"623166000000682244\\",\\"PAYEE_NAME\\":\\"測試\<seq\>\\",\\"CCY\\":\\"CNY\\",\\"TRAN_AMT\\":\\"0.01\\",\\"NARRATIVE\\":\\"123\\",\\"TRAN_USE\\":\\"0001\\",\\"TRX_TRM_TYPE\\":\\"02\\",\\"TRAN_DEVICE_INFO\\":\\"\\",\\"TRAN_NOTE\\":\\"123\\"}}");
pw.flush();
pw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(
httpURLConnection.getInputStream(), "utf-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) { // 讀取數據
sb.append(line + "\\n");
}
if (0\<=sb.toString().indexOf("000000") ) {
System.out.println(sb.toString());
//判斷本次交易成功(指成功發送消息,並受到返回)
lr.end_transaction("往賬付款交易http",lr.PASS);
}
else {
System.out.println(sb.toString());
lr.end_transaction("往賬付款交易http",lr.FAIL);
}
//關閉連接
httpURLConnection.disconnect();
return 0;
}//end of action
這裏是直接用了java.net.包中的http組件,使用io來處理報文流。
這個可以更好的模擬客戶端是java應用的壓力產生,可用於測試後端接口。如果是網頁版的可以參考另一個直接使用web腳本。