在做香港某个项目的时候,涉及到了接口之间性能测试,也就是A业务系统对外提供webservice接口,系统B调用webservice接口和A业务系统通信,需要对接口的各个操作进行压力测试。
由于是标准的webservice接口,我首先想到的就是使用java直接调用这些接口进行测试,因为loadrunner支持java Vuser这个强大的协议。
下面选取了两个操作的脚本进行总结分析:
一、创建操作
脚本如下:
- //前面这些import是需要的外部依赖组件,就是java调用webservice接口需要的一些jar包,不懂网上查下或者问问开发人员就知道了。注意,所需要的jar包请下载到loadrunner所在机器本地,然后在Run-time Setting中的ClassPath中导入这些需要的jar包;当然ClassPath中也要记得导入本地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;
- //加密发送出去的XML文件使用,因为脚本中发送出去的文件需要加密,所以这里要导入加密包
- //import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
-
- public class Actions
- {
-
- public int init() {
- return 0;
- }//end of init
-
-
- public int action() {
- //webservice的地址,A业务系统部署后对外发布的地址
- String endpoint = "http://192.168.0.116:8080/xxxu-webapp/ws/xxxxUService";
- String fileurl="d:\\test\\index.xml";//创建动作接收的主要业务数据内容是一个XML文件格式的字符串,字符串的内容需要由XML文件转换而来;
- String oparatorid="yyyy";//另外一个参数,操作者用户名,需要具有对应接口操作的权限;
- try{
- //把本地的XML文件读入转换成字符串处理,下面一段完全是业务操作
- File xmlfile=new File(fileurl);
- String xmlstring1=FileUtils.readFileToString(xmlfile,"UTF-8");
- //替换字符串里面的指定值后进行参数化,每次提交的参数值都是不同的;
- String xmlstring2=xmlstring1.replaceAll("abcdefg","<NewParam_2>");
- //替换字符串后参数化,保证提交的合同能随机存储在不同的文件夹中;
- String xmlstring3=xmlstring2.replaceAll("departyue","<NewParam_3>");
- xmlstring3=xmlstring3.replaceAll("departady","<NewParam_4>");
- BASE64Encoder encoder = new BASE64Encoder();
- xmlstring3=encoder.encode(xmlstring3.getBytes("UTF-8"));
-
- //事务开始,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;">指定对应操作需要的参数</span>
- call.addParameter("docXml", XMLType.XSD_STRING, ParameterMode.IN);
-
- call.setReturnType(XMLType.XSD_LONG);//设置操作返回值的类型,需要搞清楚webservice对应操作返回值是什么类型
- Long result=(Long)call.invoke(new Object[]{oparatorid,xmlstring3});//调用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+" 当前用户不存在");
- lr.end_transaction("Createdoc01", lr.PASS);}
- else if(result == 1102){
- System.out.println("ucontent 返回:"+result+" 当前用户权限不足");
- 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+" 索引值转换失败");
- 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;
- //导入系统中中自定义的返回结果类
- 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结果码、List<String>类型的ID列表,这个类型是系统自己定义,所以在import的时候需要导入包含该类型的包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+"当前用户不存在");}
- else if(result_code == 1102){
- System.out.println("ucontent 返回:"+result_code+"当前用户没有操作权限");}
- 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协议的几点要求:
1.不管什么系统要做性能测试,都必须对业务了解清楚,特别是需要开发涉及到一些接口和内部操作的脚本,更需要对系统业务,内部结构,数据流向等了解的非常清楚;比如我编写这个接口的测试脚本,我首先要清楚接口中方法需要的参数要求,参数传入后系统怎么处理,最后返回什么结果等;
2.懂一点简单的java语言,当然你不懂可以百度了!保证你写的java脚本没有性能问题;
3.使用java Vuser协议要注意run-time setting的设置,包括classpath和java jvm,classpath保证脚本能正常运行,JVM保证客户端的脚本能更好的运行;
4.loadruner的分步运行也能调试java脚本,但是在专门的编译工具上调试通过后再copy过来会更方便;
|