Transferred from: http://blog.csdn.net/weiweicao0429/article/details/8941137
LoadRunner Performance test Tools use:
1. Create a new script and select the Java vuser protocol. The initial structure is
[Java]View Plaincopyprint?
- Import LRAPI.LR;
- Public class Actions
- {
- In the Init method to write a virtual user loop only executes once method, such as can be written in the negotiation, you can reach each user consultation once, no longer negotiate the effect of
- public int init () throws Throwable {
- return 0;
- }//end of init
- In the Aciton method, write a business method that needs to be looped concurrently, such as the method of trading, and set the number of loops in the run_time settings.
- Public int Action () throws Throwable {
- return 0;
- }//end of Action
- In the end method, write the last method to execute, such as releasing the resources, no can not write.
- public int End () throws Throwable {
- return 0;
- }//end of End
- }
Import Lrapi.lr;public class actions{//in the Init method to write a virtual user loop to execute only once method, such as can be written in the negotiation, you can reach each user negotiation once, no longer negotiate the effect of public int Init () throws Throwable {return 0;} End of init//in the Aciton method, write a business method that requires a loop concurrency, such as a method of trading, to set the number of loops in Run_time settings. public int action () throws Throwable {return 0;} End of action//in the end method to write the last method to execute, such as releasing resources, no can not write. public int End () throws Throwable {return 0;} End of End}
2, on the basis of the initial code to continue to write business methods. It is important to note that:
1) Put only the statement that you want to create one object at a time, such as: random rd = new random (), and in addition to the Init,aciton,end method, you can avoid creating many objects in the loop. However, it is important to note that if the object that creates the HTTP request is created repeatedly and cannot be raised alone, the connection is freed without creating a new connection and the second execution is not performed.
2. After the script is written, place the jar package of all the import classes of the script in a folder and click Run Time settings classpath to add all the jar packages.
3. Run the script to see if it can be executed successfully and debug the script according to the error.
4. Next, you can parameterize the values that need to change. such as merchant name and Aeskey. Select the value, right-select Replace with a parameter, add the value of the merchant and Aeskey that need to be used in the parameter list function, select Next row of the merchant parameter to choose Unqie,update value On select once, which indicates that only a unique value is taken for a merchant. Aeskey's Select Next row selects same line as merchant.
5, next insert things, the need to calculate business time code before and after inserting things functions, such as lr.start_transaction ("pay"); Lr.end_transaction ("Pay", LR. AUTO); If you want to know more about the time of each operation, you can insert something, such as encryption, decryption, get return value, and so on to insert the thing function.
6, if you need concurrent execution, you also need to insert the collection point function lr.rendezvous ("pay"), so that when executing concurrently in the scene, all virtual users will wait until they are ready to execute at a point in time.
7. The next step is to execute in the scene. Click Tools---Create Controller scenario. Into the scene. Concurrent 10 users execute 5 minutes. Monitor tomcat logs at the same time.
In the case of concurrency, there are some problems, and the following solutions are given for these problems.
Problems encountered 1, in the concurrent execution 1 minutes later, began to have an error, the main error is decryption failure, while Tomcat hung, can not accept the new request.
Find the source of the problem: by monitoring the Tomcat logs, it is found that many of the post-error logs are too open.
Critical: Socket accept failed Java.net.SocketException: Too many open files 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: Open Too many files 2013-05-16 11:38:44,974 ERROR [creditpay.java:167]-Credit card payment failed Com.yibao.payapi.client.RequestApiException: Request service does not return normally [statuscode:404, Text:
Solution: Because a socket is created that consumes a system handle, the effect is similar to opening a file. The default maximum number of open files for Linux is 1024 (different kernel versions may not be the same), so if you have too many connections, you will get an error. You need to modify the file open number setting.
The maximum number of open files currently set can be viewed by the following command. Ulimit-n
This number shows the maximum number of files that a normal user can open in a single session. Attention. In the case of root, the following actions do not increase the output of the ulimit-n. Because the user root user is not subject to this ulimit limitation. Only ordinary users are subject to this limitation.
To increase the maximum number of open files to the default value above 1024, you need to modify 2 places on the system. In this case, we increased the maximum number of open files to 65535 (recommended by the System department). All the steps require root user action. 1,/etc/pam.d/login add session required/lib/security/pam_limits.so 2. Set the system to the maximum number of open files, and verify that the maximum number of open files has been set correctly by checking the/proc/sys/fs/file-max file. echo 65535 >/proc/sys/fs/file-max 3, set the maximum number of open files in the/etc/security/limits.conf file, add 2 lines of content: * Soft nofile 4096 * Hard Nofile 40 96 Explanation: Here is a line of hints #<domain> <type> <item> <value> Add the following lines. *-Nofile 2048 This line sets the default number of open files per user to 2048. Note There are two possible restrictions on the "Nofile" item. Is the hard and soft under <type>. These two restrictions must be set for the maximum number of open files that are modified to take effect. If the "-" character is used to set the <TYPE>, the hard and soft settings are set simultaneously. The hard limit indicates the maximum value that can be set in the soft limit. The soft limit refers to the setting value that the current system is in effect. Hard limit values can be reduced by ordinary users. But cannot be increased. The soft limit cannot be set higher than the hard limit. Only the root user can increase the hard limit value.
Problem 2: When the number of virtual users and no more files open too many errors, when the concurrency reached 30, running time of 3 minutes, there is an error: HTTP-500
Find the source of the problem: by looking at the logs of Tomcat and the program, and seeing a large number of http-500 errors, Nginx has rejected the request.
Solution: Modify Nginx configuration file
Vi/etc/nginx/nginx.conf
Events {
Worker_connections 1024;
}
Adjusted to
Events {
Worker_connections 65535;
}
Finally attach the script I wrote to pay for the credit card:
[Java]View Plaincopyprint?
- /*
- * LoadRunner Java script. (Build: _build_number_)
- *
- * Script Description:
- *
- */
- Import LRAPI.LR;
- Import http. HTTPCLIENT4;
- Import http. Httpparameter;
- Import http. HTTPRESP;
- Import http. Jsonutil;
- Import http. Testcredit;
- Import org.apache.commons.httpclient.HttpClient;
- Import org.apache.commons.httpclient.HttpException;
- Import Org.apache.commons.httpclient.methods.PostMethod;
- Import Java.util.Random;
- Import Java.util.Date;
- Import Java.text.SimpleDateFormat;
- Import Java.util.Calendar;
- Import Com.yeepay.g3.utils.common.encrypt.AES;
- Import Com.yibao.utils.des3.RSA_Encrypt;
- Public class Actions
- {
- Public String AES;
- Date d = new Date ();
- Testcredit TC = new Testcredit ();
- SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- Httpparameter parameter = new Httpparameter ();
- public int init () throws Throwable {
- String url = "Http://xxxxx/xxxxx/consult";
- HTTPCLIENT4 client =httpclient4.createdefault ();
- String data = "";
- Calendar now = Calendar.getinstance ();
- Now.settime (d);
- String Dateline=format.format (Now.gettime ());
- System.out.println (dateline);
- Date date = Format.parse (dateline);
- String dates=date.gettime ()/1000+"";
- SYSTEM.OUT.PRINTLN (dates);
- try {
- data = aes.encrypttobase64 (dates, "<aes>");
- } catch (Exception e) {
- E.printstacktrace ();
- }
- Parameter.add ("data", data);
- Parameter.add ("MerchantAccount", "<merchant>");
- Httpresp resp = new Httpresp ();
- try{
- Resp=client.dopost (URL, parameter, "Utf-8");
- String respstr= Resp.gettext ("Utf-8");
- System.out.println (RESPSTR);
- Aes=aes.decryptfrombase64 (Respstr, "<aes>");
- System.out.println ("aes=" +aes);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- Client.shutdown ();
- return 0;
- }//end of init
- Public int Action () throws Throwable {
- StringBuilder sb = new StringBuilder ("");
- Random rd = new random ();
- for (int i=1;i<=6;i++) {
- int Sr=rd.nextint (9);
- Sb.append (String.valueof (SR));
- }
- String key=sb.tostring ();
- int Rds=rd.nextint (999999);
- Lr.start_transaction ("pay");
- Lr.rendezvous ("pay");
- HttpClient client = new HttpClient ();
- Postmethod method = New Postmethod ("Http://xxxxxxxx/xxxxxxx/api/bankcard/credit/pay");
- SYSTEM.OUT.PRINTLN (AES);
- String Public_key=aes;
- System.out.println ("Public_key" +public_key);
- String Encryptkey = "0123456789" +key;
- String MerchantAccount = "<merchant>";
- Livelihood
- String Cardno = "6XXXXXXXXXXXXXX";
- String validthru = "XXXX";
- String cvv2 = "XXX";
- String phone = "13466745431";
- String orderId = rds+"334234223" +key;
- System.out.println (ORDERID);
- Integer transtime = (int) (System.currenttimemillis ()/ +);
- Integer currency = 156;
- String amount = "";
- String ProductCatalog = "1";
- String productName = "123";
- String Productdesc = "small meatballs";
- String Userip = "123.45.45.45";
- String Identityid = "a";
- Integer identitytype = 6;
- String other = "eeee";
- String data = "{\" merchantaccount\ ": \" "+ MerchantAccount
- +"\", \ "cardno\": \ "" + Cardno
- + "\", \ "validthru\": \ "" + Validthru
- + "\", \ "cvv2\": \ "" + CVV2
- + "\", \ "phone\": \ "" + Phone
- + "\", \ "orderid\": \ "" + OrderID
- + "\", \ "transtime\": "+ transtime
- + ", \" currency\ ":" + currency
- + ", \" amount\ ":" + Amount
- + ", \" productcatalog\ ": \" "+ ProductCatalog
- + "\", \ "productname\": \ "" + ProductName
- + "\", \ "productdesc\": \ "" + Productdesc
- + "\", \ "userip\": \ "" + Userip
- + "\", \ "identityid\": \ "" + Identityid
- + "\", \ "identitytype\": "+ identitytype
- + ", \" Other\ ": \" "+ other + " \ "}";
- data = aes.encrypttobase64 (data, Encryptkey);
- try {
- Method.setparameter ("MerchantAccount", merchantaccount);
- Method.setparameter ("data", data);
- Method.setparameter ("Encryptkey", Rsa_encrypt.encrypt (Encryptkey, Public_key));
- Client.executemethod (method);
- System.out.println (Method.getstatusline ());
- String respstr = method.getresponsebodyasstring ();
- System.out.println (RESPSTR);
- String result = Aes.decryptfrombase64 (Respstr, Encryptkey);
- SYSTEM.OUT.PRINTLN (result);
- Method.releaseconnection ();
- } catch (Exception e) {
- TODO auto-generated Catch block
- E.printstacktrace ();}
- Lr.end_transaction ("pay", LR.) AUTO);
- return 0;
- }//end of Action
- public int End () throws Throwable {
- return 0;
- }//end of End
- }
"Go" performance test sharing---java VUser Protocol (2)---LoadRunner