我想要做的是用rest和提供的文档复制一个应用场景,用户登录(请求oauth认证)并询问基本信息。我开始使用jmeter,并使该部分运行良好(日志记录和获取我所需的信息),如下所示:

但是现在我需要用jmeter的api从java中实现这一点,这就是我迷失的地方,因为除了blazemeter.com post 5种不用JMeter GUI启动JMeter测试的方法 --特别是ParraGraph4.3--这是我的起点,而且我的代码与它非常相似,所以我将使用它作为我迄今所做工作的示例。
// 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();几乎一样,我假设必须将元素添加到同一个LoopController中,以模拟用户从url上的url到同一个线程组(因为它们代表一个用户),还假设我必须将它们添加到testPlanTree中。
当我像上面一样运行它时,只执行取样器#3 (我用wireshark检查),在尝试运行了示例#1和#2之后,只执行了示例#1,这种行为就失去了我的能力。我对部分进行了注释并一次执行一个,它们确实被执行了请求#2和#3 --好吧,除了返回错误代码之外,不要做更多的事情,因为它们需要来自以前rest调用的信息,而且它们不能一起工作。
我花了一些时间阅读文档,但很难破译,谷歌也没有让我找到与我试图做的类似的例子。
在过去的两天里,我感到沮丧,任何帮助都是受欢迎的。
发布于 2020-08-16 09:19:50
执行下列更改:
或者,尝试使用此库:
使用Maven,添加到pom.xml中:
<dependency>
   <groupId>us.abstracta.jmeter</groupId>
   <projectId>jmeter-java-dsl</projectId>
   <version>0.1</version>
 </dependency>样本代码:
 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));
  }
 }https://stackoverflow.com/questions/26983068
复制相似问题