gpt4 book ai didi

java - Jmeter API : how to add post data to httpsampler

转载 作者:行者123 更新时间:2023-11-30 08:01:25 25 4
gpt4 key购买 nike

我是 jmeter 新手。我使用 java 和 jmeter api 编写了一个程序,用于获取对某些 Web 服务的 GET 请求的结果。

但是我无法向服务器发送 POST 请求。每当我在执行代码后打开 .jtl 文件时,请求正文为 null。

我的程序:

package com.walmart.gls.PerformanceFramework;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
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 AppTest {
public static void main(String args[]) throws FileNotFoundException,
IOException {
// Set jmeter home for the jmeter utils to load
File jmeterHome = new File("C:\\apache-jmeter-2.13\\");
String slash = System.getProperty("file.separator");

if (jmeterHome.exists()) {
File jmeterProperties = new File(jmeterHome.getPath() + slash
+ "bin" + slash + "jmeter.properties");
if (jmeterProperties.exists()) {
StandardJMeterEngine jmeter = new StandardJMeterEngine();

JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
// JMeterUtils.initLogging();// you can comment this line out to
// see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();

HashTree testPlanTree = new HashTree();

HTTPSamplerProxy httpsampler = new HTTPSamplerProxy();
httpsampler.setDomain("jsonplaceholder.typicode.com");
httpsampler.setPort(GlobalVariables.DEFAULT_HTTP_PORT);
httpsampler.setMethod("POST");
httpsampler.setPath("/posts");

HTTPArgument httpArgument = new HTTPArgument();
httpArgument
.setValue("{ data: {\"title\": \"venkatachalam\", \"body\": \"Venkata\", \"userId\": 3} }");
httpsampler.addTestElement(httpArgument);

httpsampler.setName("Posting POSTS");
httpsampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpsampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
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();

ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Sample 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());

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());

testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan,
threadGroup);
threadGroupHashTree.add(httpsampler);

SaveService
.saveTree(
testPlanTree,
new FileOutputStream(
"C:\\Users\\rvj03\\Documents\\JMETER results\\jmeter_api_sample.jmx"));

Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault(
"summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}

String reportFile = "C:\\Users\\rvj03\\Documents\\JMETER results\\report.jtl";
String csvFile = "C:\\Users\\rvj03\\Documents\\JMETER results\\report.csv";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(reportFile);
ResultCollector csvlogger = new ResultCollector(summer);
csvlogger.setFilename(csvFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
testPlanTree.add(testPlanTree.getArray()[0], csvlogger);
jmeter.configure(testPlanTree);
jmeter.run();

System.out.println("Test completed. See " + jmeterHome + slash
+ "report.jtl file for results");
System.out.println("JMeter .jmx script is available at "
+ jmeterHome + slash + "jmeter_api_sample.jmx");
System.exit(0);

}
}

System.err
.println("jmeterHome property is not set or pointing to incorrect location");
System.exit(1);
}
}

最佳答案

更改这些行:

HTTPArgument httpArgument = new HTTPArgument();
httpArgument
.setValue("{ data: {\"title\": \"venkatachalam\", \"body\": \"Venkata\", \"userId\": 3} }");
httpsampler.addTestElement(httpArgument);

以下内容:

httpsampler.addNonEncodedArgument("","{ data: {\"title\": \"venkatachalam\", \"body\": \"Venkata\", \"userId\": 3} }","");
httpsampler.setPostBodyRaw(true);

它应该能帮到你。请参阅HTTPSamplerBase.addNonEncodedArgument()方法 JavaDoc.

我还建议添加 HTTP Header Manager发送值为 application/jsonContent-Type header

关于java - Jmeter API : how to add post data to httpsampler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37699034/

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