【轉】效能測試分享---java vuser協議(2)---LoadRunner篇

來源:互聯網
上載者:User

標籤:

轉自:http://blog.csdn.net/weiweicao0429/article/details/8941137

LoadRunner效能測試工具使用:

1、建立指令碼,選擇java vuser協議。初步結構是

 

[java] view plaincopyprint?
  1. import lrapi.lr; 
  2. public class Actions 
  3.  
  4. //在init方法裡面編寫一個虛擬使用者迴圈只執行一次的方法,如可以把協商寫在裡面,就可以達到每個使用者協商一次之後,就不再協商的效果 
  5. public int init() throws Throwable { 
  6. return 0; 
  7. }//end of init 
  8.  
  9. //在aciton方法裡面編寫需要迴圈並發的業務方法,如交易的方法,在run_time settings中設定迴圈的次數。 
  10. public int action() throws Throwable { 
  11. return 0; 
  12. }//end of action 
  13.  
  14. //在end方法裡面編寫最後要執行的方法,如釋放資源,沒有可以不寫。 
  15. public int end() throws Throwable { 
  16. return 0; 
  17. }//end of end 
import lrapi.lr;public class Actions{//在init方法裡面編寫一個虛擬使用者迴圈只執行一次的方法,如可以把協商寫在裡面,就可以達到每個使用者協商一次之後,就不再協商的效果public int init() throws Throwable {return 0;}//end of init//在aciton方法裡面編寫需要迴圈並發的業務方法,如交易的方法,在run_time settings中設定迴圈的次數。public int action() throws Throwable {return 0;}//end of action//在end方法裡面編寫最後要執行的方法,如釋放資源,沒有可以不寫。public int end() throws Throwable {return 0;}//end of end}

 


2、在初始代碼的基礎上繼續編寫業務方法。需要注意的是:

1)把只需要建立一次對象的語句如:Random rd = new Random();放在init,aciton,end方法之外,可以避免在迴圈中建立很多的對象。但是需要注意的是如果是建立HTTP請求的對象是需要反覆建立的,不能單提出來,否則執行一次之後,該串連被釋放了,而沒有建立新的串連,第二次就執行不下去了。

2、指令碼編寫完之後,將該指令碼所有import的類的jar包放在一個檔案夾下,點擊Run Time Settings的classpath,把所有jar包都添加進去。

3、運行指令碼,看看能不能成功執行,按照錯誤提示調試指令碼。

4、接下來,可以把需要變化的值進行參數化。如商戶名和AESKEY。選中值,右鍵選擇replace with a parameter,在parameter list功能裡添加需要用到的商戶和AESKEY的值,將商戶參數的迴圈方式select next row選擇unqie,update value on選擇once,表示為個商戶只取唯一的值。aeskey的select next row選擇same line as merchant。

5、接下來插入事物,把需要計算業務時間的代碼前後都插入事物函數,如lr.start_transaction("pay");lr.end_transaction("pay", lr.AUTO);如果想更加詳細的知道每一步操作的時間,可以多插入一些事物,如加密,解密,擷取傳回值等步驟均插入事物函數。

6、如果需要並發執行,還需要插入集合點函數lr.rendezvous("pay");這樣在情境裡並發執行的時候,所有的虛擬使用者會等到都準備好之後在一個時間點執行。

7、接下來就是在情境裡執行了。點擊tools---create controller scenario。進入情境。並發10個使用者執行5分鐘。同時監控tomcat的日誌。

在並發的情況下,出現了一些問題,下面針對這些問題給出解決方案。

遇到的問題1、在並發執行1分鐘後,開始有報錯,主要的報錯是解密失敗,同時tomcat掛了,不能在接受新的請求。

找到問題根源:通過監控tomcat日誌,發現後期有很多的報錯日誌是檔案開啟過多。

