性能测试工具 Jmeter 二次开发—实现自定义函数

Wzh for 杭州数云测试团队 · 2022年03月30日 · 最后由 baoqiang 回复于 2022年03月31日 · 2215 次阅读

前言

在使用 jmeter 工具编写性能测试脚本的过程中,我们可以使用 jmeter 的函数助手,里面有已经为我们封装好的函数可以进行调用,但有些时候这些函数并不能满足我们的需求,这时候我们就可以开发自定义函数来满足我们的需求,当然也可以使用 BeanShell 等方式编写脚本来支撑,但不能持久化的应用,所以采用扩展函数的方式更灵活

背景及解决思路

一次接口压测过程中需要请求参数为 platCode+phone+secretKey 生成 md5 的值,采用自定义函数实现,继承 AbstractFunction 抽象类,定义函数参数个数、描述、处理逻辑..........,废话不多说直接上代码

实现逻辑

函数包路径

org.apache.jmeter.functions
开发完成后,需要导出 jar 包,替换目录下 ApacheJMeter_functions.jar 文件

代码

继承抽象类 AbstractFunction

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jmeter.functions;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MakePhone extends AbstractFunction{

    private Object[] values;
    private static final String KEY = "__Make_Phone";/*给自定义函数起个名字,rg:函数助手上的函数名称*/

    private static final List<String> desc = new ArrayList<>();/*该ArrayList用来定义自义定函数参数输入描述*/

    static {
        desc.add("platCode");
        desc.add("secretKey");

    }


    /*定义函数参数最大、最小参数范围变量*/
    private static final int MIN_PARAMETER_COUNT = 2;
    private static final int MAX_PARAMETER_COUNT = 2;




    /*获取传递得交给execute方法执行*/
    @Override
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
        checkParameterCount(parameters,MIN_PARAMETER_COUNT,MAX_PARAMETER_COUNT);
        values = parameters.toArray();

    }

    /*将定义KEY显示在函数助手*/
    @Override
    public String getReferenceKey() {
        return KEY;
    }

    /*获取参数描述*/
    @Override
    public List<String> getArgumentDesc() {
        return desc;
    }


    private static int getNum(int start,int end) {
        return (int)(Math.random()*(end-start+1)+start);
    }

    @Override
    public  String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
        /* 获取函数参数*/
        JMeterVariables vars = getVariables();
        String platCode = ((CompoundVariable)values[0]).execute();
        String secretKey = ((CompoundVariable)values[1]).execute();
        /*处理逻辑*/
        String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
        int index=getNum(0,telFirst.length-1);
        String first=telFirst[index];

        String second=String.valueOf(getNum(1,888)+10000).substring(1);
        String third=String.valueOf(getNum(1,9100)+10000).substring(1);
        String phone =  first+second+third;
        /*将phone变量回写给jmeter变量,做断言使用*/
        vars.put("phone",phone);
//        System.out.println(phone);
        /*生成加密参数*/
        return DigestUtils.md5Hex(DigestUtils.md5Hex(platCode  + phone + secretKey ).toUpperCase()).toUpperCase();




    }


}

实现展示

共收到 2 条回复 时间 点赞

写的好呀,写的好

在配合和 Jmeter 使用的时候先将写好的 Java 类包打包 jar 通过 BeanShell 前置处理器来使用类包中定义的一些方法和参数

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册