jmeter发送java请求

本文描述jmeter如何发送java请求

1.编写java请求,将加密、签名等封装,对外只暴露需要传的业务参数,jemter发送java请求时直接传入业务参数即可

添加依赖:

        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>5.4</version>
        </dependency>

 

 具体代码如下:

import com.alibaba.fastjson.JSONObject;
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 java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

public class taxfilling_detail extends AbstractJavaSamplerClient {

    private String assetCode;
    private String bankSafeCode;
    private String privateKey;
    private String service;
    private String method;

    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("assetCode","");
        params.addArgument("bankSafeCode", "xx");
        params.addArgument("privateKey", "xx");
        params.addArgument("service", "xx");
        params.addArgument("method", xxEnum.xx.getMethod());

        return params;
    }

    @Override
    public void setupTest(JavaSamplerContext arg0) {
        assetCode = arg0.getParameter("assetCode");
        bankSafeCode = arg0.getParameter("bankSafeCode");
        privateKey = arg0.getParameter("privateKey");
        service = arg0.getParameter("service");
        method = arg0.getParameter("method");

    }

    @Override
    public void teardownTest(JavaSamplerContext arg0) {

    }

    @Override
    public SampleResult runTest(JavaSamplerContext arg0) {
        SampleResult sr = new SampleResult();
        try {
            sr.sampleStart();
            sr.setSamplerData(detail().toString());
            sr.setResponseData(send_detail().toString(), "");
            sr.setDataType(SampleResult.TEXT);
            sr.setSuccessful(true);
        } catch (Throwable e) {
            sr.setSuccessful(false);
            e.printStackTrace();
        }finally {
            sr.sampleEnd();
        }
        return sr;
    }

    // 业务请求参数组装
    public JSONObject detail(){
        JSONObject reqContent = new JSONObject();
        reqContent.put("taxPaymentCode", assetCode);
        return reqContent;

    }

    // 发送业务请求
    public JSONObject send_detail() throws NoSuchAlgorithmException, InvalidKeyException {
        JSONObject content = detail();
        Map<String,String> reqObj = GetRequestDataMap.getRequestDataMap(method,content.toString(),bankSafeCode,privateKey,service);
        content = GetRequestDataMap.service(reqObj);
        return content;

    }
}

 

2.mvn install 生成jar包

 

 

3.将jar包放/.../jmeter/lib/ext/下

 

 

4.打开jmeter,新建一个线程组,线程组下增加"java sample"后,可以看到jar包中的测试类,点击选择一个测试类,下方为请求中的具体参数

 

 

填写参数后发起请求

 

posted @ 2023-03-08 10:30  _titleInfo  阅读(136)  评论(0编辑  收藏  举报
//雪花飘落效果