Search in sources :

Example 1 with AbstractThreadGroup

use of org.apache.jmeter.threads.AbstractThreadGroup in project jmeter by apache.

the class ModuleControllerGui method buildTreeNodeModel.

/**
     * Recursively build module to run tree. Only 4 types of elements are allowed to be added:
     * - All controllers except ModuleController
     * - TestPlan
     * - TestFragmentController
     * - AbstractThreadGroup
     * 
     * @param node - element of test plan tree
     * @param level - level of element in a tree
     * @param parent
     */
private void buildTreeNodeModel(JMeterTreeNode node, int level, DefaultMutableTreeNode parent) {
    if (node != null) {
        for (int i = 0; i < node.getChildCount(); i++) {
            JMeterTreeNode cur = (JMeterTreeNode) node.getChildAt(i);
            TestElement te = cur.getTestElement();
            if (te instanceof Controller && !(te instanceof ModuleController) && level > 0) {
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(cur);
                parent.add(newNode);
                buildTreeNodeModel(cur, level + 1, newNode);
            } else if (te instanceof TestFragmentController || te instanceof AbstractThreadGroup) {
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(cur);
                parent.add(newNode);
                buildTreeNodeModel(cur, level + 1, newNode);
            } else if (te instanceof TestPlan) {
                ((DefaultMutableTreeNode) moduleToRunTreeModel.getRoot()).setUserObject(cur);
                buildTreeNodeModel(cur, level, (DefaultMutableTreeNode) moduleToRunTreeModel.getRoot());
            }
        }
    }
}
Also used : TestFragmentController(org.apache.jmeter.control.TestFragmentController) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TestPlan(org.apache.jmeter.testelement.TestPlan) AbstractThreadGroup(org.apache.jmeter.threads.AbstractThreadGroup) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TestElement(org.apache.jmeter.testelement.TestElement) ModuleController(org.apache.jmeter.control.ModuleController) Controller(org.apache.jmeter.control.Controller) TestFragmentController(org.apache.jmeter.control.TestFragmentController) ModuleController(org.apache.jmeter.control.ModuleController)

Example 2 with AbstractThreadGroup

use of org.apache.jmeter.threads.AbstractThreadGroup in project jmeter by apache.

the class Start method doAction.

@Override
public void doAction(ActionEvent e) {
    if (e.getActionCommand().equals(ActionNames.ACTION_START)) {
        popupShouldSave(e);
        startEngine(false);
    } else if (e.getActionCommand().equals(ActionNames.ACTION_START_NO_TIMERS)) {
        popupShouldSave(e);
        startEngine(true);
    } else if (e.getActionCommand().equals(ActionNames.ACTION_STOP)) {
        if (engine != null) {
            log.info("Stopping test");
            GuiPackage.getInstance().getMainFrame().showStoppingMessage("");
            engine.stopTest();
        }
    } else if (e.getActionCommand().equals(ActionNames.ACTION_SHUTDOWN)) {
        if (engine != null) {
            log.info("Shutting test down");
            GuiPackage.getInstance().getMainFrame().showStoppingMessage("");
            engine.askThreadsToStop();
        }
    } else if (e.getActionCommand().equals(ActionNames.RUN_TG) || e.getActionCommand().equals(ActionNames.RUN_TG_NO_TIMERS) || e.getActionCommand().equals(ActionNames.VALIDATE_TG)) {
        popupShouldSave(e);
        boolean noTimers = e.getActionCommand().equals(ActionNames.RUN_TG_NO_TIMERS);
        boolean isValidation = e.getActionCommand().equals(ActionNames.VALIDATE_TG);
        JMeterTreeListener treeListener = GuiPackage.getInstance().getTreeListener();
        JMeterTreeNode[] nodes = treeListener.getSelectedNodes();
        nodes = Copy.keepOnlyAncestors(nodes);
        AbstractThreadGroup[] tg = keepOnlyThreadGroups(nodes);
        if (nodes.length > 0) {
            startEngine(noTimers, isValidation, tg);
        } else {
            log.warn("No thread group selected the test will not be started");
        }
    }
}
Also used : JMeterTreeListener(org.apache.jmeter.gui.tree.JMeterTreeListener) AbstractThreadGroup(org.apache.jmeter.threads.AbstractThreadGroup) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode)

Example 3 with AbstractThreadGroup

