gpt4 book ai didi

java - 无法在java中使用proxycontrol创建jmx文件

转载 作者:行者123 更新时间:2023-12-01 14:00:01 24 4
gpt4 key购买 nike

我用 java 编写了一个代码,使用 jmeter 在浏览器中记录 HTTP 流量。但是我无法将记录的流量保存在 jmx 文件中。我使用代码打开了一个端口并将相同的端口设置为浏览器。当我尝试执行代码时,它抛出异常。

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

import junit.framework.TestSuite;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LogicControllerGui;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.control.gui.WorkBenchGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.tree.JMeterTreeListener;
import org.apache.jmeter.visualizers.ViewResultsFullVisualizer;
import org.apache.jmeter.gui.tree.JMeterTreeModel;
import org.apache.jmeter.gui.tree.JMeterTreeNode;
import org.apache.jmeter.protocol.http.control.RecordingController;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.control.gui.RecordController;
import org.apache.jmeter.protocol.http.proxy.ProxyControl;
import org.apache.jmeter.protocol.http.proxy.gui.ProxyControlGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.java.control.gui.JavaTestSamplerGui;
import org.apache.jmeter.protocol.java.sampler.JavaSampler;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.WorkBench;
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;
import org.apache.jorphan.collections.ListedHashTree;

public class JmeterNewCode {

public static void Initialize() {
try {
// get the property value
String jmeterHome = "C:\\Users\\kumar\\Downloads\\apache-jmeter-2.13\\apache-jmeter-2.13";

// Initialize Properties, logging, locale, etc.
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "\\jmeter.properties");
// JMeterUtils.initLogging(); // you can comment this line out to
// see

// extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
} catch (Exception E) {
E.printStackTrace();
}
}

public static void setLoopController(ThreadGroup threadGroup, int loop, boolean isEnabled) {

LoopController loopController = new LoopController();
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());

loopController.setEnabled(isEnabled);
loopController.setLoops(loop);
threadGroup.setSamplerController(loopController);
}

public static HTTPSamplerProxy CreateHTTPSampler() {
HTTPSamplerProxy sampler = new HTTPSamplerProxy();
sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
sampler.setEnabled(true);
return sampler;
}

@SuppressWarnings("unused")
public static void main(String[] argv) throws Exception {

Initialize();

// JMeter Test Plan, basic all u JOrphan HashTree
ListedHashTree testPlanTree = new ListedHashTree();

// Test Plan
TestPlan testPlan = new TestPlan();
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setName("JMeter Script From Java Code");
testPlan.setEnabled(true);
testPlan.setFunctionalMode(false);
testPlan.setSerialized(false);
Arguments argument = new Arguments();
argument.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setUserDefinedVariables(argument);

// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
threadGroup.setName("Thread Group");
threadGroup.setEnabled(true);
setLoopController(threadGroup, 1, true);
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);

/*
* //Create HTTP Request HTTPSamplerProxy sampler = CreateHTTPSampler();
* Arguments argument1 = new Arguments(); sampler.setName("HTTP Request"
* ); sampler.setArguments(argument1);
* sampler.setDomain("www.google.com"); sampler.setMethod("GET");
* sampler.setFollowRedirects(true); sampler.setAutoRedirects(false);
* sampler.setUseKeepAlive(true); sampler.setDoMultipartPost(false);
* sampler.setMonitor(false); sampler.setImplementation("HttpClient4");
*/

// Create Recording Controller to get the captured requests
RecordingController rc = new RecordingController();
rc.setProperty(TestElement.TEST_CLASS, RecordingController.class.getName());
rc.setProperty(TestElement.GUI_CLASS, RecordController.class.getName());
rc.setProperty(TestElement.ENABLED, true);
rc.setProperty(TestElement.NAME, "Captured Requests");

// Add Proxy control
ProxyControl control = new ProxyControl();
control.setProperty(TestElement.TEST_CLASS, ProxyControl.class.getName());
control.setProperty(TestElement.GUI_CLASS, ProxyControlGui.class.getName());
control.setProperty(TestElement.ENABLED, true);
control.setProperty(TestElement.NAME, "HTTP TestScript Recorder");
control.setPort("4444");
control.setCaptureHttpHeaders(true);
control.setGroupingMode(0);
control.setAssertions(false);
control.setSamplerRedirectAutomatically(false);
control.setSamplerFollowRedirects(true);
control.setUseKeepAlive(true);
control.setSamplerDownloadImages(false);


JMeterTreeModel jModel = new JMeterTreeModel(control, threadGroup);
JMeterTreeListener jListener = new JMeterTreeListener(jModel);

GuiPackage gPackage = GuiPackage.getInstance(jListener, jModel);

