Search in sources :

Example 1 with TestStateListener

use of org.apache.jmeter.testelement.TestStateListener in project jmeter by apache.

the class ProxyControl method notifyTestListenersOfEnd.

/**
 * This will notify test listeners directly within the Proxy that the 'test'
 * (here meaning the proxy recording) has ended.
 */
@SuppressWarnings("JdkObsolete")
private void notifyTestListenersOfEnd() {
    JMeterTreeModel treeModel = getJmeterTreeModel();
    JMeterTreeNode myNode = treeModel.getNodeOf(this);
    if (myNode != null) {
        Enumeration<?> kids = myNode.children();
        while (kids.hasMoreElements()) {
            JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
            if (subNode.isEnabled()) {
                TestElement testElement = subNode.getTestElement();
                if (testElement instanceof TestStateListener) {
                    // TL - TE
                    ((TestStateListener) testElement).testEnded();
                }
            }
        }
    }
}
Also used : JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TestStateListener(org.apache.jmeter.testelement.TestStateListener) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) NonTestElement(org.apache.jmeter.testelement.NonTestElement)

Example 2 with TestStateListener

use of org.apache.jmeter.testelement.TestStateListener in project jmeter by apache.

the class ProxyControl method notifyTestListenersOfStart.

/**
 * This will notify test listeners directly within the Proxy that the 'test'
 * (here meaning the proxy recording) has started.
 */
@SuppressWarnings("JdkObsolete")
private void notifyTestListenersOfStart() {
    JMeterTreeModel treeModel = getJmeterTreeModel();
    JMeterTreeNode myNode = treeModel.getNodeOf(this);
    if (myNode != null) {
        Enumeration<?> kids = myNode.children();
        while (kids.hasMoreElements()) {
            JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
            if (subNode.isEnabled()) {
                TestElement testElement = subNode.getTestElement();
                if (testElement instanceof TestStateListener) {
                    TestBeanHelper.prepare(testElement);
                    ((TestStateListener) testElement).testStarted();
                }
            }
        }
    }
}
Also used : JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TestStateListener(org.apache.jmeter.testelement.TestStateListener) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) NonTestElement(org.apache.jmeter.testelement.NonTestElement)

Example 3 with TestStateListener

use of org.apache.jmeter.testelement.TestStateListener 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 && 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 4 with TestStateListener

use of org.apache.jmeter.testelement.TestStateListener 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 ArrayList<>(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: {} to finish before starting next setup group", groupName);
                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: {} to finish before starting next group", groupName);
            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 = 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: {} to finish before starting next post group", groupName);
                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 : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AbstractThreadGroup(org.apache.jmeter.threads.AbstractThreadGroup) TestStateListener(org.apache.jmeter.testelement.TestStateListener) SearchByClass(org.apache.jorphan.collections.SearchByClass) ListenerNotifier(org.apache.jmeter.threads.ListenerNotifier) PostThreadGroup(org.apache.jmeter.threads.PostThreadGroup) SetupThreadGroup(org.apache.jmeter.threads.SetupThreadGroup)

Example 5 with TestStateListener

use of org.apache.jmeter.testelement.TestStateListener in project jmeter by apache.

the class StandardJMeterEngine method notifyTestListenersOfEnd.

@SuppressWarnings("JdkObsolete")
private void notifyTestListenersOfEnd(SearchByClass<TestStateListener> testListeners) {
    log.info("Notifying test listeners of end of test");
    for (TestStateListener tl : testListeners.getSearchResults()) {
        try {
            if (host == null) {
                tl.testEnded();
            } else {
                tl.testEnded(host);
            }
        } catch (Exception e) {
            log.warn("Error encountered during shutdown of " + tl.toString(), e);
        }
    }
    if (host != null) {
        log.info("Test has ended on host {} ", host);
        long now = System.currentTimeMillis();
        System.out.println(// NOSONAR Intentional
        "Finished the test on host " + host + " @ " + new Date(now) + " (" + now + ")" + (EXIT_AFTER_TEST ? " - exit requested." : ""));
        if (EXIT_AFTER_TEST) {
            exit();
        }
    }
    active = false;
}
Also used : TestStateListener(org.apache.jmeter.testelement.TestStateListener) JMeterStopTestException(org.apache.jorphan.util.JMeterStopTestException) Date(java.util.Date)

Aggregations

TestStateListener (org.apache.jmeter.testelement.TestStateListener)6 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)2 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)2 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)2 NonTestElement (org.apache.jmeter.testelement.NonTestElement)2 TestElement (org.apache.jmeter.testelement.TestElement)2 AbstractThreadGroup (org.apache.jmeter.threads.AbstractThreadGroup)2 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Function (org.apache.jmeter.functions.Function)1 InvalidVariableException (org.apache.jmeter.functions.InvalidVariableException)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