在做香港某個項目的時候,涉及到了接口之間性能測試,也就是A業(yè)務(wù)系統(tǒng)對外提供webservice接口,系統(tǒng)B調(diào)用webservice接口和A業(yè)務(wù)系統(tǒng)通信,需要對接口的各個操作進行壓力測試。
由于是標(biāo)準(zhǔn)的webservice接口,我首先想到的就是使用java直接調(diào)用這些接口進行測試,因為loadrunner支持java Vuser這個強大的協(xié)議。
下面選取了兩個操作的腳本進行總結(jié)分析:
一、創(chuàng)建操作
腳本如下:
- //前面這些import是需要的外部依賴組件,就是java調(diào)用webservice接口需要的一些jar包,不懂網(wǎng)上查下或者問問開發(fā)人員就知道了。注意,所需要的jar包請下載到loadrunner所在機器本地,然后在Run-time Setting中的ClassPath中導(dǎo)入這些需要的jar包;當(dāng)然ClassPath中也要記得導(dǎo)入本地JDK的bin/lib下面的dt.jar包和tools.jar包。
- import lrapi.lr;
- import java.io.File;
- import java.io.IOException;
- import java.rmi.RemoteException;
- import javax.xml.rpc.ParameterMode;
- import javax.xml.rpc.ServiceException;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.apache.axis.encoding.XMLType;
- import org.apache.commons.io.FileUtils;
- //加密發(fā)送出去的XML文件使用,因為腳本中發(fā)送出去的文件需要加密,所以這里要導(dǎo)入加密包
- //import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
-
- public class Actions
- {
-
- public int init() {
- return 0;
- }//end of init
-
-
- public int action() {
- //webservice的地址,A業(yè)務(wù)系統(tǒng)部署后對外發(fā)布的地址
- String endpoint = "http://192.168.0.116:8080/xxxu-webapp/ws/xxxxUService";
- String fileurl="d:\\test\\index.xml";//創(chuàng)建動作接收的主要業(yè)務(wù)數(shù)據(jù)內(nèi)容是一個XML文件格式的字符串,字符串的內(nèi)容需要由XML文件轉(zhuǎn)換而來;
- String oparatorid="yyyy";//另外一個參數(shù),操作者用戶名,需要具有對應(yīng)接口操作的權(quán)限;
- try{
- //把本地的XML文件讀入轉(zhuǎn)換成字符串處理,下面一段完全是業(yè)務(wù)操作
- File xmlfile=new File(fileurl);
- String xmlstring1=FileUtils.readFileToString(xmlfile,"UTF-8");
- //替換字符串里面的指定值后進行參數(shù)化,每次提交的參數(shù)值都是不同的;
- String xmlstring2=xmlstring1.replaceAll("abcdefg","<NewParam_2>");
- //替換字符串后參數(shù)化,保證提交的合同能隨機存儲在不同的文件夾中;
- String xmlstring3=xmlstring2.replaceAll("departyue","<NewParam_3>");
- xmlstring3=xmlstring3.replaceAll("departady","<NewParam_4>");
- BASE64Encoder encoder = new BASE64Encoder();
- xmlstring3=encoder.encode(xmlstring3.getBytes("UTF-8"));
-
- //事務(wù)開始,java操作webservice
- lr.start_transaction("Createdoc01");
-
- Service service=new Service();
- Call call=(Call)service.createCall();
- call.setTargetEndpointAddress(new java.net.URL(endpoint));//指定webservice的地址
- call.setOperationName("createDoc");指定需要操作webservice中哪個操作
- call.addParameter("operatorID", XMLType.XSD_STRING, ParameterMode.IN);//<span style="color:#ff0000;">指定對應(yīng)操作需要的參數(shù)</span>
- call.addParameter("docXml", XMLType.XSD_STRING, ParameterMode.IN);
-
- call.setReturnType(XMLType.XSD_LONG);//設(shè)置操作返回值的類型,需要搞清楚webservice對應(yīng)操作返回值是什么類型
- Long result=(Long)call.invoke(new Object[]{oparatorid,xmlstring3});//調(diào)用webservice
- System.out.println(result);
- if(result == 0){
- System.out.println("ucontent 返回:"+result+"created sucessfull");
- lr.end_transaction("Createdoc01", lr.PASS);
- }
- else if(result == 1103){
- System.out.println("ucontent 返回:"+result+" XML格式錯誤");
- lr.end_transaction("Createdoc01", lr.PASS);}
- else if(result == 1101){
- System.out.println("ucontent 返回:"+result+" 當(dāng)前用戶不存在");
- lr.end_transaction("Createdoc01", lr.PASS);}
- else if(result == 1102){
- System.out.println("ucontent 返回:"+result+" 當(dāng)前用戶權(quán)限不足");
- lr.end_transaction("Createdoc01", lr.PASS);}
- else if(result == 1201){
- System.out.println("ucontent 返回:"+result+" 指定文檔類型不存在");
- lr.end_transaction("Createdoc01", lr.PASS);}
- else if(result == 1202){
- System.out.println("ucontent 返回:"+result+" 指定的索引不存在");
- lr.end_transaction("Createdoc01", lr.PASS);}
- else if(result == 1301){
- System.out.println("ucontent 返回:"+result+" 文檔重名");
- lr.end_transaction("Createdoc01", lr.PASS);}
- else if(result == 1302){
- System.out.println("ucontent 返回:"+result+" 索引值轉(zhuǎn)換失敗");
- lr.end_transaction("Createdoc01", lr.PASS);}
- else
- {System.out.println("ucontent 返回:"+result+" 其它錯誤");
- lr.end_transaction("Createdoc01", lr.FAIL);}
- } catch(Exception e){
- e.printStackTrace();
- System.out.println("created faild");
- }
- return 0;
- }//end of action
-
-
- public int end() {
- return 0;
- }//end of end
- }
二、讀取操作
- import lrapi.lr;
- import java.io.File;
- import java.io.IOException;
- import java.rmi.RemoteException;
- import javax.xml.rpc.ParameterMode;
- import javax.xml.rpc.ServiceException;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.apache.axis.encoding.XMLType;
- import org.apache.commons.io.FileUtils;
- import javax.xml.namespace.QName;
- import org.apache.axis.encoding.ser.BeanDeserializerFactory;
- import org.apache.axis.encoding.ser.BeanSerializerFactory;
- //導(dǎo)入系統(tǒng)中中自定義的返回結(jié)果類
- import com.xxx.dm6p.web.server.webservice.xxx.domain.Result;
-
-
- public class Actions
- {
-
- public int init() {
- return 0;
- }//end of init
-
-
- public int action() {
- //webservice的地址
- String endpoint = "http://192.168.0.193:8080/dm6p-webapp/ws/xxxUContentService";
- //操作者的ID
- String oparatorid="yinzg";
- //文檔存在ucontent中的docid
- String docid="";
- try{
- Service service=new Service();
- Call call=(Call)service.createCall();
- QName qn = new QName("urn:xxxUContentService","ResultDoc");
- call.registerTypeMapping(Result.class,qn,new BeanSerializerFactory(Result.class,qn),
- new BeanDeserializerFactory(Result.class,qn));
-
- call.setTargetEndpointAddress(new java.net.URL(endpoint));
- call.setOperationName("readDoc");
- call.addParameter("operatorID", XMLType.XSD_STRING, ParameterMode.IN);
- call.addParameter("docID", XMLType.XSD_STRING, ParameterMode.IN);
- call.setReturnClass(Result.class);
- Result res=(Result)call.invoke(new Object[]{oparatorid,docid});
- call.invoke(new Object[]{oparatorid,docid});
- //返回的是一個result,包含int結(jié)果碼、List<String>類型的ID列表,這個類型是系統(tǒng)自己定義,所以在import的時候需要導(dǎo)入包含該類型的包com.xxx.dm6p.web.server.webservice.xxx.domain.Result;
- int result_code=res.getCode();
- System.out.println(result_code);
-
- if(result_code == 0){
- System.out.println("ucontent 返回:"+result_code+"created sucessfull");
- System.out.println(res.getCode());
- for(String docid: result.getDocIDs())
- {System.out.println(docid);}
- }
- else if(result_code == 1101){
- System.out.println("ucontent 返回:"+result_code+"當(dāng)前用戶不存在");}
- else if(result_code == 1102){
- System.out.println("ucontent 返回:"+result_code+"當(dāng)前用戶沒有操作權(quán)限");}
- else if(result_code == 1303){
- System.out.println("ucontent 返回:"+result_code+"指定的文檔不存在");}
- else System.out.println("ucontent 返回:"+result_code+"其它錯誤");
- } catch(Exception e){
- e.printStackTrace();
- System.out.println("操作失敗");
- }
-
- return 0;
- }//end of action
-
-
- public int end() {
- return 0;
- }//end of end
- }
使用java Vuser協(xié)議的幾點要求:
1.不管什么系統(tǒng)要做性能測試,都必須對業(yè)務(wù)了解清楚,特別是需要開發(fā)涉及到一些接口和內(nèi)部操作的腳本,更需要對系統(tǒng)業(yè)務(wù),內(nèi)部結(jié)構(gòu),數(shù)據(jù)流向等了解的非常清楚;比如我編寫這個接口的測試腳本,我首先要清楚接口中方法需要的參數(shù)要求,參數(shù)傳入后系統(tǒng)怎么處理,最后返回什么結(jié)果等;
2.懂一點簡單的java語言,當(dāng)然你不懂可以百度了!保證你寫的java腳本沒有性能問題;
3.使用java Vuser協(xié)議要注意run-time setting的設(shè)置,包括classpath和java jvm,classpath保證腳本能正常運行,JVM保證客戶端的腳本能更好的運行;
4.loadruner的分步運行也能調(diào)試java腳本,但是在專門的編譯工具上調(diào)試通過后再copy過來會更方便;
|