嚴重: Socket accept failed java.net.SocketException: 開啟的檔案過多 at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408) at java.net.ServerSocket.implAccept(ServerSocket.java:462) at java.net.ServerSocket.accept(ServerSocket.java:430) at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61) at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:352) at java.lang.Thread.run(Thread.java:662) 2013-5-16 11:38:46 org.apache.tomcat.util.net.JIoEndpoint$Acceptor run 2013-05-16 11:38:44,974 INFO [DefaultRequestDirector.java:586] - I/O exception (java.net.SocketException) caught when connecting to the target host: 開啟的檔案過多 2013-05-16 11:38:44,974 ERROR [CreditPay.java:167] - 信用卡支付失敗 com.yibao.payapi.client.RequestApiException: 請求服務未正常返回[statuscode:404, text:<html><head><title>Apache Tomcat/6.0.29 -

解決方案:由於建立SOCKET會佔用一個系統控制代碼,效果類似於開啟了一個檔案。LINUX預設的最大檔案開啟個數是1024(可能不同核心版本不一樣),所以如果並發太多串連時就會報錯。需要修改檔案開啟數設定。

當前設定最大開啟檔案數可以通過如下命令查看。    ulimit -n

這個數字說明了一個普通使用者能夠在一個單獨會話中所能開啟最大的檔案數目。注意。如果是root,以下操作不能使ulimit -n的輸出增加。因為使用者root使用者不受這個ulimit限制。只有普通使用者才會受這個限制。

為了提高最大開啟檔案數到預設值1024以上, 需要在系統上修改2個地方。 在這個案例中, 我們將最大開啟檔案數增加到為65535(系統部給出的建議)。 所有的步驟需要root使用者操作。 1、/etc/pam.d/login 添加 session required     /lib/security/pam_limits.so 2. 按照最大開啟檔案數量的需求設定系統, 並且通過檢查/proc/sys/fs/file-max檔案來確認最大開啟檔案數已經被正確設定。 echo 65535 > /proc/sys/fs/file-max 3、在/etc/security/limits.conf檔案中設定最大開啟檔案數,添加2行內容:* soft nofile 4096 * hard nofile 4096 解釋:下面是一行提示     #<domain>      <type>  <item>         <value>添加如下這行。 * - nofile 2048 這行設定了每個使用者的預設開啟檔案數為2048。 注意"nofile"項有兩個可能的限制措施。 就是<type>項下的hard和soft。 要使修改過得最大開啟檔案數生效,必須對這兩種限制進行設定。 如果使用"-"字元設定<type>, 則hard和soft設定會同時被設定。硬限制表明soft限制中所能設定的最大值。 soft限制指的是當前系統生效的設定值。 hard限制值可以被普通使用者降低。但是不能增加。 soft限制不能設定的比hard限制更高。 只有root使用者才能夠增加hard限制值。 

遇到的問題2:在虛擬使用者少的時候並發沒有再出現檔案開啟過多的錯誤,在並發量達到30,已耗用時間達到3分鐘的時候,有報錯:HTTP-500

 

找到問題根源:通過查看tomcat和程式的日誌,看到有大量的http-500的錯誤,是nginx已經拒絕了請求。

解決方案:修改nginx設定檔

vi /etc/nginx/nginx.conf

events {

worker_connections 1024;

}

調整為

events {

worker_connections 65535;

}

最後附上我寫的信用卡支付的指令碼:

[java] view plaincopyprint?
  1. /*
  2. * LoadRunner Java script. (Build: _build_number_)
  3. *
  4. * Script Description:
  5. *
  6. */
  7. import lrapi.lr;
  8. import http.HttpClient4;
  9. import http.HttpParameter;
  10. import http.HttpResp;
  11. import http.JsonUtil;
  12. import http.Testcredit;
  13. import org.apache.commons.httpclient.HttpClient;
  14. import org.apache.commons.httpclient.HttpException;
  15. import org.apache.commons.httpclient.methods.PostMethod;
  16. import java.util.Random;
  17. import java.util.Date;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Calendar;
  20. import com.yeepay.g3.utils.common.encrypt.AES;
  21. import com.yibao.utils.des3.RSA_Encrypt;
  22. public class Actions
  23. {
  24. public String aes;
  25. Date d = new Date();
  26. Testcredit tc = new Testcredit();
  27. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  28. HttpParameter parameter = new HttpParameter();
  29. public int init() throws Throwable {
  30. String url = "http://xxxxx/xxxxx/consult";
  31. HttpClient4 client =HttpClient4.createDefault();
  32. String data = "";
  33. Calendar now = Calendar.getInstance();
  34. now.setTime(d);
  35. String dateline=format.format(now.getTime());
  36. System.out.println(dateline);
  37. Date date = format.parse(dateline);
  38. String dates=date.getTime()/1000+"";
  39. System.out.println(dates);
  40. try {
  41. data = AES.encryptToBase64(dates, "<aes>");
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. parameter.add("data", data);
  46. parameter.add("merchantaccount", "<merchant>");
  47. HttpResp resp = new HttpResp();
  48. try{
  49. resp=client.doPost(url, parameter, "utf-8");
  50. String respStr= resp.getText("utf-8");
  51. System.out.println(respStr);
  52. aes=AES.decryptFromBase64(respStr, "<aes>");
  53. System.out.println("aes="+aes);
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. client.shutdown();
  58. return 0;
  59. }//end of init
  60. public int action() throws Throwable {
  61. StringBuilder sb = new StringBuilder("");
  62. Random rd = new Random();
  63. for(int i=1;i<=6;i++){
  64. int sr=rd.nextInt(9);
  65. sb.append(String.valueOf(sr));
  66. }
  67. String key=sb.toString();
  68. int rds=rd.nextInt(999999);
  69. lr.start_transaction("pay");
  70. lr.rendezvous("pay");
  71. HttpClient client = new HttpClient();
  72. PostMethod method = new PostMethod("http://xxxxxxxx/xxxxxxx/api/bankcard/credit/pay");
  73. System.out.println(aes);
  74. String PUBLIC_KEY=aes;
  75. System.out.println("PUBLIC_KEY"+PUBLIC_KEY);
  76. String encryptkey = "0123456789"+key;
  77. String merchantAccount = "<merchant>";
  78. //民生
  79. String cardNo = "6XXXXXXXXXXXXXX";
  80. String validthru = "XXXX";
  81. String cvv2 = "XXX";
  82. String phone = "13466745431";
  83. String orderId = rds+"334234223"+key;
  84. System.out.println(orderId);
  85. Integer transtime = (int)(System.currentTimeMillis()/1000);
  86. Integer currency = 156;
  87. String amount = "50";
  88. String productcatalog = "1";
  89. String productName = "123";
  90. String productDesc = "小丸子";
  91. String userIp = "123.45.45.45";
  92. String identityId = "a";
  93. Integer identityType = 6;
  94. String other = "eeee";
  95. String data = "{\"merchantaccount\":\"" + merchantAccount
  96. +"\",\"cardno\":\"" + cardNo
  97. + "\",\"validthru\":\"" + validthru
  98. + "\",\"cvv2\":\"" + cvv2
  99. + "\",\"phone\":\"" + phone
  100. + "\",\"orderid\":\"" + orderId
  101. + "\",\"transtime\":" + transtime
  102. + ",\"currency\":" + currency
  103. + ",\"amount\":" + amount
  104. + ",\"productcatalog\":\"" + productcatalog
  105. + "\",\"productname\":\"" + productName
  106. + "\",\"productdesc\":\"" + productDesc
  107. + "\",\"userip\":\"" + userIp
  108. + "\",\"identityid\":\"" + identityId
  109. + "\",\"identitytype\":" + identityType
  110. + ",\"other\":\"" + other + "\"}";
  111. data = AES.encryptToBase64(data, encryptkey);
  112. try {
  113. method.setParameter("merchantaccount", merchantAccount);
  114. method.setParameter("data", data);
  115. method.setParameter("encryptkey", RSA_Encrypt.encrypt(encryptkey, PUBLIC_KEY));
  116. client.executeMethod(method);
  117. System.out.println(method.getStatusLine());
  118. String respStr = method.getResponseBodyAsString();
  119. System.out.println(respStr);
  120. String result = AES.decryptFromBase64(respStr, encryptkey);
  121. System.out.println(result);
  122. method.releaseConnection();
  123. } catch (Exception e) {
  124. // TODO Auto-generated catch block
  125. e.printStackTrace();}
  126. lr.end_transaction("pay", lr.AUTO);
  127. return 0;
  128. }//end of action
  129. public int end() throws Throwable {
  130. return 0;
  131. }//end of end
  132. }

【轉】效能測試分享---java vuser協議(2)---LoadRunner篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.