use of org.apache.jmeter.threads.AbstractThreadGroup in project jmeter by apache.

the class Start method removeThreadGroupsFromHashTree.

/**
     * Remove thread groups from testTree that are not in threadGroupsToKeep
     * @param testTree {@link HashTree}
     * @param threadGroupsToKeep Array of {@link AbstractThreadGroup} to keep
     */
private void removeThreadGroupsFromHashTree(HashTree testTree, AbstractThreadGroup[] threadGroupsToKeep) {
    LinkedList<Object> copyList = new LinkedList<>(testTree.list());
    for (Object o : copyList) {
        TestElement item = (TestElement) o;
        if (o instanceof AbstractThreadGroup) {
            if (!isInThreadGroups(item, threadGroupsToKeep)) {
                // where 2 AbstractTestElement can be equals but have different hashcode
                try {
                    item.setEnabled(false);
                    testTree.remove(item);
                } finally {
                    item.setEnabled(true);
                }
            } else {
                removeThreadGroupsFromHashTree(testTree.getTree(item), threadGroupsToKeep);
            }
        } else {
            removeThreadGroupsFromHashTree(testTree.getTree(item), threadGroupsToKeep);
        }
    }
}
Also used : AbstractThreadGroup(org.apache.jmeter.threads.AbstractThreadGroup) TestElement(org.apache.jmeter.testelement.TestElement) LinkedList(java.util.LinkedList)

Example 4 with AbstractThreadGroup

use of org.apache.jmeter.threads.AbstractThreadGroup in project jmeter by apache.

the class ConvertListeners method addNode.

/**
     * {@inheritDoc}
     */
@Override
public void addNode(Object node, HashTree subTree) {
    for (Object item : subTree.list()) {
        if (item instanceof AbstractThreadGroup) {
            if (log.isDebugEnabled()) {
                log.debug("num threads = {}", ((AbstractThreadGroup) item).getNumThreads());
            }
        }
        if (item instanceof Remoteable) {
            if (item instanceof RemoteThreadsListenerTestElement) {
                // Used for remote notification of threads start/stop,see BUG 54152
                try {
                    RemoteThreadsListenerWrapper wrapper = new RemoteThreadsListenerWrapper(new RemoteThreadsListenerImpl());
                    subTree.replaceKey(item, wrapper);
                } catch (RemoteException e) {
                    log.error("Error replacing {} by wrapper: {}", RemoteThreadsListenerTestElement.class, RemoteThreadsListenerWrapper.class, e);
                }
                continue;
            }
            if (item instanceof ThreadListener) {
                // TODO Document the reason for this
                log.error("Cannot handle ThreadListener Remotable item: {}", item.getClass());
                continue;
            }
            try {
                RemoteSampleListener rtl = new RemoteSampleListenerImpl(item);
                if (item instanceof TestStateListener && item instanceof SampleListener) {
                    // TL - all
                    RemoteListenerWrapper wrap = new RemoteListenerWrapper(rtl);
                    subTree.replaceKey(item, wrap);
                } else if (item instanceof TestStateListener) {
                    RemoteTestListenerWrapper wrap = new RemoteTestListenerWrapper(rtl);
                    subTree.replaceKey(item, wrap);
                } else if (item instanceof SampleListener) {
                    RemoteSampleListenerWrapper wrap = new RemoteSampleListenerWrapper(rtl);
                    subTree.replaceKey(item, wrap);
                } else {
                    if (log.isWarnEnabled()) {
                        log.warn("Could not replace Remotable item: {}", item.getClass());
                    }
                }
            } catch (RemoteException e) {
                // $NON-NLS-1$
                log.error("RemoteException occurred while replacing Remotable item.", e);
            }
        }
    }
}
Also used : RemoteThreadsListenerWrapper(org.apache.jmeter.threads.RemoteThreadsListenerWrapper) ThreadListener(org.apache.jmeter.testelement.ThreadListener) AbstractThreadGroup(org.apache.jmeter.threads.AbstractThreadGroup) Remoteable(org.apache.jmeter.samplers.Remoteable) RemoteThreadsListenerTestElement(org.apache.jmeter.threads.RemoteThreadsListenerTestElement) TestStateListener(org.apache.jmeter.testelement.TestStateListener) RemoteTestListenerWrapper(org.apache.jmeter.samplers.RemoteTestListenerWrapper) RemoteSampleListener(org.apache.jmeter.samplers.RemoteSampleListener) RemoteSampleListenerWrapper(org.apache.jmeter.samplers.RemoteSampleListenerWrapper) SampleListener(org.apache.jmeter.samplers.SampleListener) RemoteSampleListener(org.apache.jmeter.samplers.RemoteSampleListener) RemoteListenerWrapper(org.apache.jmeter.samplers.RemoteListenerWrapper) RemoteException(java.rmi.RemoteException) RemoteSampleListenerImpl(org.apache.jmeter.samplers.RemoteSampleListenerImpl) RemoteThreadsListenerImpl(org.apache.jmeter.threads.RemoteThreadsListenerImpl)

Example 5 with AbstractThreadGroup

use of org.apache.jmeter.threads.AbstractThreadGroup in project jmeter by apache.

the class StandardJMeterEngine method run.

@Override
public void run() {
    log.info("Running the test!");
    running = true;
    /*
         * Ensure that the sample variables are correctly initialised for each run.
         */
    SampleEvent.initSampleVariables();
    JMeterContextService.startTest();
    try {
        PreCompiler compiler = new PreCompiler();
        test.traverse(compiler);
    } catch (RuntimeException e) {
        log.error("Error occurred compiling the tree:", e);
        JMeterUtils.reportErrorToUser("Error occurred compiling the tree: - see log file", e);
        // no point continuing
        return;
    }
    /**
         * Notification of test listeners needs to happen after function
         * replacement, but before setting RunningVersion to true.
         */
    // TL - S&E
    SearchByClass<TestStateListener> testListeners = new SearchByClass<>(TestStateListener.class);
    test.traverse(testListeners);
    // Merge in any additional test listeners
    // currently only used by the function parser
    testListeners.getSearchResults().addAll(testList);
    // no longer needed
    testList.clear();
    test.traverse(new TurnElementsOn());
    notifyTestListenersOfStart(testListeners);
    List<?> testLevelElements = new LinkedList<>(test.list(test.getArray()[0]));
    removeThreadGroups(testLevelElements);
    SearchByClass<SetupThreadGroup> setupSearcher = new SearchByClass<>(SetupThreadGroup.class);
    SearchByClass<AbstractThreadGroup> searcher = new SearchByClass<>(AbstractThreadGroup.class);
    SearchByClass<PostThreadGroup> postSearcher = new SearchByClass<>(PostThreadGroup.class);
    test.traverse(setupSearcher);
    test.traverse(searcher);
    test.traverse(postSearcher);
    TestCompiler.initialize();
    // for each thread group, generate threads
    // hand each thread the sampler controller
    // and the listeners, and the timer
    Iterator<SetupThreadGroup> setupIter = setupSearcher.getSearchResults().iterator();
    Iterator<AbstractThreadGroup> iter = searcher.getSearchResults().iterator();
    Iterator<PostThreadGroup> postIter = postSearcher.getSearchResults().iterator();
    ListenerNotifier notifier = new ListenerNotifier();
    int groupCount = 0;
    JMeterContextService.clearTotalThreads();
    if (setupIter.hasNext()) {
        log.info("Starting setUp thread groups");
        while (running && setupIter.hasNext()) {
            //for each setup thread group
            AbstractThreadGroup group = setupIter.next();
            groupCount++;
            String groupName = group.getName();
            log.info("Starting setUp ThreadGroup: " + groupCount + " : " + groupName);
            startThreadGroup(group, groupCount, setupSearcher, testLevelElements, notifier);
            if (serialized && setupIter.hasNext()) {
                log.info("Waiting for setup thread group: " + groupName + " to finish before starting next setup group");
                group.waitThreadsStopped();
            }
        }
        log.info("Waiting for all setup thread groups to exit");
        //wait for all Setup Threads To Exit
        waitThreadsStopped();
        log.info("All Setup Threads have ended");
        groupCount = 0;
        JMeterContextService.clearTotalThreads();
    }
    // The groups have all completed now                
    groups.clear();
    /*
         * Here's where the test really starts. Run a Full GC now: it's no harm
         * at all (just delays test start by a tiny amount) and hitting one too
         * early in the test can impair results for short tests.
         */
    JMeterUtils.helpGC();
    JMeterContextService.getContext().setSamplingStarted(true);
    // still running at this point, i.e. setUp was not cancelled
    boolean mainGroups = running;
    while (running && iter.hasNext()) {
        // for each thread group
        AbstractThreadGroup group = iter.next();
        //future Thread Group objects wouldn't execute.
        if (group instanceof SetupThreadGroup || group instanceof PostThreadGroup) {
            continue;
        }
        groupCount++;
        String groupName = group.getName();
        log.info("Starting ThreadGroup: " + groupCount + " : " + groupName);
        startThreadGroup(group, groupCount, searcher, testLevelElements, notifier);
        if (serialized && iter.hasNext()) {
            log.info("Waiting for thread group: " + groupName + " to finish before starting next group");
            group.waitThreadsStopped();
        }
    }
    // end of thread groups
    if (groupCount == 0) {
        // No TGs found
        log.info("No enabled thread groups found");
    } else {
        if (running) {
            log.info("All thread groups have been started");
        } else {
            log.info("Test stopped - no more thread groups will be started");
        }
    }
    //wait for all Test Threads To Exit
    waitThreadsStopped();
    // The groups have all completed now            
    groups.clear();
    if (postIter.hasNext()) {
        groupCount = 0;
        JMeterContextService.clearTotalThreads();
        log.info("Starting tearDown thread groups");
        if (mainGroups && !running) {
            // i.e. shutdown/stopped during main thread groups
            // re-enable for tearDown if necessary
            running = shutdown && tearDownOnShutdown;
        }
        while (running && postIter.hasNext()) {
            //for each setup thread group
            AbstractThreadGroup group = postIter.next();
            groupCount++;
            String groupName = group.getName();
            log.info("Starting tearDown ThreadGroup: " + groupCount + " : " + groupName);
            startThreadGroup(group, groupCount, postSearcher, testLevelElements, notifier);
            if (serialized && postIter.hasNext()) {
                log.info("Waiting for post thread group: " + groupName + " to finish before starting next post group");
                group.waitThreadsStopped();
            }
        }
        // wait for Post threads to stop
        waitThreadsStopped();
    }
    notifyTestListenersOfEnd(testListeners);
    JMeterContextService.endTest();
    if (JMeter.isNonGUI() && SYSTEM_EXIT_FORCED) {
        log.info("Forced JVM shutdown requested at end of test");
        // NOSONAR Intentional
        System.exit(0);
    }
}
Also used : AbstractThreadGroup(org.apache.jmeter.threads.AbstractThreadGroup) TestStateListener(org.apache.jmeter.testelement.TestStateListener) SearchByClass(org.apache.jorphan.collections.SearchByClass) LinkedList(java.util.LinkedList) ListenerNotifier(org.apache.jmeter.threads.ListenerNotifier) PostThreadGroup(org.apache.jmeter.threads.PostThreadGroup) SetupThreadGroup(org.apache.jmeter.threads.SetupThreadGroup)

