简体   繁体   中英

Creating a JMeter test with multiple HTTPSampler from Java using the jmeter api

Well what I`m trying to do is to replicate an app scenario with the rest api and documentation provided, where a user logins (requesting oauth authentication) and asks for basic information. I started using the jmeter gui and got that part running good (logging and getting the info I needed) like this:jmeter gui测试计划

but now i need to do that from java with the api of jmeter and thats where im getting lost, since there isn´t much info other than the blazemeter.com post 5 Ways To Launch a JMeter Test without Using the JMeter GUI the parragraph 4.3 specifically that was my starting point, and my code is very similar to it so I will use it as example of what I have done so far

// HTTP Sampler 1
HTTPSampler httpSampler1 = new HTTPSampler();
httpSampler.setDomain("example1.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");
RegexExtractor regex1=new RegexExtractor();
regex1.setRegex("regex1");
regex1.setRefName("REGEX1");
regex1.setTemplate("$1$");
regex1.setMatchNumber("0");
regex1.setDefaultValue("false");
regex1.useHeaders();

// HTTP Sampler 2
HTTPSampler httpSampler2 = new HTTPSampler();
httpSampler2.setDomain("example.com");
httpSampler2.setPort(80);
httpSampler2.setPath("/2");
httpSampler2.setMethod("GET");
RegexExtractor regex2=new RegexExtractor();
regex2.setRegex("regex2");
regex2.setRefName("REGEX2");
regex2.setTemplate("$1$");
regex2.setMatchNumber("0");
regex2.setDefaultValue("false");
regex2.useHeaders();

// HTTP Sampler 3
HTTPSampler httpSampler3 = new HTTPSampler();
httpSampler3.setDomain("example.com");
httpSampler3.setPort(80);
httpSampler3.setPath("/3");
httpSampler3.setMethod("GET");
RegexExtractor regex3=new RegexExtractor();
regex3.setRegex("regex3");
regex3.setRefName("REGEX3");
regex3.setTemplate("$1$");
regex3.setMatchNumber("0");
regex3.setDefaultValue("false");
regex3.useHeaders();

// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.addTestElement(httpSampler1);
loopController.addTestElement(httpSampler2);
loopController.addTestElement(httpSampler3);
loopController.setFirst(true);
loopController.initialize();

// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);

// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");

// Construct Test Plan from previously initialized elements
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler1", httpSampler1);
testPlanTree.add("httpSampler2", httpSampler2);
testPlanTree.add("httpSampler3", httpSampler3);

// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();

Almost the same, I assume one has to add the elements to the same LoopController to simulate a user going from url on url and the same thread group(as they represent a user), also assuming that I have to add them in order to the testPlanTree.

When i run it just like above, only sampler #3 is executed (I´m checkng with wireshark), after that tryied running sampler #1 and #2 and only sampler #1 was executed, this behavior lost me. I commented parts and executed them one at a time and they do get executed the request #2 and #3 well dont do much more than returning error code since they need info from previous rest call and they dont work all together.

I spend some time reading the documentation but it was hard to decipher and google didn´t got me far looking for examples similar to what im trying to do.

I´ve spend the past two days with these, feeling frustrated, any help will be welcome.

Do the following changes:

  • add the samplers as children of LoopController instead of adding it to TestPlan element.
  • add the loopController as child of thread group
  • add the thread group as child of Test plan

Alternatively, Try using this library :

Using Maven, add to pom.xml:

<dependency>
   <groupId>us.abstracta.jmeter</groupId>
   <projectId>jmeter-java-dsl</projectId>
   <version>0.1</version>
 </dependency>

Sample code:

 import static org.assertj.core.api.Assertions.assertThat;
 import static us.abstracta.jmeter.javadsl.JmeterDsl.*;

 import java.time.Duration;
 import org.eclipse.jetty.http.MimeTypes.Type;
 import org.junit.jupiter.api.Test;
 import us.abstracta.jmeter.javadsl.TestPlanStats;

 public class PerformanceTest {

   @Test
   public void testPerformance() throws IOException {
     TestPlanStats stats = testPlan(
        threadGroup(2, 10,
        httpSampler("http://my.service")
           .post("{\"name\": \"test\"}", Type.APPLICATION_JSON)
     ),
      //this is just to log details of each request stats
     jtlWriter("test.jtl")
     ).run();
              assertThat(stats.overall().elapsedTimePercentile99()).isLessThan(Duration.ofSeconds(5));
  }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM