我正在尝试做的是使用其他api和提供的文档来复制应用场景,其中用户登录(请求oauth身份验证)并请求基本信息。 我开始使用jmeter gui并使该部分运行良好(记录并获取我需要的信息),如下所示:
但是现在我需要用jmeter的api来实现这一点,那就是我迷失的地方,因为除了blazemeter.com发布5 Ways To Launch a JMeter Test without Using the JMeter GUI之外没有太多的信息。起点,我的代码与它非常相似,所以我将它用作迄今为止我所做的例子
// 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,除了返回错误代码之外没有做更多的事情,因为他们需要来自之前的休息调用的信息并且他们不能一起工作。
我花了一些时间阅读文档,但很难解读,谷歌没有让我远远地寻找类似于我试图做的例子。
我过去两天都在这些,感到沮丧,欢迎任何帮助。
答案 0 :(得分:1)
进行以下更改:
或者,尝试使用此库:
使用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));
}
}