分享

loadrunner脚本实例

 战争213 2015-01-19

在做香港某个项目的时候,涉及到了接口之间性能测试,也就是A业务系统对外提供webservice接口,系统B调用webservice接口和A业务系统通信,需要对接口的各个操作进行压力测试。


由于是标准的webservice接口,我首先想到的就是使用java直接调用这些接口进行测试,因为loadrunner支持java Vuser这个强大的协议。


下面选取了两个操作的脚本进行总结分析:


一、创建操作


脚本如下:


  1. //前面这些import是需要的外部依赖组件,就是java调用webservice接口需要的一些jar包,不懂网上查下或者问问开发人员就知道了。注意,所需要的jar包请下载到loadrunner所在机器本地,然后在Run-time Setting中的ClassPath中导入这些需要的jar包;当然ClassPath中也要记得导入本地JDK的bin/lib下面的dt.jar包和tools.jar包。  
  1. import lrapi.lr;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.rmi.RemoteException;  
  5. import javax.xml.rpc.ParameterMode;  
  6. import javax.xml.rpc.ServiceException;  
  7. import org.apache.axis.client.Call;  
  8. import org.apache.axis.client.Service;  
  9. import org.apache.axis.encoding.XMLType;  
  10. import org.apache.commons.io.FileUtils;  
  11. //加密发送出去的XML文件使用,因为脚本中发送出去的文件需要加密,所以这里要导入加密包  
  12. //import sun.misc.BASE64Decoder;  
  13. import sun.misc.BASE64Encoder;  
  14.   
  15. public class Actions  
  16. {  
  17.   
  18.     public int init() {  
  19.         return 0;  
  20.     }//end of init  
  21.   
  22.   
  23.     public int action() {  
  24.         //webservice的地址,A业务系统部署后对外发布的地址  
  25.         String endpoint = "http://192.168.0.116:8080/xxxu-webapp/ws/xxxxUService";  
  26.         String fileurl="d:\\test\\index.xml";//创建动作接收的主要业务数据内容是一个XML文件格式的字符串,字符串的内容需要由XML文件转换而来;  
  27.                 String oparatorid="yyyy";//另外一个参数,操作者用户名,需要具有对应接口操作的权限;  
  28.         try{  
  29.             //把本地的XML文件读入转换成字符串处理,下面一段完全是业务操作  
  30.             File xmlfile=new File(fileurl);  
  31.             String xmlstring1=FileUtils.readFileToString(xmlfile,"UTF-8");  
  32.             //替换字符串里面的指定值后进行参数化,每次提交的参数值都是不同的;  
  33.             String xmlstring2=xmlstring1.replaceAll("abcdefg","<NewParam_2>");  
  34.             //替换字符串后参数化,保证提交的合同能随机存储在不同的文件夹中;  
  35.             String xmlstring3=xmlstring2.replaceAll("departyue","<NewParam_3>");  
  36.             xmlstring3=xmlstring3.replaceAll("departady","<NewParam_4>");  
  37.             BASE64Encoder encoder = new BASE64Encoder();  
  38.             xmlstring3=encoder.encode(xmlstring3.getBytes("UTF-8"));  
  39.   
  40.                         //事务开始,java操作webservice  
  41.             lr.start_transaction("Createdoc01");  
  42.   
  43.             Service service=new Service();  
  44.             Call call=(Call)service.createCall();  
  45.             call.setTargetEndpointAddress(new  java.net.URL(endpoint));//指定webservice的地址  
  46.             call.setOperationName("createDoc");指定需要操作webservice中哪个操作  
  47.             call.addParameter("operatorID", XMLType.XSD_STRING, ParameterMode.IN);//<span style="color:#ff0000;">指定对应操作需要的参数</span>  
  48.             call.addParameter("docXml", XMLType.XSD_STRING, ParameterMode.IN);  
  49.       
  50.             call.setReturnType(XMLType.XSD_LONG);//设置操作返回值的类型,需要搞清楚webservice对应操作返回值是什么类型  
  51.             Long result=(Long)call.invoke(new Object[]{oparatorid,xmlstring3});//调用webservice  
  52.             System.out.println(result);  
  53.             if(result == 0){  
  54.                 System.out.println("ucontent 返回:"+result+"created sucessfull");  
  55.                 lr.end_transaction("Createdoc01", lr.PASS);  
  56.                 }  
  57.             else if(result == 1103){  
  58.                 System.out.println("ucontent 返回:"+result+" XML格式错误");  
  59.                 lr.end_transaction("Createdoc01", lr.PASS);}  
  60.             else if(result == 1101){  
  61.                 System.out.println("ucontent 返回:"+result+" 当前用户不存在");  
  62.                 lr.end_transaction("Createdoc01", lr.PASS);}  
  63.             else if(result == 1102){  
  64.                 System.out.println("ucontent 返回:"+result+" 当前用户权限不足");  
  65.                 lr.end_transaction("Createdoc01", lr.PASS);}  
  66.             else if(result == 1201){  
  67.                 System.out.println("ucontent 返回:"+result+" 指定文档类型不存在");  
  68.                 lr.end_transaction("Createdoc01", lr.PASS);}  
  69.             else if(result == 1202){  
  70.                 System.out.println("ucontent 返回:"+result+" 指定的索引不存在");  
  71.                 lr.end_transaction("Createdoc01", lr.PASS);}  
  72.             else if(result == 1301){  
  73.                 System.out.println("ucontent 返回:"+result+" 文档重名");  
  74.                 lr.end_transaction("Createdoc01", lr.PASS);}  
  75.             else if(result == 1302){  
  76.                 System.out.println("ucontent 返回:"+result+" 索引值转换失败");  
  77.                 lr.end_transaction("Createdoc01", lr.PASS);}  
  78.             else   
  79.                 {System.out.println("ucontent 返回:"+result+" 其它错误");  
  80.                 lr.end_transaction("Createdoc01", lr.FAIL);}  
  81.         } catch(Exception e){  
  82.             e.printStackTrace();  
  83.             System.out.println("created faild");  
  84.             }  
  85.         return 0;  
  86.     }//end of action  
  87.   
  88.   
  89.     public int end() {  
  90.         return 0;  
  91.     }//end of end  
  92. }  

