Search in sources :

Example 1 with SampleListener

use of org.apache.jmeter.samplers.SampleListener in project jmeter by apache.

the class TestCompiler method saveTransactionControllerConfigs.

private void saveTransactionControllerConfigs(TransactionController tc) {
    List<ConfigTestElement> configs = new ArrayList<>();
    List<Controller> controllers = new ArrayList<>();
    List<SampleListener> listeners = new ArrayList<>();
    List<Timer> timers = new ArrayList<>();
    List<Assertion> assertions = new ArrayList<>();
    List<PostProcessor> posts = new ArrayList<>();
    List<PreProcessor> pres = new ArrayList<>();
    for (int i = stack.size(); i > 0; i--) {
        addDirectParentControllers(controllers, stack.get(i - 1));
        for (Object item : testTree.list(stack.subList(0, i))) {
            if (item instanceof SampleListener) {
                listeners.add((SampleListener) item);
            }
            if (item instanceof Assertion) {
                assertions.add((Assertion) item);
            }
        }
    }
    SamplePackage pack = new SamplePackage(configs, listeners, timers, assertions, posts, pres, controllers);
    pack.setSampler(new TransactionSampler(tc, tc.getName()));
    pack.setRunningVersion(true);
    transactionControllerConfigMap.put(tc, pack);
}
Also used : ArrayList(java.util.ArrayList) Assertion(org.apache.jmeter.assertions.Assertion) PreProcessor(org.apache.jmeter.processor.PreProcessor) Controller(org.apache.jmeter.control.Controller) TransactionController(org.apache.jmeter.control.TransactionController) SampleListener(org.apache.jmeter.samplers.SampleListener) Timer(org.apache.jmeter.timers.Timer) TransactionSampler(org.apache.jmeter.control.TransactionSampler) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) PostProcessor(org.apache.jmeter.processor.PostProcessor)

Example 2 with SampleListener

use of org.apache.jmeter.samplers.SampleListener in project jmeter by apache.

the class JMeterThread method getSampleListeners.

/**
 * Get the SampleListeners for the sampler. Listeners who receive transaction sample
 * will not be in this list.
 *
 * @param samplePack
 * @param transactionPack
 * @param transactionSampler
 * @return the listeners who should receive the sample result
 */
private List<SampleListener> getSampleListeners(SamplePackage samplePack, SamplePackage transactionPack, TransactionSampler transactionSampler) {
    List<SampleListener> sampleListeners = samplePack.getSampleListeners();
    // Do not send subsamples to listeners which receive the transaction sample
    if (transactionSampler != null) {
        List<SampleListener> onlySubSamplerListeners = new ArrayList<>();
        List<SampleListener> transListeners = transactionPack.getSampleListeners();
        for (SampleListener listener : sampleListeners) {
            // Check if this instance is present in transaction listener list
            boolean found = false;
            for (SampleListener trans : transListeners) {
                // Check for the same instance
                if (trans == listener) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                onlySubSamplerListeners.add(listener);
            }
        }
        sampleListeners = onlySubSamplerListeners;
    }
    return sampleListeners;
}
Also used : ArrayList(java.util.ArrayList) SampleListener(org.apache.jmeter.samplers.SampleListener)

Example 3 with SampleListener

use of org.apache.jmeter.samplers.SampleListener in project jmeter by apache.

the class ListenerNotifier method notifyListeners.

/**
 * Notify a list of listeners that a sample has occurred.
 *
 * @param res
 *            the sample event that has occurred. Must be non-null.
 * @param listeners
 *            a list of the listeners which should be notified. This list
 *            must not be null and must contain only SampleListener
 *            elements.
 */
public void notifyListeners(SampleEvent res, List<SampleListener> listeners) {
    for (SampleListener sampleListener : listeners) {
        try {
            TestBeanHelper.prepare((TestElement) sampleListener);
            sampleListener.sampleOccurred(res);
        } catch (RuntimeException e) {
            log.error("Detected problem in Listener.", e);
            log.info("Continuing to process further listeners");
        }
    }
}
Also used : SampleListener(org.apache.jmeter.samplers.SampleListener)

