How to achieve Jmeter interface request parameters and the number of dynamic terms

  • Needs analysis
    to do the interface automation and performance testing, often encounter some request parameters is a request on the results, the number of dynamic parameters, parameter names may be the same or may increment form of a sequence, the number of parameters may be 10, 20, thus leading to poor hand simulation results of the request, the encapsulated custom function to achieve this effect

Jmeter is an open source performance testing tools, is currently one of the industry's highest rates of performance testing tool, Jmeter using JAVA language development, this article describes how to use JAVA language development extension functions they need


  • Currently two popular industry development model, this article describes the second
    1, Jmeter secondary open-source environment, directly below org.apache.jmeter.functions package development function they need, and then export the jar package, replacing Jmeter directory ApacheJMeter_functions.jar document
    2, in the direct development project Maven custom function, and then packaged, the method is simple and efficient

  • Development of ideas
    1, using the Eclipse Maven project Pom configuration file, the introduction of the extended function required dependencies;
    2, create a class, then inherit AbstractFunction, to achieve four main methods;
    3, to write a good project to compile, package
    4 , the packaged functions into jmeter extend the directory, call a custom function to check whether correct;
JDK environment Jmeter version dependencies Maven repository version
1.8 4.0 3.5.3

First, the new Maven project

1.File "New" Project, select Maven "Maven Project, has been click Next, here Package package name must end with .functions format, otherwise developed function, Jmeter read less than
How to achieve Jmeter interface request parameters and the number of dynamic terms
How to achieve Jmeter interface request parameters and the number of dynamic terms
2 configuration file Pom, Pom file Jmeter4.0 version of the configuration, as long as the Pom save file will be automatically downloaded and associated with a respective jar package, Pom configuration file as follows:
How to achieve Jmeter interface request parameters and the number of dynamic terms
3. after Pom save file, the system will automatically download the associated dependencies, as shown below:
How to achieve Jmeter interface request parameters and the number of dynamic terms
How to achieve Jmeter interface request parameters and the number of dynamic terms
4 in this case, because of the pom file has been modified, the project will be a top ×, and the compiler error might need to perform Maven "update project to update external dependencies Jar package

  • Updated ago:
    How to achieve Jmeter interface request parameters and the number of dynamic terms
    How to achieve Jmeter interface request parameters and the number of dynamic terms
  • After the update, compile, package properly:
    How to achieve Jmeter interface request parameters and the number of dynamic terms
    Second, write custom code that function
  • To achieve the expansion Jmeter function, there are two main
    1.1. Package class that implements the function declaration must contain ".functions"

    JMeter designed to allow some of the core classes can be loaded at run time in a non-UI comes in the way, these classes will be given priority loaded. When these classes are loaded by naming achieved. All implementations function of the class must contain ".functions", so we custom class implementation must contain ".functions", such as stated package called: "jmeter.test.functions"

