How can i use jar file into loop counter - Jmeter

若如初见. 提交于 2020-01-23 18:02:35

问题


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:


  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:

  • Java Reflection API
  • Apache Groovy - Why and How You Should Use It


来源:https://stackoverflow.com/questions/58609835/how-can-i-use-jar-file-into-loop-counter-jmeter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!