Example 4 with SampleListener

use of org.apache.jmeter.samplers.SampleListener 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 5 with SampleListener

use of org.apache.jmeter.samplers.SampleListener in project jmeter by apache.

the class JMeterThread method executeSamplePackage.

/**
 * Execute the sampler with its pre/post processors, timers, assertions
 * Broadcast the result to the sample listeners
 */
private void executeSamplePackage(Sampler current, TransactionSampler transactionSampler, SamplePackage transactionPack, JMeterContext threadContext) {
    threadContext.setCurrentSampler(current);
    // Get the sampler ready to sample
    SamplePackage pack = compiler.configureSampler(current);
    runPreProcessors(pack.getPreProcessors());
    // Hack: save the package for any transaction controllers
    threadVars.putObject(PACKAGE_OBJECT, pack);
    delay(pack.getTimers());
    SampleResult result = null;
    if (running) {
        Sampler sampler = pack.getSampler();
        result = doSampling(threadContext, sampler);
    }
    // If we got any results, then perform processing on the result
    if (result != null) {
        if (!result.isIgnore()) {
            int nbActiveThreadsInThreadGroup = threadGroup.getNumberOfThreads();
            int nbTotalActiveThreads = JMeterContextService.getNumberOfThreads();
            fillThreadInformation(result, nbActiveThreadsInThreadGroup, nbTotalActiveThreads);
            SampleResult[] subResults = result.getSubResults();
            if (subResults != null) {
                for (SampleResult subResult : subResults) {
                    fillThreadInformation(subResult, nbActiveThreadsInThreadGroup, nbTotalActiveThreads);
                }
            }
            threadContext.setPreviousResult(result);
            runPostProcessors(pack.getPostProcessors());
            checkAssertions(pack.getAssertions(), result, threadContext);
            // PostProcessors can call setIgnore, so reevaluate here
            if (!result.isIgnore()) {
                // Do not send subsamples to listeners which receive the transaction sample
                List<SampleListener> sampleListeners = getSampleListeners(pack, transactionPack, transactionSampler);
                notifyListeners(sampleListeners, result);
            }
            compiler.done(pack);
            // Add the result as subsample of transaction if we are in a transaction
            if (transactionSampler != null && !result.isIgnore()) {
                transactionSampler.addSubSamplerResult(result);
            }
        } else {
            // This call is done by checkAssertions() , as we don't call it
            // for isIgnore, we explictely call it here
            setLastSampleOk(threadContext.getVariables(), result.isSuccessful());
            compiler.done(pack);
        }
        // Check if thread or test should be stopped
        if (result.isStopThread() || (!result.isSuccessful() && onErrorStopThread)) {
            stopThread();
        }
        if (result.isStopTest() || (!result.isSuccessful() && onErrorStopTest)) {
            shutdownTest();
        }
        if (result.isStopTestNow() || (!result.isSuccessful() && onErrorStopTestNow)) {
            stopTestNow();
        }
        if (result.getTestLogicalAction() != TestLogicalAction.CONTINUE) {
            threadContext.setTestLogicalAction(result.getTestLogicalAction());
        }
    } else {
        // Finish up
        compiler.done(pack);
    }
}
Also used : TransactionSampler(org.apache.jmeter.control.TransactionSampler) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) SampleListener(org.apache.jmeter.samplers.SampleListener)

Aggregations

SampleListener (org.apache.jmeter.samplers.SampleListener)6 ArrayList (java.util.ArrayList)2 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)2 TransactionSampler (org.apache.jmeter.control.TransactionSampler)2 RemoteException (java.rmi.RemoteException)1 Assertion (org.apache.jmeter.assertions.Assertion)1 Controller (org.apache.jmeter.control.Controller)1 TransactionController (org.apache.jmeter.control.TransactionController)1 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)1 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)1 PostProcessor (org.apache.jmeter.processor.PostProcessor)1 PreProcessor (org.apache.jmeter.processor.PreProcessor)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 SampleResult (org.apache.jmeter.samplers.SampleResult)1 Sampler (org.apache.jmeter.samplers.Sampler)1