Search in sources :

Example 1 with ReplaceableController

use of org.apache.jmeter.control.ReplaceableController in project jmeter by apache.

the class JMeter method runNonGui.

// run test in batch mode
private void runNonGui(String testFile, String logFile, boolean remoteStart, String remoteHostsString, boolean generateReportDashboard) {
    try {
        File f = new File(testFile);
        if (!f.exists() || !f.isFile()) {
            println("Could not open " + testFile);
            return;
        }
        FileServer.getFileServer().setBaseForScript(f);
        HashTree tree = SaveService.loadTree(f);
        // Deliberate use of deprecated ctor
        @SuppressWarnings("deprecation") JMeterTreeModel // 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);
        }
        // Remove the disabled items
        // For GUI runs this is done in Start.java
        convertSubTree(tree);
        if (deleteResultFile) {
            SearchByClass<ResultCollector> resultListeners = new SearchByClass<>(ResultCollector.class);
            tree.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();
                }
            }
        }
        Summariser summer = null;
        //$NON-NLS-1$
        String summariserName = JMeterUtils.getPropDefault("summariser.name", "");
        if (summariserName.length() > 0) {
            log.info("Creating summariser <{}>", summariserName);
            println("Creating summariser <" + summariserName + ">");
            summer = new Summariser(summariserName);
        }
        ReportGenerator reportGenerator = null;
        if (logFile != null) {
            ResultCollector logger = new ResultCollector(summer);
            logger.setFilename(logFile);
            tree.add(tree.getArray()[0], logger);
            if (generateReportDashboard) {
                reportGenerator = new ReportGenerator(logFile, logger);
            }
        } else {
            // only add Summariser if it can not be shared with the ResultCollector
            if (summer != null) {
                tree.add(tree.getArray()[0], summer);
            }
        }
        // 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
        tree.add(tree.getArray()[0], new RemoteThreadsListenerTestElement());
        List<JMeterEngine> engines = new LinkedList<>();
        tree.add(tree.getArray()[0], new ListenToTest(remoteStart && remoteStop ? engines : null, reportGenerator));
        println("Created the tree successfully using " + testFile);
        if (!remoteStart) {
            JMeterEngine engine = new StandardJMeterEngine();
            engine.configure(tree);
            long now = System.currentTimeMillis();
            println("Starting the test @ " + new Date(now) + " (" + now + ")");
            engine.runTest();
            engines.add(engine);
        } else {
            //$NON-NLS-1$
            java.util.StringTokenizer st = new java.util.StringTokenizer(remoteHostsString, ",");
            List<String> hosts = new LinkedList<>();
            while (st.hasMoreElements()) {
                hosts.add((String) st.nextElement());
            }
            DistributedRunner distributedRunner = new DistributedRunner(this.remoteProps);
            // NOSONAR
            distributedRunner.setStdout(System.out);
            // NOSONAR
            distributedRunner.setStdErr(System.err);
            distributedRunner.init(hosts, tree);
            engines.addAll(distributedRunner.getEngines());
            distributedRunner.start();
        }
        startUdpDdaemon(engines);
    } catch (Exception e) {
        //NOSONAR
        System.out.println("Error in NonGUIDriver " + e.toString());
        log.error("Error in NonGUIDriver", e);
    }
}
Also used : HashTree(org.apache.jorphan.collections.HashTree) RemoteThreadsListenerTestElement(org.apache.jmeter.threads.RemoteThreadsListenerTestElement) JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) Summariser(org.apache.jmeter.reporters.Summariser) ReportGenerator(org.apache.jmeter.report.dashboard.ReportGenerator) StringTokenizer(java.util.StringTokenizer) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) ReplaceableController(org.apache.jmeter.control.ReplaceableController) ResultCollector(org.apache.jmeter.reporters.ResultCollector) StandardJMeterEngine(org.apache.jmeter.engine.StandardJMeterEngine) SearchByClass(org.apache.jorphan.collections.SearchByClass) LinkedList(java.util.LinkedList) Date(java.util.Date) ConfigurationException(org.apache.jmeter.report.config.ConfigurationException) JMeterException(org.apache.jorphan.util.JMeterException) FileNotFoundException(java.io.FileNotFoundException) ConversionException(com.thoughtworks.xstream.converters.ConversionException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) GenerationException(org.apache.jmeter.report.dashboard.GenerationException) StringTokenizer(java.util.StringTokenizer) ClientJMeterEngine(org.apache.jmeter.engine.ClientJMeterEngine) JMeterEngine(org.apache.jmeter.engine.JMeterEngine) StandardJMeterEngine(org.apache.jmeter.engine.StandardJMeterEngine) DistributedRunner(org.apache.jmeter.engine.DistributedRunner) File(java.io.File)

