我如何将 jar 文件用于循环计数器 - Jmeter [英] How can i use jar file into loop counter - Jmeter

查看:12
本文介绍了我如何将 jar 文件用于循环计数器 - Jmeter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个场景,我想将 jar 文件中的数据用于 Jmeter 循环逻辑.

我的罐子看起来像:

public String Australia(){字符串 a = "{"+ ""位置":{"+ ""纬度":-33.8669710,"+ ""lng": 151.1958750"+ "},"+ ""准确度": 50,"+ ""name": "Google 鞋!","+ ""电话号码": "(02) 9374 4000","+ ""address": "48 Pirrama Road, Pyrmont, NSW 2009, Australia","+ ""类型": ["shoe_store"],"+ ""网站":"http://www.google.com.au/","+ ""语言": "en-AU""+"}";返回一个;}公共字符串加拿大(){字符串 c = "{"+ ""位置":{"+ ""纬度":-33.8669710,"+ ""lng": 151.1958750"+ "},"+ ""准确度": 50,"+ ""name": "Google 鞋!","+ ""电话号码": "(02) 9374 4000","+ ""address": "48 Pirrama Road, Pyrmont, NSW 2009, Canada","+ ""类型": ["shoe_store"],"+ ""网站": "http://www.google.com.ca/","+ ""语言": "en-CA""+"}";返回 c;}

1) 使用上述数据,我想按照下图所述馈送"Jmeter 调用

2) 每次我在 jar 文件中添加新国家时,循环相应地增加.

有些人认为如何做到这一点,我应该使用什么作为变量以及如何告诉循环增加?

解决方案

  1. com.example 更改为您自己的包名,将 TestData 更改为您的类名

  2. 将新版本的 .jar 放到 JMeter 类路径后,您将需要重新启动 JMeter 以获取更改

就是这样,JSR223 PreProcessor 的循环控制器的每次迭代都会执行您的类中的下一个函数并使用返回的数据更新请求主体:

参考文献:

I want to create scenario where i want use data from jar file into Jmeter Loop logic.

My jar looks like:

public String Australia()
{
    String a = "{"
            + ""location": {" 
            + ""lat": -33.8669710,"
            + ""lng": 151.1958750"
            + "},"
            + ""accuracy": 50,"
            + ""name": "Google Shoes!","
            + ""phone_number": "(02) 9374 4000","
            + ""address": "48 Pirrama Road, Pyrmont, NSW 2009, Australia","
            + ""types": ["shoe_store"],"
            + ""website": "http://www.google.com.au/","
            + ""language": "en-AU""
            +
            "}";

    return a;
}

public String canada()
{
    String c = "{"
            + ""location": {" 
            + ""lat": -33.8669710,"
            + ""lng": 151.1958750"
            + "},"
            + ""accuracy": 50,"
            + ""name": "Google Shoes!","
            + ""phone_number": "(02) 9374 4000","
            + ""address": "48 Pirrama Road, Pyrmont, NSW 2009, Canada","
            + ""types": ["shoe_store"],"
            + ""website": "http://www.google.com.ca/","
            + ""language": "en-CA""
            +
            "}";

    return c;
}

1) with above data i want to 'feed' Jmeter call as described on the bellow picture

2) every time i add new country at the jar file, loop be increased accordingly.

Some thought how this could be done, what should i use as variable and how i can tell the loop to increase?

解决方案

  1. Add JSR223 PreProcessor as a child of the 002_2_send payment request
  2. Put the following code into "Script" area:

    def testData = new com.example.TestData()
    def methods = testData.class.getDeclaredMethods()
    def payload = org.apache.commons.lang.reflect.MethodUtils.invokeExactMethod(testData, methods[vars.get('__jm__Loop Controller__idx') as int].getName())
    sampler.addNonEncodedArgument('',payload,'')
    sampler.setPostBodyRaw(true)
    

  3. Define "Loop Count" in the Loop Controller using __groovy() function like:

    ${__groovy(com.example.TestData.getDeclaredMethods().size(),)}
    

  4. Change com.example to your own package name and TestData to your class name

  5. Once you drop the new version of .jar to JMeter Classpath you will need to restart JMeter to pick up the changes

That's it, each iteration of the Loop Controller the JSR223 PreProcessor will execute the next function in your class and update the request body with the returned data:

References:

这篇关于我如何将 jar 文件用于循环计数器 - Jmeter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