我在设置JMeter测试计划时以编程方式向HTTPSampler添加自定义标头。
请参阅以下代码段:
HeaderManager headerManager = new HeaderManager();
headerManager.add(new Header("Foo", "Joe"));
sampler.setHeaderManager(headerManager);`
此处的问题是此自定义标头未发送到服务器。
这里有什么我想念的吗?
答案 0 :(得分:1)
它不会以这种方式工作,您需要修改代码以将HeaderManager HashTree添加到HTTPSamplerProxy HashTree。之后,您需要将此HTTPSamplerProxy添加到线程组,例如:
HashTree httpRequestTree = new HashTree();
httpRequestTree.add(httpRequest, manager);
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpRequestTree);
完整代码,以防万一:
// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();
// Create Header Manager
HeaderManager manager = new HeaderManager();
manager.add(new Header("Foo", "Joe"));
manager.setName(JMeterUtils.getResString("header_manager_title")); // $NON-NLS-1$
manager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
manager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());
// HTTP Sampler - open example.com
HTTPSamplerProxy httpRequest = new HTTPSamplerProxy();
httpRequest.setDomain("example.com");
httpRequest.setPort(80);
httpRequest.setPath("/");
httpRequest.setMethod("GET");
httpRequest.setName("Open example.com");
httpRequest.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpRequest.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
// HTTP Request Sampler and Header Manager
HashTree httpRequestTree = new HashTree();
httpRequestTree.add(httpRequest, manager);
// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpRequestTree);
查看Five Ways To Launch a JMeter Test without Using the JMeter GUI以获取有关以编程方式构建JMeter测试计划的更多信息。
答案 1 :(得分:0)
您可以选中jmeter-java-dsl,该代码可以执行相同的操作,但是代码更少。
例如,您可以添加以下Maven依赖项:
<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));
}
}
您将获得一个运行中的嵌入式JMeter测试,该测试具有对结果的简单断言以及每个请求结果的记录。