qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

性能测试分享—JMeter篇

 这周尝试了一把性能测试,之前都是测试网站的性能测试,java接口的性能测试还是头一次,学到了很多,特此分享一下。
  主要用到了两个性能测试工具,一个是jmeter,一个是LoadRunner
  使用jmeter的测试方法:
  1、下载jemeter的linux版本的,下载的文件里包含了两个jar包,一个是ApacheJMeter_core.jar,ApacheJMter_java.jar。分别把这两个jar放到eclipse的Java Build Path的libraries下面。
  2、创建编写脚本的类,使他继承AbstractJavaSamplerClient。初步结构是
private SampleResult results;
public void setupTest(JavaSamplerContext arg0) {
results = new SampleResult();
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
return params;
}
//测试执行的循环体,根据线程数和循环次数的不同可执行多次,类似于LoadRunner中的Action方法
public SampleResult runTest(JavaSamplerContext arg0) {
results.sampleStart();
results.sampleEnd();
results.setSuccessful(true);
return results;
}
//结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行,类似于LoadRunner中的end方法
public void teardownTest(JavaSamplerContext arg0) {
}
 将需要循环的业务流程放到runTest方法里。编写好的代码如下:
package app;
import http.HttpClient4;
import http.HttpParameter;
import http.HttpResp;
import http.JsonUtil;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import com.yeepay.g3.utils.common.encrypt.AES;
import com.yibao.utils.des3.RSA_Encrypt;
public class TheardTest extends AbstractJavaSamplerClient{
private SampleResult results;
public void setupTest(JavaSamplerContext arg0) {
results = new SampleResult();
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
return params;
}
public static String ramdonNum(int num){
Random rd = new Random();
StringBuilder sb = new StringBuilder("");
if (num <1){
return null;
}
else{
for(int i=1;i<=num;i++){
int sr=rd.nextInt(9);
sb.append(String.valueOf(sr));
}
return sb.toString();
}}
public static void testCreditPay() throws ParseException{
String PUBLIC_KEY=TheardTest.testConsult();
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://xxxxxxxx/xxxxxx/api/bankcard/credit/pay");
String key=TheardTest.ramdonNum(6);
String encryptkey = "0123456789"+key;
String merchantAccount = "YB01000000078";
//民生
String cardNo ="XXXXXXXXX";
String validthru="xxx7";
String cvv2 = "6xx";
String phone = "13xxxxxx5431";
String orderId = "33hhkssseef3utyt2"+key;
System.out.println(orderId);
Integer transtime = (int)(System.currentTimeMillis()/1000);
Integer currency = 156;
String  amount = "50";
String productcatalog = "1";
String productName = "123";
String productDesc = "小丸子";
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);
//                    自动退款
Map<String, Object> mapjson=JsonUtil.parse(result);
Object obj = mapjson.get("yborderid");
String yborderid=obj.toString();
System.out.println("流水号:"+yborderid);
TheardTest.refundTeseCase(yborderid,merchantAccount,amount);
method.releaseConnection();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试协商接口
* @author shangwei
* @return
* @throws ParseException
*
*/
public static String testConsult() throws ParseException{
String url = "http://xxxxxxx/xxxxxx/consult";
HttpClient4 client =HttpClient4.createDefault();
String data = "";
Date d = new Date();
Calendar now = Calendar.getInstance();
now.setTime(d);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
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, "12345678901234HH");
} catch (Exception e) {
e.printStackTrace();
}
HttpParameter parameter = new HttpParameter();
parameter.add("data", data);
parameter.add("merchantaccount", "YB01000000078");
HttpResp resp = new HttpResp();
try{
resp=client.doPost(url, parameter, "utf-8");
String respStr= resp.getText("utf-8");
System.out.println(respStr);
String aes=AES.decryptFromBase64(respStr, "12345678901234HH");
System.out.println(aes);
return aes;
}catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
client.shutdown();
return "出错啦";
}
public static void refundTeseCase(String yborderid,String merchantAccount,String amount) throws IOException{
String url ="http://xxxxxxxx/xxxxxxx/api/refund/direct";
HttpClient4 client =HttpClient4.createDefault();
HttpParameter parameter = new HttpParameter();
parameter.add("merchantaccount", merchantAccount);
parameter.add("merchantrefundid", "093032323");
parameter.add("transorderid", yborderid);
parameter.add("amount", amount);
HttpResp rep;
try {
rep = client.doPost(url, parameter,"utf-8");
System.out.println(rep.getStatusCode());
System.out.println(rep.getText("utf-8"));
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//测试执行的循环体,根据线程数和循环次数的不同可执行多次,类似于LoadRunner中的Action方法
public SampleResult runTest(JavaSamplerContext arg0) {
results.sampleStart();
try {
testCreditPay();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
results.sampleEnd();
results.setSuccessful(true);
return results;
}
//结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行,类似于LoadRunner中的end方法
public void teardownTest(JavaSamplerContext arg0) {
}
}
  3、将写好的class类和该类引入到的类都打成jar包,放到jemter文件的lib/ext文件下。将用到的其他的jar包放到lib文件夹下面。
  4、启动jemter后,创建先添加线程组,在添加sampler的java请求,如果之前步骤成功,在类名称下拉列表里会出现测试代码打好jar包的类。之后操作jemter进行测试就OK啦。
  下面在分享LoadRunner进行性能测试及环境优化。

posted on 2014-01-02 09:24 顺其自然EVO 阅读(855) 评论(0)  编辑  收藏 所属分类: jmeter and badboy


只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问  
 
<2014年1月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