当前位置: 首页> 汽车> 报价 > 宽带办理_php网站建设设计制作方案_口碑营销的优缺点_域名是什么

宽带办理_php网站建设设计制作方案_口碑营销的优缺点_域名是什么

时间:2025/7/14 8:58:59来源:https://blog.csdn.net/xjian32123/article/details/144282694 浏览次数: 0次
宽带办理_php网站建设设计制作方案_口碑营销的优缺点_域名是什么

Jmeter 自带有各种功能丰富的函数,可以帮助我们进行测试,但有时候提供的这些函数并不能满足我们的要求,这时候就需要我们自己来编写一个自定义的函数了。例如我们在测试时,有时候需要填入当前的时间,虽然我们可以使用props.get("START.YMD")来获取当前日期,但使用起来很不方便。所以今天就来写一个获取当前时间的函数,并且可以自定义时间的显示格式。

一、创建Package及类

  1. 在IDEA中创建一个Java项目(此步略),再创建一个包。包名必须是含有function​,如:package com.hetc.functions;​在function包下创建一个Java类

  1. 类创建后要继承AbstractFunction​抽象类,继承后需要实现的方法为execute()、setParameters()、getReferenceKey()、getArgumentDesc()​

二、方法详解

execute()方法

函数的执行主方法,可以实现主要的处理逻辑,该方法的返回结果会显示在函数助手的The result of the function is栏

方法内容:

    @Overridepublic String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {// 获取函数的参数,并进行格式转换String s = String.valueOf(((CompoundVariable) this.arrayValue[0]).execute());DateTimeFormatter formatter = DateTimeFormatter.ofPattern(s);return formatter.format(LocalDateTime.now());}

setParameters()方法

该方法用来接收和处理用户输入的函数参数,即获取在Jmeter函数助手中,函数参数的值中输入的内容

方法中如果没有其他处理逻辑的话,直接使用默认写法即可:

    @Overridepublic void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {// 判断参数的个数是否为 1checkParameterCount(collection, 1);// 将入参转为数组this.arrayValue = collection.toArray();}

getReferenceKey()方法

定义函数的名称,即函数助手下拉列表中显示的内容,以及调用函数时的函数名

可以直接return一个名称,我这里是通过在类的开始定义一个常量,在return这个常量

    private static final String KEY = "__TimeMaker";@Overridepublic String getReferenceKey() {return KEY;}

getArgumentDesc()方法

用来定义函数入参的描述内容,返回值是一个List<String>格式,函数有几个参数,就往List中add几个参数的描述。

    private static final List<String> desc = new ArrayList<>();static {desc.add("请输入要展示的时间格式,如“yyyy-MM-dd HH:mm”");}@Overridepublic List<String> getArgumentDesc() {return desc;}

具体的定义方法随意,无需拘泥于我上面的例子。

完整内容

package com.hetc.functions;import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;/*** @ClassName: TimeFunction* @Author: Jaxx* @Date: 2024/12/5* @Description: Jmeter自定义时间函数**/
public class TimeFunction extends AbstractFunction {private static final String KEY = "__TimeMaker";private static final List<String> desc = new ArrayList<>();private Object[] arrayValue;static {desc.add("请输入要展示的时间格式,如“yyyy-MM-dd HH:mm”");}@Overridepublic String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {String s = String.valueOf(((CompoundVariable) this.arrayValue[0]).execute());DateTimeFormatter formatter = DateTimeFormatter.ofPattern(s);return formatter.format(LocalDateTime.now());}@Overridepublic void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {checkParameterCount(collection, 1);this.arrayValue = collection.toArray();}@Overridepublic String getReferenceKey() {return KEY;}@Overridepublic List<String> getArgumentDesc() {return desc;}
}

三、打包

在代码编写完成后,还需要将其打成Jar包,方法如下:

  1. 进入idea -> File -> Project Structure​
  2. 如下图,依次点击​
  3. 在弹出窗中依次进行输入
  4. 点击确定后,回到Project Structure窗口,默认会带出所有的依赖包,为了减小打出的Jar包的体积,可以将不需要的依赖手动删除,只保留必须的即可。点击确定进行保存
  5. 点击Build -> Build Artifacts
  6. 选中上一步中的Artifacts,点击Build,在out目录中会生成打好的Jar包

四、测试验证

将打好的Jar包放入jmeter_path/lib/ext目录,重启Jmeter后进入函数助手,选中新增的函数TimeMaker,输入参数值后点击生成,成功!

关键字:宽带办理_php网站建设设计制作方案_口碑营销的优缺点_域名是什么

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: