gpt4 book ai didi

java - 如何以编程方式获取 HttpSamplerProxy 元素(例如 HeaderManager 及其添加的 Header)

转载 作者:行者123 更新时间:2023-12-02 08:53:11 26 4
gpt4 key购买 nike

我有一个要求,即以编程方式添加 header 并创建 jmxscript,但我知道我想检索 HeaderManager 及其添加的 header 。

这是以编程方式添加 header 的代码

    HeaderManager headerManager = new HeaderManager();
headerManager.add(new Header("Content-Type", "application/json"));

我将这些数据提供到 JMX 脚本中,如下所示。

<org.apache.jorphan.collections.HashTree>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager">
<collectionProp name="HeaderManager.headers">
<elementProp name="Content-Type" elementType="Header">
<stringProp name="Header.name">Content-Type</stringProp>
<stringProp name="Header.value">application/json</stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<org.apache.jorphan.collections.HashTree/>
</org.apache.jorphan.collections.HashTree>

我尝试过这样的操作,但无法获取 null。

        headerManager=httpSampler.getHeaderManager();
PropertyIterator iter= headerManager.getHeaders().iterator();
while(iter.hasNext()) {
String name=iter.next().getName();
String value=iter.next().getStringValue();
addParameters.put(name, value);

我该如何摆脱这个困境?

最佳答案

正确的代码为HeaderManager初始化并添加相关Content-Type标题将类似于:

HeaderManager headerManager = new HeaderManager();
headerManager.setName("HTTP Header Manager");
headerManager.add(new Header("Content-Type", "application/json"));
headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
headerManager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());

以防万一,使用单个 HTTP 请求采样器和添加为该采样器子级的 HTTP header 管理器来创建 JMeter 测试计划的完整代码:

package com.example;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.gui.HeaderPanel;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

public class JMeterFromCode {
public static void main(String[] args) throws Exception {

String slash = System.getProperty("file.separator");
File jmeterHome = new File("c:/apps/jmeter");
File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");


//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());


// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();

// HTTP Request Sampler
HTTPSamplerProxy httpRequest = new HTTPSamplerProxy();
httpRequest.setDomain("example.com");
httpRequest.setPort(80);
httpRequest.setPath("/");
httpRequest.setMethod("GET");
httpRequest.setName("HTTP Request");
httpRequest.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpRequest.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

// HTTP Header Manager
HeaderManager headerManager = new HeaderManager();
headerManager.setName("HTTP Header Manager");
headerManager.add(new Header("Content-Type", "application/json"));
headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
headerManager.setProperty(TestElement.GUI_CLASS, HeaderPanel.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());

// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
HashTree httpSamplerHashTree = new HashTree();
httpSamplerHashTree.add(httpRequest, headerManager);
threadGroupHashTree.add(httpSamplerHashTree);

// save generated test plan to JMeter's .jmx file format
SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + slash + "example.jmx"));

}
}

更多信息:Five Ways To Launch a JMeter Test without Using the JMeter GUI

关于java - 如何以编程方式获取 HttpSamplerProxy 元素(例如 HeaderManager 及其添加的 Header),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60654079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com