How to achieve Jmeter interface request parameters and the number of dynamic terms
1.2. First, create a class in Jmeter.test.functions package, class name is Save_Body_Data, you need to inherit AbstractFunction class, and then implement the four methods of the parent class
How to achieve Jmeter interface request parameters and the number of dynamic terms

  • Here is the definition of variable desc, KEY will then be described to show the function of the parameters described below FIG.
    How to achieve Jmeter interface request parameters and the number of dynamic terms
    How to achieve Jmeter interface request parameters and the number of dynamic terms
  • setParameters function role is to fetch the input function of the input interface box, the input value passed to the execute
    How to achieve Jmeter interface request parameters and the number of dynamic terms
  • getReferenceKey函数是提供一个在Jmeter函数助手显示的函数名称
    How to achieve Jmeter interface request parameters and the number of dynamic terms

    • getArgumentDesc函数用来获取对输入参数的描述
      How to achieve Jmeter interface request parameters and the number of dynamic terms
    • execute函数用来实现获取输入的参数,然后通过对参数做需要的处理,该函数是开发中最重要的模块,并且进行最后执行,本次案例中主要实现的功能就是获取上一个接口返回的变量个数,然后对获取的变量名和参数值进行封装,最后通过字符连接成我们需要的效果
      三、编译、打包
      1. 编写代码完成,就可以进行编译、打包操作了,右键点击项目》Run As》Maven build
        How to achieve Jmeter interface request parameters and the number of dynamic terms
      2. 在Goals中输入package,然后点击Apply》Run执行编译,打包操作
        How to achieve Jmeter interface request parameters and the number of dynamic terms
      3. 编译打包完成,在target目录会生成一个jar包,如下图所示
        How to achieve Jmeter interface request parameters and the number of dynamic terms
        How to achieve Jmeter interface request parameters and the number of dynamic terms

    四、验证开发的函数是否正常

    1. 首先把Jmeter.test.functions函数放到jmeter的\lib\ext目录
      How to achieve Jmeter interface request parameters and the number of dynamic terms
    2. 然后启动jmeter,进入选项》函数助手对话框,选择_Media_Save_Body_Data,输入参数值,点击生成按钮(点了生成,系统就会生成一个函数,然后就相当于做了复制功能,可以拷贝到需要调用的地方)本次 只输入第二个参数值(该值必须为上一个接口定义的变量名称,稍后介绍如何使用该函数)
      How to achieve Jmeter interface request parameters and the number of dynamic terms
    3. 首先需要在上一个接口添加JSON Extractor元件,用来获取变化的参数值,
      How to achieve Jmeter interface request parameters and the number of dynamic terms
    4. 然后使用BeanShell Sampler调用生成的函数,该sample执行成功之后,就会生成我们要的字段效果
      How to achieve Jmeter interface request parameters and the number of dynamic terms
    5. 手工请求参数如下
      How to achieve Jmeter interface request parameters and the number of dynamic terms
    6. 通过代码实现的参数只需要用参数名称=值然后每个参数使用&连接即可
      How to achieve Jmeter interface request parameters and the number of dynamic terms
    7. 执行结果,如下:
      How to achieve Jmeter interface request parameters and the number of dynamic terms
      How to achieve Jmeter interface request parameters and the number of dynamic terms
      • 函数功能介绍
        1、本次开发的函数实现的效果为通过传入5个参数,其中只有第二个为必填项,其它参数可以为空
        2、第一个参数可以为空,当为空的时候,则需要把parameters中的参数和值在body data中手动写入,本次就是在body data中手工写入了不会变化的参数名,你也可以直接把这些参数传入到第一个参数,格式为funid=imp_provider&destfunid=pur_apply_provider&parentId=${apply_id}&pagetype=import&eventcode=import&user_id=${user}&dataType=json
        3、第二个参数为获取的变量名称,该名称一定要和jmeter中json path中定义的参数名一致,这样才能获取到jmeter中变化的参数
        4、第三个参数为判断重复变化的参数名称的格式是一样还是序列递增,比如id[0],id[1],默认不填相同,如果是序列递增,只需要填否
        How to achieve Jmeter interface request parameters and the number of dynamic terms
        5、第四个参数则是用来保存整个函数封装的字符串,默认不填则会把所有字符串封装到第二个参数中,填写了则封装在该参数中,需要在body data中调用该变量,调用方式为${变量名}
        6、第五个参数则为各个参数的连接方式,默认不填则为&

Jmeter.test.functions.jar下载方法见文章末尾介绍
完整源码如下:
How to achieve Jmeter interface request parameters and the number of dynamic terms


   如果文章对你有帮助,可以在右下角点击关注功能,欢迎转发、拍砖,本人会持续推出与测试有关的文章,与大家分享测试技术,每一篇原创文章都是用心编写,杜绝抄袭复制,微信公众号与51cto文章同步,方便大家查阅

Micro-channel two-dimensional code number of public attention:
How to achieve Jmeter interface request parameters and the number of dynamic terms
after attention, resources download reply, I can get a variety of shared resources Download


How to achieve Jmeter interface request parameters and the number of dynamic terms

Guess you like

Origin blog.51cto.com/6183574/2430423