JMeterTreeNode jNode = new JMeterTreeNode(rc, jModel);
control.setTarget(jNode);

// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
ListedHashTree threadGroupTree = (ListedHashTree) testPlanTree.add(testPlan, threadGroup);

// threadGroupTree.add(sampler);
ListedHashTree controlTree = (ListedHashTree) threadGroupTree.add(control);
threadGroupTree.add(rc);

StandardJMeterEngine jmeter = new StandardJMeterEngine();

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

File f = new File("C:\\Users\\kumar\\Desktop\\New folder\\file1.jtl");
String logJTLFile = f.getAbsolutePath();

// Result Collector for View Results
ResultCollector logger = new ResultCollector(summer);
logger.setProperty(TestElement.TEST_CLASS, ResultCollector.class.getName());
logger.setProperty(TestElement.GUI_CLASS, ViewResultsFullVisualizer.class.getName());
logger.setProperty(TestElement.ENABLED, true);
logger.setName("View Results");
logger.setFilename(logJTLFile);
SampleSaveConfiguration saveConfig = new SampleSaveConfiguration();
saveConfig.setResponseData(true);
saveConfig.setAssertions(true);
saveConfig.setAssertionResultsFailureMessage(true);
saveConfig.setAsXml(true);
saveConfig.setDataType(true);
saveConfig.setMessage(true);
saveConfig.setRequestHeaders(true);
saveConfig.setResponseData(true);
saveConfig.setResponseHeaders(true);
saveConfig.setSamplerData(true);
saveConfig.setSuccess(true);
saveConfig.setUrl(true);

logger.setSaveConfig(saveConfig);

threadGroupTree.add(logger);

SaveService.saveTree(testPlanTree,
new FileOutputStream("C:\\Users\\kumar\\Desktop\\New folder\\k.jmx"));

jmeter.configure(testPlanTree);
logger.testStarted();
System.out.println("-------------------------------");
control.startProxy();

for (int i = 0; i < 50; i++) {
Thread.sleep(1000);
}
control.stopProxy();
jmeter.run();
}
}

异常(exception)是

INFO    2016-03-04 22:25:12.685 [jmeter.e] (): All thread groups have been started
DEBUG 2016-03-04 22:25:12.685 [jmeter.t] (): Subtracting node, stack size = 2
DEBUG 2016-03-04 22:25:12.685 [jmeter.t] (): Subtracting node, stack size = 2
DEBUG 2016-03-04 22:25:12.685 [jmeter.t] (): Subtracting node, stack size = 2
DEBUG 2016-03-04 22:25:12.685 [jmeter.t] (): Subtracting node, stack size = 1
INFO 2016-03-04 22:25:12.685 [jmeter.t] (): Thread started: Thread Group 1-1
ERROR 2016-03-04 22:25:12.685 [jmeter.t] (): Test failed!
java.lang.NullPointerException
at org.apache.jmeter.threads.JMeterThread.threadStarted(JMeterThread.java:576)
at org.apache.jmeter.threads.JMeterThread.initRun(JMeterThread.java:567)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257)
at java.lang.Thread.run(Unknown Source)
INFO 2016-03-04 22:25:12.685 [jmeter.t] (): Thread finished: Thread Group 1-1
Exception in thread "Thread Group 1-1" INFO 2016-03-04 22:25:12.708 [jmeter.e] (): Notifying test listeners of end of test
java.lang.NullPointerException
at org.apache.jmeter.threads.JMeterThread.threadFinished(JMeterThread.java:589)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:315)
at java.lang.Thread.run(Unknown Source)
INFO 2016-03-04 22:25:12.710 [jmeter.r] (): Shutdown hook started
DEBUG 2016-03-04 22:25:12.710 [jmeter.r] (): Flushing: C:\Users\kumar\Desktop\New folder\file1.jtl
INFO 2016-03-04 22:25:12.710 [jmeter.r] (): Shutdown hook ended

最佳答案

我认为存在设计问题,因为 ProxyControl 需要 GuiPackage.getInstance() != null & StandardJMeterEngine 运行方法 (org.apache.jmeter .threads.JMeterThread.threadStarted()) 需要 GuiPackage.getInstance() == null OR GuiPackage.getInstance().getMainFrame() != null 但是 MainFrame 不容易创建,因为它需要 JMeter lib 文件夹中的库,您可以在此处查看 org.apache.jmeter.JMeter.startGui(String)

解决方案是让一个进程创建 jmx 文件并保存它,另一个进程创建 ProxyControl 并启动它,另一个进程加载测试计划并运行它。

关于java - 无法在java中使用proxycontrol创建jmx文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811365/

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