Java教程

jmeter发送java请求

本文主要是介绍jmeter发送java请求,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、创建一个java工程

  • 1、新建一个java Project

 File->new->Java Project

 

 

 

 

  •  2、在Eclipse下导入$\jmeter\lib\ext\下的所有jar包;

导入额外的5个jar包,在 $\jmeter\lib\ 下

还有被测的接口的jar包也要导入

 

 

 

 

 

 

  • 创建class,将新建的类extends AbstractJavaSamplerClient 类

   继承"getDefaultParameters","setupTest","runTest"和"teardownTest"四个方法:

public Arguments getDefaultParameters();设置可用参数及的默认值;

public void setupTest(JavaSamplerContext arg0):每个线程测试前执行一次,做一些初始化工作;

public SampleResult runTest(JavaSamplerContext arg0):开始测试,从arg0参数可以获得参数值;

public void teardownTest(JavaSamplerContext arg0):测试结束时调用;

private String getResponseData(ApplyResult rs):将返回进行json格式化

 

二、继承类AbstractJavaSamplerClient(代码示例为接口getApplyProcess

  • 1、getDefaultParameters: 获取默认参数以及参数值

(这里只有一个参数 certi_code,接口getApplyProcess 只有一个入参,如果多个入参,应全部都添加上)

      

 

 

  • 2、setupTest: 测试方法运行之前的工作(初始化记录结果;从参数列表中获取对应变)

    (如果是有多个入参,也应都添加)

   

  • 3、runTest:运行测试方法(初始化记录结果,设定开始/结束记录时间,测试调用Service的主方法,测试成功与否的判断方法)

(1)配置获取:(setUrl提供的url,所在的系统地址而定;setInterface是接口的 实现类名)

 

 

 

(2)请求、返回记录:(setSamplerData为请求数据,getResponseData为返回数据)

 

 

  • 4、teardownTest 固定写法

 

 

  • 5、返回进行json的格式化

   

 

 

 

导出jar

  • 右键点击项目:Export… 直接导出为 jar包就可以

 

  • export后的jar包放在$/jmeter/lib/ext/下($/jmeter为jmeter的安装路径)

 

四、运行java请求

1、在$\jmeter\bin\下执行jmeter.bat

2、右键点击“测试计划”添加一个Thread Group(线程组)

3、右键点击线程组,添加一个Java Request费用状态查询

 

 

 

 

 

五、接口getApplyProcess示例java文件

 java文件放在了“文件”页签

 

备注:JsonFormatTool.java为json格式转换代码

 

 

package salesLoad;

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.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.utils.ReferenceConfigCache;
import com.google.gson.Gson;
import com.ulic.agent.ws.enter.FreshManEnterInterface;
import com.ulic.agent.ws.enter.model.ApplyResult;
import com.ulic.im.client.vo.AppInfo;


public class GetApplyProcess extends AbstractJavaSamplerClient{

    
    private FreshManEnterInterface enterService;
    private ReferenceConfig<FreshManEnterInterface> reference;
    private ReferenceConfigCache cache;
    
    private String certiCode;
    
    // 这个方法是用来自定义java方法入参的。  
    // params.addArgument("num1","");表示入参名字叫num1,默认值为空。  
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("certi_code", "");
        return params;
    }
    
    //每个线程测试前执行一次,做一些初始化工作;  
    public void setupTest(JavaSamplerContext arg0) {
        certiCode = arg0.getParameter("certi_code");
    }

    @Override
    public SampleResult runTest(JavaSamplerContext arg0) {
        setupTest(arg0);
        reference = new ReferenceConfig<FreshManEnterInterface>();
        reference.setApplication(new ApplicationConfig("letmetest"));
        reference.setTimeout(300000);
        reference.setUrl("dubbo://10.10.1.11:20881");
        reference.setInterface("com.mims.agent.ws.enter.FreshManEnterInterface");
        cache = ReferenceConfigCache.getCache();
        enterService = cache.get(reference);
        
        AppInfo app = new AppInfo();
        app.setAppCode("MAP");
        app.setAppIP("10.51.4.9");
        ApplyResult rs = null;
        
        SampleResult sr = new SampleResult(); 
        try {  
            sr.sampleStart();
            sr.setSamplerData("请求参数certi_code:"+certiCode);
            rs = enterService.getApplyProcess(app, certiCode);
            
            sr.setResponseData(getResponseData(rs), null);
            sr.setDataType(SampleResult.TEXT);
            sr.setResponseCode("0000");
            
            Thread.sleep(10);
            sr.setSuccessful(true);
        }catch(Exception e){
            e.printStackTrace();
            sr.setResponseCode("0001");
            sr.setSuccessful(false);
        }finally{
            sr.sampleEnd();
        }
        return sr;
    }
    
    private String getResponseData(ApplyResult rs) {
        Gson gson = new Gson();
        String s = gson.toJson(rs, ApplyResult.class);
        JsonFormatTool jft = new JsonFormatTool();        
        return jft.formatJson(s);
    }
    
    public void teardownTest(JavaSamplerContext arg0){  
        super.teardownTest(arg0);  
    }
}

 

  1 package com.ulic.loadtest.esales;
  2 
  3 import java.math.BigDecimal;
  4 
  5 import org.apache.jmeter.config.Arguments;
  6 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
  7 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
  8 import org.apache.jmeter.samplers.SampleResult;
  9 
 10 import com.alibaba.dubbo.config.ApplicationConfig;
 11 import com.alibaba.dubbo.config.ReferenceConfig;
 12 import com.alibaba.dubbo.config.utils.ReferenceConfigCache;
 13 import com.ulic.esales.ws.EsalesServiceInterface;
 14 import com.ulic.esales.ws.vo.AoPolicyProductVO;
 15 import com.ulic.im.client.vo.AppInfo;
 16 import com.ulic.loadtest.util.JsonFormatTool;
 17 
 18 public class CheckAoProductRule extends AbstractJavaSamplerClient {
 19     
 20     private EsalesServiceInterface esalesService;
 21     private ReferenceConfig<EsalesServiceInterface> reference;
 22     private ReferenceConfigCache cache;
 23     
 24     private AoPolicyProductVO vo;
 25     
 26     // 这个方法是用来自定义java方法入参的。  
 27     // params.addArgument("num1","");表示入参名字叫num1,默认值为空。  
 28     public Arguments getDefaultParameters() {
 29         Arguments params = new Arguments();
 30         params.addArgument("item_id", "0");
 31         params.addArgument("policy_id", "0");
 32         params.addArgument("product_id", "0");
 33         params.addArgument("product_num", "");
 34         params.addArgument("master_id", "0");
 35         params.addArgument("main_rider", "");
 36         params.addArgument("amount", "0");
 37         params.addArgument("period_prem", "0");
 38         params.addArgument("unit", "0");
 39         params.addArgument("relation_1", "0");
 40         params.addArgument("insurant_1", "0");
 41         params.addArgument("age_1", "0");
 42         params.addArgument("bonus_choice", "0");
 43         params.addArgument("charge_period", "");
 44         params.addArgument("charge_year", "0");
 45         params.addArgument("coverage_period", "");
 46         params.addArgument("coverage_year", "0");
 47         params.addArgument("pay_year", "0");
 48         params.addArgument("insert_id", "0");
 49         return params;
 50     }
 51 
 52     @Override
 53     public SampleResult runTest(JavaSamplerContext arg0) {
 54         vo = new AoPolicyProductVO();
 55         vo.setItemId(Long.parseLong(arg0.getParameter("item_id")));
 56         vo.setPolicyId(Long.parseLong(arg0.getParameter("policy_id")));
 57         vo.setProductId(Long.parseLong(arg0.getParameter("product_id")));
 58         vo.setProductNum(arg0.getParameter("product_num"));
 59         vo.setMasterId(Long.parseLong(arg0.getParameter("master_id")));
 60         vo.setMainRider(arg0.getParameter("main_rider"));
 61         vo.setAmount(new BigDecimal(Float.parseFloat(arg0.getParameter("amount"))));
 62         vo.setPeriodPrem(new BigDecimal(Float.parseFloat(arg0.getParameter("period_prem"))));
 63         vo.setUnit(new BigDecimal(arg0.getParameter("unit")));
 64         vo.setRelation1(Long.parseLong(arg0.getParameter("relation_1")));
 65         vo.setInsurant1(Long.parseLong(arg0.getParameter("insurant_1")));
 66         vo.setAge1(Long.parseLong(arg0.getParameter("age_1")));
 67         vo.setBonusChoice(Long.parseLong(arg0.getParameter("bonus_choice")));
 68         vo.setChargePeriod(arg0.getParameter("charge_period"));
 69         vo.setChargeYear(Long.parseLong(arg0.getParameter("charge_year")));
 70         vo.setCoveragePeriod(arg0.getParameter("coverage_period"));
 71         vo.setCoverageYear(Long.parseLong(arg0.getParameter("coverage_year")));
 72         vo.setPayYear(Long.parseLong(arg0.getParameter("pay_year")));
 73         vo.setInsertId(Long.parseLong(arg0.getParameter("insert_id")));
 74         
 75         reference = new ReferenceConfig<EsalesServiceInterface>();
 76         reference.setApplication(new ApplicationConfig("letmetest"));
 77         reference.setTimeout(300000);
 78         reference.setUrl("dubbo://10.181.3.16:21889");
 79         //reference.setRegistry(registry);
 80         reference.setInterface("com.ulic.esales.ws.EsalesServiceInterface");
 81         cache = ReferenceConfigCache.getCache();
 82         esalesService = cache.get(reference);
 83         //moaQueryService = reference.get();
 84         
 85         AppInfo app = new AppInfo();
 86         app.setAppCode("ESALES");
 87         app.setAppIP("10.51.40.4");
 88         String rs = null;
 89         
 90         SampleResult sr = new SampleResult(); 
 91         try {  
 92             sr.sampleStart();
 93             
 94             rs = esalesService.checkAoProductRule(app, vo);
 95             
 96             sr.setResponseData(getResponseData(rs), null);
 97             sr.setDataType(SampleResult.TEXT);
 98             sr.setResponseCode("0000");
 99             
100             Thread.sleep(10);
101             sr.setSuccessful(true);
102         }catch(Exception e){
103             e.printStackTrace();
104             sr.setResponseCode("0001");
105             sr.setSuccessful(false);
106         }finally{
107             sr.sampleEnd();
108         }
109         return sr;
110     }
111     
112     private String getResponseData(String rs) {
113         JsonFormatTool jft = new JsonFormatTool();
114         
115         return jft.formatJson(rs);
116     }
117     
118     //每个线程测试前执行一次,做一些初始化工作;  
119     public void setupTest(JavaSamplerContext arg0) {
120         super.setupTest(arg0);
121     }
122     
123     //每个线程测试结束后执行一次
124     public void teardownTest(JavaSamplerContext arg0){  
125         super.teardownTest(arg0);  
126     }
127 
128 }

 

这篇关于jmeter发送java请求的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!