use of org.apache.jmeter.engine.JMeterEngine in project jmeter-plugins by undera.
the class TestPlanCheckTool method loadJMX.
private HashTree loadJMX(File file) throws Exception {
HashTree tree = SaveService.loadTree(file);
// unfortunately core JMeter code does not throw exception, we may only guess...
if (tree == null) {
throw new TestPlanBrokenException("There was problems loading test plan. Please investigate error messages above.");
}
// Remove the disabled items
JMeter.convertSubTree(tree);
JMeterEngine engine = new StandardJMeterEngine();
engine.configure(tree);
return tree;
}
use of org.apache.jmeter.engine.JMeterEngine in project jmeter by apache.
the class JMeter method runNonGui.
// run test in batch mode
@SuppressWarnings("JdkObsolete")
void runNonGui(String testFile, String logFile, boolean remoteStart, String remoteHostsString, boolean generateReportDashboard) throws ConfigurationException {
try {
File f = new File(testFile);
if (!f.exists() || !f.isFile()) {
throw new ConfigurationException("The file " + f.getAbsolutePath() + " doesn't exist or can't be opened");
}
FileServer.getFileServer().setBaseForScript(f);
HashTree tree = SaveService.loadTree(f);
// Deliberate use of deprecated ctor
@SuppressWarnings("deprecation") JMeterTreeModel // NOSONAR Create non-GUI version to avoid headless problems
treeModel = new JMeterTreeModel(new Object());
JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
treeModel.addSubTree(tree, root);
// Hack to resolve ModuleControllers in non GUI mode
SearchByClass<ReplaceableController> replaceableControllers = new SearchByClass<>(ReplaceableController.class);
tree.traverse(replaceableControllers);
Collection<ReplaceableController> replaceableControllersRes = replaceableControllers.getSearchResults();
for (ReplaceableController replaceableController : replaceableControllersRes) {
replaceableController.resolveReplacementSubTree(root);
}
// Ensure tree is interpreted (ReplaceableControllers are replaced)
// For GUI runs this is done in Start.java
HashTree clonedTree = convertSubTree(tree, true);
Summariser summariser = null;
// $NON-NLS-1$
String summariserName = JMeterUtils.getPropDefault("summariser.name", "");
if (summariserName.length() > 0) {
log.info("Creating summariser <{}>", summariserName);
println("Creating summariser <" + summariserName + ">");
summariser = new Summariser(summariserName);
}
ResultCollector resultCollector = null;
if (logFile != null) {
resultCollector = new ResultCollector(summariser);
resultCollector.setFilename(logFile);
clonedTree.add(clonedTree.getArray()[0], resultCollector);
} else {
// only add Summariser if it can not be shared with the ResultCollector
if (summariser != null) {
clonedTree.add(clonedTree.getArray()[0], summariser);
}
}
if (deleteResultFile) {
SearchByClass<ResultCollector> resultListeners = new SearchByClass<>(ResultCollector.class);
clonedTree.traverse(resultListeners);
Iterator<ResultCollector> irc = resultListeners.getSearchResults().iterator();
while (irc.hasNext()) {
ResultCollector rc = irc.next();
File resultFile = new File(rc.getFilename());
if (resultFile.exists() && !resultFile.delete()) {
throw new IllegalStateException("Could not delete results file " + resultFile.getAbsolutePath() + "(canRead:" + resultFile.canRead() + ", canWrite:" + resultFile.canWrite() + ")");
}
}
}
ReportGenerator reportGenerator = null;
if (logFile != null && generateReportDashboard) {
reportGenerator = new ReportGenerator(logFile, resultCollector);
}
// Used for remote notification of threads start/stop,see BUG 54152
// Summariser uses this feature to compute correctly number of threads
// when NON GUI mode is used
clonedTree.add(clonedTree.getArray()[0], new RemoteThreadsListenerTestElement());
List<JMeterEngine> engines = new ArrayList<>();
println("Created the tree successfully using " + testFile);
if (!remoteStart) {
JMeterEngine engine = new StandardJMeterEngine();
clonedTree.add(clonedTree.getArray()[0], new ListenToTest(org.apache.jmeter.JMeter.ListenToTest.RunMode.LOCAL, false, reportGenerator));
engine.configure(clonedTree);
long now = System.currentTimeMillis();
println("Starting standalone test @ " + new Date(now) + " (" + now + ")");
engines.add(engine);
engine.runTest();
} else {
// $NON-NLS-1$
java.util.StringTokenizer st = new java.util.StringTokenizer(remoteHostsString.trim(), ",");
List<String> hosts = new ArrayList<>();
while (st.hasMoreElements()) {
hosts.add(((String) st.nextElement()).trim());
}
ListenToTest testListener = new ListenToTest(org.apache.jmeter.JMeter.ListenToTest.RunMode.REMOTE, remoteStop, reportGenerator);
clonedTree.add(clonedTree.getArray()[0], testListener);
DistributedRunner distributedRunner = new DistributedRunner(this.remoteProps);
// NOSONAR
distributedRunner.setStdout(System.out);
// NOSONAR
distributedRunner.setStdErr(System.err);
distributedRunner.init(hosts, clonedTree);
engines.addAll(distributedRunner.getEngines());
testListener.setStartedRemoteEngines(engines);
distributedRunner.start();
}
startUdpDdaemon(engines);
} catch (ConfigurationException e) {
throw e;
} catch (Exception e) {
// NOSONAR
System.out.println("Error in NonGUIDriver " + e.toString());
log.error("Error in NonGUIDriver", e);
throw new ConfigurationException("Error in NonGUIDriver " + e.getMessage(), e);
}
}
use of org.apache.jmeter.engine.JMeterEngine in project jmeter by apache.
the class JMeter method waitForSignals.
private static void waitForSignals(final List<JMeterEngine> engines, DatagramSocket socket) {
byte[] buf = new byte[80];
// NOSONAR
System.out.println("Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port " + socket.getLocalPort());
DatagramPacket request = new DatagramPacket(buf, buf.length);
try {
while (true) {
socket.receive(request);
InetAddress address = request.getAddress();
// Only accept commands from the local host
if (address.isLoopbackAddress()) {
String command = new String(request.getData(), request.getOffset(), request.getLength(), StandardCharsets.US_ASCII);
// NOSONAR
System.out.println("Command: " + command + " received from " + address);
log.info("Command: {} received from {}", command, address);
switch(command) {
case "StopTestNow":
for (JMeterEngine engine : engines) {
engine.stopTest(true);
}
break;
case "Shutdown":
for (JMeterEngine engine : engines) {
engine.stopTest(false);
}
break;
case "HeapDump":
HeapDumper.dumpHeap();
break;
case "ThreadDump":
ThreadDumper.threadDump();
break;
default:
// NOSONAR
System.out.println("Command: " + command + " not recognised ");
}
}
}
} catch (Exception e) {
// NOSONAR
System.out.println(e);
} finally {
socket.close();
}
}
Aggregations