LR——java 协议(转)
之前写了一篇jemter的性能测试工具的使用,但是LoadRunner才算是真正重量级的性能测试工具,下面详细介绍一下Java vuser协议的使用和环境参数调试。
LoadRunner性能测试工具使用:
1、新建脚本,选择Java vuser协议。初步结构是
1 import lrapi.lr; 2 public class Actions 3 { 4 5 //在init方法里面编写一个虚拟用户循环只执行一次的方法,如可以把协商写在里面,就可以达到每个用户协商一次之后,就不再协商的效果 6 public int init() throws Throwable { 7 return 0; 8 }//end of init 9 10 //在aciton方法里面编写需要循环并发的业务方法,如交易的方法,在run_time settings中设置循环的次数。 11 public int action() throws Throwable { 12 return 0; 13 }//end of action 14 15 //在end方法里面编写最后要执行的方法,如释放资源,没有可以不写。 16 public int end() throws Throwable { 17 return 0; 18 }//end of end 19 }
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限制。只有普通用户才会受这个限制。
遇到的问题2:在虚拟用户少的时候并发没有再出现文件打开过多的错误,在并发量达到30,运行时间达到3分钟的时候,有报错:HTTP-500
找到问题根源:通过查看tomcat和程序的日志,看到有大量的http-500的错误,是nginx已经拒绝了请求。
解决方案:修改nginx配置文件
vi /etc/nginx/nginx.conf
events {
worker_connections 1024;
}
调整为
events {
worker_connections 65535;
}
最后附上我写的信用卡支付的脚本:
1 /* 2 * LoadRunner Java script. (Build: _build_number_) 3 * 4 * Script Description: 5 * 6 */ 7 8 import lrapi.lr; 9 import http.HttpClient4; 10 import http.HttpParameter; 11 import http.HttpResp; 12 import http.JsonUtil; 13 import http.Testcredit; 14 import org.apache.commons.httpclient.HttpClient; 15 import org.apache.commons.httpclient.HttpException; 16 import org.apache.commons.httpclient.methods.PostMethod; 17 import java.util.Random; 18 import java.util.Date; 19 import java.text.SimpleDateFormat; 20 import java.util.Calendar; 21 import com.yeepay.g3.utils.common.encrypt.AES; 22 import com.yibao.utils.des3.RSA_Encrypt; 23 public class Actions 24 { 25 public String aes; 26 Date d = new Date(); 27 Testcredit tc = new Testcredit(); 28 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 29 HttpParameter parameter = new HttpParameter(); 30 31 public int init() throws Throwable { 32 String url = "http://xxxxx/xxxxx/consult"; 33 HttpClient4 client =HttpClient4.createDefault(); 34 String data = ""; 35 Calendar now = Calendar.getInstance(); 36 now.setTime(d); 37 String dateline=format.format(now.getTime()); 38 System.out.println(dateline); 39 Date date = format.parse(dateline); 40 String dates=date.getTime()/1000+""; 41 System.out.println(dates); 42 43 try { 44 data = AES.encryptToBase64(dates, "<aes>"); 45 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 50 parameter.add("data", data); 51 parameter.add("merchantaccount", "<merchant>"); 52 HttpResp resp = new HttpResp(); 53 try{ 54 55 resp=client.doPost(url, parameter, "utf-8"); 56 String respStr= resp.getText("utf-8"); 57 System.out.println(respStr); 58 aes=AES.decryptFromBase64(respStr, "<aes>"); 59 60 System.out.println("aes="+aes); 61 } catch (Exception e) { 62 63 e.printStackTrace(); 64 } 65 client.shutdown(); 66 67 return 0; 68 }//end of init 69 70 public int action() throws Throwable { 71 72 StringBuilder sb = new StringBuilder(""); 73 Random rd = new Random(); 74 for(int i=1;i<=6;i++){ 75 int sr=rd.nextInt(9); 76 sb.append(String.valueOf(sr)); 77 } 78 String key=sb.toString(); 79 80 int rds=rd.nextInt(999999); 81 lr.start_transaction("pay"); 82 83 lr.rendezvous("pay"); 84 HttpClient client = new HttpClient(); 85 PostMethod method = new PostMethod("http://xxxxxxxx/xxxxxxx/api/bankcard/credit/pay"); 86 System.out.println(aes); 87 String PUBLIC_KEY=aes; 88 System.out.println("PUBLIC_KEY"+PUBLIC_KEY); 89 String encryptkey = "0123456789"+key; 90 String merchantAccount = "<merchant>"; 91 92 //民生 93 String cardNo = "6XXXXXXXXXXXXXX"; 94 String validthru = "XXXX"; 95 String cvv2 = "XXX"; 96 String phone = "13466745431"; 97 String orderId = rds+"334234223"+key; 98 System.out.println(orderId); 99 100 Integer transtime = (int)(System.currentTimeMillis()/1000); 101 Integer currency = 156; 102 String amount = "50"; 103 String productcatalog = "1"; 104 String productName = "123"; 105 String productDesc = "小丸子"; 106 String userIp = "123.45.45.45"; 107 String identityId = "a"; 108 Integer identityType = 6; 109 String other = "eeee"; 110 111 String data = "{\"merchantaccount\":\"" + merchantAccount 112 +"\",\"cardno\":\"" + cardNo 113 + "\",\"validthru\":\"" + validthru 114 + "\",\"cvv2\":\"" + cvv2 115 + "\",\"phone\":\"" + phone 116 + "\",\"orderid\":\"" + orderId 117 + "\",\"transtime\":" + transtime 118 + ",\"currency\":" + currency 119 + ",\"amount\":" + amount 120 + ",\"productcatalog\":\"" + productcatalog 121 + "\",\"productname\":\"" + productName 122 + "\",\"productdesc\":\"" + productDesc 123 + "\",\"userip\":\"" + userIp 124 + "\",\"identityid\":\"" + identityId 125 + "\",\"identitytype\":" + identityType 126 + ",\"other\":\"" + other + "\"}"; 127 128 data = AES.encryptToBase64(data, encryptkey); 129 130 try { 131 132 method.setParameter("merchantaccount", merchantAccount); 133 method.setParameter("data", data); 134 method.setParameter("encryptkey", RSA_Encrypt.encrypt(encryptkey, PUBLIC_KEY)); 135 client.executeMethod(method); 136 137 System.out.println(method.getStatusLine()); 138 139 String respStr = method.getResponseBodyAsString(); 140 System.out.println(respStr); 141 String result = AES.decryptFromBase64(respStr, encryptkey); 142 System.out.println(result); 143 method.releaseConnection(); 144 145 } catch (Exception e) { 146 // TODO Auto-generated catch block 147 e.printStackTrace();} 148 149 lr.end_transaction("pay", lr.AUTO); 150 151 return 0; 152 }//end of action 153 154 public int end() throws Throwable { 155 return 0; 156 }//end of end 157 }

浙公网安备 33010602011771号