Aggregations

AbstractThreadGroup (org.apache.jmeter.threads.AbstractThreadGroup)8 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)3 LinkedList (java.util.LinkedList)2 TestElement (org.apache.jmeter.testelement.TestElement)2 TestStateListener (org.apache.jmeter.testelement.TestStateListener)2 RemoteException (java.rmi.RemoteException)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1 Controller (org.apache.jmeter.control.Controller)1 LoopController (org.apache.jmeter.control.LoopController)1 ModuleController (org.apache.jmeter.control.ModuleController)1 TestFragmentController (org.apache.jmeter.control.TestFragmentController)1 JMeterTreeListener (org.apache.jmeter.gui.tree.JMeterTreeListener)1 RecordingController (org.apache.jmeter.protocol.http.control.RecordingController)1 RemoteListenerWrapper (org.apache.jmeter.samplers.RemoteListenerWrapper)1 RemoteSampleListener (org.apache.jmeter.samplers.RemoteSampleListener)1 RemoteSampleListenerImpl (org.apache.jmeter.samplers.RemoteSampleListenerImpl)1 RemoteSampleListenerWrapper (org.apache.jmeter.samplers.RemoteSampleListenerWrapper)1 RemoteTestListenerWrapper (org.apache.jmeter.samplers.RemoteTestListenerWrapper)1 Remoteable (org.apache.jmeter.samplers.Remoteable)1 SampleListener (org.apache.jmeter.samplers.SampleListener)1