二、读取操作


  1. import lrapi.lr;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.rmi.RemoteException;  
  5. import javax.xml.rpc.ParameterMode;  
  6. import javax.xml.rpc.ServiceException;  
  7. import org.apache.axis.client.Call;  
  8. import org.apache.axis.client.Service;  
  9. import org.apache.axis.encoding.XMLType;  
  10. import org.apache.commons.io.FileUtils;  
  11. import javax.xml.namespace.QName;  
  12. import org.apache.axis.encoding.ser.BeanDeserializerFactory;  
  13. import org.apache.axis.encoding.ser.BeanSerializerFactory;  
  14. //导入系统中中自定义的返回结果类  
  15. import com.xxx.dm6p.web.server.webservice.xxx.domain.Result;  
  16.   
  17.   
  18. public class Actions  
  19. {  
  20.   
  21.     public int init() {  
  22.         return 0;  
  23.     }//end of init  
  24.   
  25.   
  26.     public int action() {  
  27.         //webservice的地址   
  28.         String endpoint = "http://192.168.0.193:8080/dm6p-webapp/ws/xxxUContentService";  
  29.         //操作者的ID  
  30.         String oparatorid="yinzg";  
  31.         //文档存在ucontent中的docid  
  32.         String docid="";  
  33.         try{  
  34.                 Service service=new Service();  
  35.                 Call call=(Call)service.createCall();  
  36.             QName qn = new QName("urn:xxxUContentService","ResultDoc");  
  37.             call.registerTypeMapping(Result.class,qn,new BeanSerializerFactory(Result.class,qn),  
  38.             new BeanDeserializerFactory(Result.class,qn));  
  39.   
  40.                 call.setTargetEndpointAddress(new  java.net.URL(endpoint));  
  41.             call.setOperationName("readDoc");  
  42.             call.addParameter("operatorID", XMLType.XSD_STRING, ParameterMode.IN);  
  43.             call.addParameter("docID", XMLType.XSD_STRING, ParameterMode.IN);  
  44.             call.setReturnClass(Result.class);  
  45.             Result res=(Result)call.invoke(new Object[]{oparatorid,docid});  
  46.             call.invoke(new Object[]{oparatorid,docid});  
  47.             //返回的是一个result,包含int结果码、List<String>类型的ID列表,这个类型是系统自己定义,所以在import的时候需要导入包含该类型的包com.xxx.dm6p.web.server.webservice.xxx.domain.Result;  
  48.             int result_code=res.getCode();  
  49.             System.out.println(result_code);  
  50.                   
  51.             if(result_code == 0){  
  52.                 System.out.println("ucontent 返回:"+result_code+"created sucessfull");  
  53.                 System.out.println(res.getCode());  
  54.                 for(String docid: result.getDocIDs())  
  55.                     {System.out.println(docid);}  
  56.             }  
  57.             else if(result_code == 1101){  
  58.                 System.out.println("ucontent 返回:"+result_code+"当前用户不存在");}  
  59.             else if(result_code == 1102){  
  60.                 System.out.println("ucontent 返回:"+result_code+"当前用户没有操作权限");}  
  61.             else if(result_code == 1303){  
  62.                 System.out.println("ucontent 返回:"+result_code+"指定的文档不存在");}  
  63.             else System.out.println("ucontent 返回:"+result_code+"其它错误");   
  64.             } catch(Exception e){  
  65.             e.printStackTrace();  
  66.             System.out.println("操作失败");  
  67.         }  
  68.   
  69.         return 0;  
  70.     }//end of action  
  71.   
  72.   
  73.     public int end() {  
  74.         return 0;  
  75.     }//end of end  
  76. }  







 


使用java Vuser协议的几点要求:


1.不管什么系统要做性能测试,都必须对业务了解清楚,特别是需要开发涉及到一些接口和内部操作的脚本,更需要对系统业务,内部结构,数据流向等了解的非常清楚;比如我编写这个接口的测试脚本,我首先要清楚接口中方法需要的参数要求,参数传入后系统怎么处理,最后返回什么结果等;


2.懂一点简单的java语言,当然你不懂可以百度了!保证你写的java脚本没有性能问题;


3.使用java Vuser协议要注意run-time setting的设置,包括classpath和java jvm,classpath保证脚本能正常运行,JVM保证客户端的脚本能更好的运行;


4.loadruner的分步运行也能调试java脚本,但是在专门的编译工具上调试通过后再copy过来会更方便;


 


 



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多