batchGetAnchorLevel(dubbo接口)
一、编写脚本前的准备工作
1、安装idea,安装本地maven库,并在idea里面配置maven
2、导入git源码(目的在于下载所依赖的基础包)-->File-new-Project from version Contorl-git
3、加入pom文件,下载jmeter依赖的包
4、找开发要本次测试接口的pom文件,下载依赖的业务包
二、开发提供接口文档后,具体的测试方法
B、在resurces下面建一个文件applicationcontext.xml文件—>输入:<dubbo:reference id="LiveVideoFacade" interface="com.yzb.service.liveroomfacade.live.facade.LiveVideoFacade" protocol="dubbo" timeout="3000" actives="0" retries="0" check="false" url="172.16.254.137:20881" />
其中: id="LiveVideoFacade”指:服务的对象,interface="com.yzb.service.liveroomfacade.live.facade.LiveVideoFacade 指:接口地址
url="172.16.254.137:20881" 指:连接的服务器地址,端口可以从第四步查询
三、详细的代码:
package com.yzb.user; import com.alibaba.fastjson.JSON; import com.yixia.anchor.level.api.AnchorLevelService; import com.yixia.anchor.level.api.request.BatchGetAnchorLevelRequest; import com.yixia.anchor.level.api.response.BatchGetAnchorLevelResp; import com.yixia.anchor.level.api.response.ResponseResultVo; 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 org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.*; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class batchGetAnchorLever extends AbstractJavaSamplerClient { //连接dubbo服务 private static final String configLocation = "classpath:aplication-context.xml"; private SampleResult results; private static ApplicationContext appContext = null; private String memberids; private AnchorLevelService anchorLevelService; //参数暴露在jmeter面板上 public Arguments getDefaultParameters() { Arguments args = new Arguments(); args.addArgument("memberids",""); return args; } //调用服务并获取接口对象 public void setupTest(JavaSamplerContext context) { appContext = new ClassPathXmlApplicationContext(configLocation); anchorLevelService = (AnchorLevelService) appContext.getBean("anchorLevelService"); } //声明对象里面的参数,并由参数获取是由memberlist组成 public SampleResult runTest(JavaSamplerContext Context) { results = new SampleResult(); BatchGetAnchorLevelRequest batchGetAnchorLevelRequest = new BatchGetAnchorLevelRequest(); memberids = Context.getParameter("memberids", memberids); List<String> memberidlist = new LinkedList<String>(); memberidlist.add("10380124"); //memberidlist.add("10380312"); //从CSV文件内读取member File csv = new File("/Users/wufq/lidong/file/xk_anchor_task_score.csv"); BufferedReader br = null; try { br = new BufferedReader(new FileReader(csv)); } catch (FileNotFoundException e) { e.printStackTrace(); } String line = ""; String everyLine = ""; List<String> memberidlist = new LinkedList<String>(); try { //List<String> allString = new ArrayList<>(); while ((line = br.readLine()) != null) //读取到的内容给line变量 { everyLine = line; String memberId = everyLine.split(",")[0]; //System.out.println(memberId); memberidlist.add(memberId); } System.out.println("csv表格中所有行数:"+memberidlist.size()); } catch (IOException e) { e.printStackTrace(); } batchGetAnchorLevelRequest.setMemberIds(memberidlist); results.sampleStart(); //计算程序开始到结束所消耗的时间 long start = System.currentTimeMillis(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("start time:" + start + "; end time:" + end + "; Run time:" + (end - start) + "(ms)"); //对象关联方法,方法传入参数,用res接受 ResponseResultVo<BatchGetAnchorLevelResp> res = anchorLevelService.batchGetAnchorLevel(batchGetAnchorLevelRequest); results.sampleEnd(); if (res.getResult().equals("10000")){ results.setSuccessful(true); System.out.println("查询成功"); System.out.println(JSON.toJSONString(res)); }else{ results.setSuccessful(false); } return results; } public void teardownTest(JavaSamplerContext arg0) { System.out.println("test end"); } public static void main(String[] args){ batchGetAnchorLever test = new batchGetAnchorLever(); Arguments arguments = test.getDefaultParameters(); JavaSamplerContext context = new JavaSamplerContext(arguments); test.setupTest(context); test.runTest(context); test.teardownTest(context); } }