Example 2 with ReplaceableController

use of org.apache.jmeter.control.ReplaceableController in project jmeter by apache.

the class JMeter method convertSubTree.

/**
     * Remove disabled elements
     * Replace the ReplaceableController with the target subtree
     *
     * @param tree The {@link HashTree} to convert
     */
public static void convertSubTree(HashTree tree) {
    LinkedList<Object> copyList = new LinkedList<>(tree.list());
    for (Object o : copyList) {
        if (o instanceof TestElement) {
            TestElement item = (TestElement) o;
            if (item.isEnabled()) {
                if (item instanceof ReplaceableController) {
                    ReplaceableController rc = ensureReplaceableControllerIsLoaded(item);
                    HashTree subTree = tree.getTree(item);
                    if (subTree != null) {
                        HashTree replacementTree = rc.getReplacementSubTree();
                        if (replacementTree != null) {
                            convertSubTree(replacementTree);
                            tree.replaceKey(item, rc);
                            tree.set(rc, replacementTree);
                        }
                    }
                } else {
                    // not Replaceable Controller
                    convertSubTree(tree.getTree(item));
                }
            } else {
                // Not enabled
                tree.remove(item);
            }
        } else {
            // Not a TestElement
            JMeterTreeNode item = (JMeterTreeNode) o;
            if (item.isEnabled()) {
                // @see StandardJMeterEngine.run()
                if (item.getUserObject() instanceof ReplaceableController) {
                    TestElement controllerAsItem = item.getTestElement();
                    ReplaceableController rc = ensureReplaceableControllerIsLoaded(controllerAsItem);
                    HashTree subTree = tree.getTree(item);
                    if (subTree != null) {
                        HashTree replacementTree = rc.getReplacementSubTree();
                        if (replacementTree != null) {
                            convertSubTree(replacementTree);
                            tree.replaceKey(item, rc);
                            tree.set(rc, replacementTree);
                        }
                    }
                } else {
                    // Not a ReplaceableController
                    convertSubTree(tree.getTree(item));
                    TestElement testElement = item.getTestElement();
                    tree.replaceKey(item, testElement);
                }
            } else {
                // Not enabled
                tree.remove(item);
            }
        }
    }
}
Also used : HashTree(org.apache.jorphan.collections.HashTree) ReplaceableController(org.apache.jmeter.control.ReplaceableController) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TestElement(org.apache.jmeter.testelement.TestElement) RemoteThreadsListenerTestElement(org.apache.jmeter.threads.RemoteThreadsListenerTestElement) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)2 ReplaceableController (org.apache.jmeter.control.ReplaceableController)2 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)2 RemoteThreadsListenerTestElement (org.apache.jmeter.threads.RemoteThreadsListenerTestElement)2 HashTree (org.apache.jorphan.collections.HashTree)2 ConversionException (com.thoughtworks.xstream.converters.ConversionException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 SocketException (java.net.SocketException)1 Date (java.util.Date)1 StringTokenizer (java.util.StringTokenizer)1 ClientJMeterEngine (org.apache.jmeter.engine.ClientJMeterEngine)1 DistributedRunner (org.apache.jmeter.engine.DistributedRunner)1 JMeterEngine (org.apache.jmeter.engine.JMeterEngine)1 StandardJMeterEngine (org.apache.jmeter.engine.StandardJMeterEngine)1 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)1 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)1 ConfigurationException (org.apache.jmeter.report.config.ConfigurationException)1