性能测试(6)JMETER取样器二次开发

由于很多新型的请求协议,Jmeter并不能够都支持,所以Jmeter中的很多Sampler插件需要我们自己进行二次开发。

第一步,把需要的jar包,先添加到maven依赖中


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>scfstresstest</groupId>
    <artifactId>scfstresstest</artifactId>
    <version>1.0.2-SNAPSHOT</version>
    
    <dependencies>
    	<!--JMETER依赖-->
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_core</artifactId>
            <version>5.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_java -->
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>5.2.1</version>
        </dependency>

        <!--其他依赖-->
        <dependency>
            <groupId>xxxxxxxxx</groupId>
            <artifactId>xxxxxxxxxxxx</artifactId>
            <version>xxxxxx</version>
        </dependency>
    </dependencies>
</project>

第二步,编写取样器

package jmetertest.interfaces;

import com.alibaba.fastjson.JSON;
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.slf4j.Logger;

public class queryQuotaDetail extends AbstractJavaSamplerClient {
    //日志
    private Logger log = super.getNewLogger(); 
    //调用服务
    public static QuotaService quotaService =  ProxyFactory.create(QuotaService.class, "tcp://quota/QuotaServiceImpl");
    private String wbId;
    private String productCode;

	//初始化(开始时执行一次)
    @Override
    public void setupTest(JavaSamplerContext javaSamplerContext) {
        //某种连接只连接一次,例如数据库
        System.out.print("测试开始");
    }

	//添加参数
    @Override
    public Arguments getDefaultParameters() {
        //添加参数
        Arguments arguments = new Arguments();
        arguments.addArgument("wbId","");   //显示在Jmter页面上的interfaceName和methodName
        arguments.addArgument("productCode","");
        return arguments;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult(); //结果树

        //接口入参
        StringBuffer stringBuffer = new StringBuffer();
        this.wbId = javaSamplerContext.getParameter("wbId");  //获取Jmeter页面上参数
        this.productCode = javaSamplerContext.getParameter("productCode");
        long startTime = System.currentTimeMillis();

        //接口调用
        try {
            sampleResult.sampleStart();//事物开始 用于统计接口响应时间 聚合报告中使用
            //调用接口
            ApiResponse<QuotaDetailDTO> quotaDetailDTOApiResponse = quotaService.queryQuotaDetail(this.wbId, this.productCode);
            sampleResult.sampleEnd();
            String response = JSON.toJSONString(quotaDetailDTOApiResponse);
            long endTime = System.currentTimeMillis();
            stringBuffer.append("入参为: ");
            stringBuffer.append(this.wbId);
            stringBuffer.append("\t"+this.productCode);
            stringBuffer.append("\n");
            stringBuffer.append("出参结果为: ");
            stringBuffer.append(response);
            stringBuffer.append("\n");
            stringBuffer.append("开始时间: ").append(startTime);
            stringBuffer.append("\n");
            stringBuffer.append("结束时间: ").append(endTime);
            log.info(stringBuffer.toString());
            sampleResult.setSuccessful(true);
        } catch (Exception e) {
            stringBuffer.append("接口调用异常");
            log.info(stringBuffer.toString());
            sampleResult.setSuccessful(false);
            e.printStackTrace();
        }
        sampleResult.setResponseData(stringBuffer.toString(), "UTF-8");
        sampleResult.setDataType(SampleResult.TEXT);


        return sampleResult;
    }
}

第三步,构建项目
构建项目
构建成功后,将jar包放到lib/ext文件夹内。

发布了7 篇原创文章 · 获赞 1 · 访问量 167

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览