我在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();
}
}
,例外是
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
答案 0 :(得分:0)
我认为存在设计问题,因为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
并启动它,另一个进程加载testplan并运行它。