Search in sources :

Example 26 with Sampler

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

the class TransactionController method nextWithoutTransactionSampler.

////////////////////// Transaction Controller - additional sample //////////////////////////////
private Sampler nextWithoutTransactionSampler() {
    if (// must be the start of the subtree
    isFirst()) {
        calls = 0;
        noFailingSamples = 0;
        res = new SampleResult();
        res.setSampleLabel(getName());
        // Assume success
        res.setSuccessful(true);
        res.sampleStart();
        //???
        prevEndTime = res.getStartTime();
        pauseTime = 0;
    }
    boolean isLast = current == super.subControllersAndSamplers.size();
    Sampler returnValue = super.next();
    if (// Must be the end of the controller
    returnValue == null && isLast) {
        if (res != null) {
            // See BUG 55816
            if (!isIncludeTimers()) {
                long processingTimeOfLastChild = res.currentTimeInMillis() - prevEndTime;
                pauseTime += processingTimeOfLastChild;
            }
            res.setIdleTime(pauseTime + res.getIdleTime());
            res.sampleEnd();
            res.setResponseMessage(TransactionController.NUMBER_OF_SAMPLES_IN_TRANSACTION_PREFIX + calls + ", number of failing samples : " + noFailingSamples);
            if (res.isSuccessful()) {
                res.setResponseCodeOK();
            }
            notifyListeners();
        }
    } else {
        // We have sampled one of our children
        calls++;
    }
    return returnValue;
}
Also used : Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult)

Example 27 with Sampler

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

the class JMeterThread method run.

@Override
public void run() {
    // threadContext is not thread-safe, so keep within thread
    JMeterContext threadContext = JMeterContextService.getContext();
    LoopIterationListener iterationListener = null;
    try {
        iterationListener = initRun(threadContext);
        while (running) {
            Sampler sam = threadGroupLoopController.next();
            while (running && sam != null) {
                processSampler(sam, null, threadContext);
                threadContext.cleanAfterSample();
                // - or the last sample failed AND the onErrorStartNextLoop option is enabled
                if (threadContext.isRestartNextLoop() || (onErrorStartNextLoop && !TRUE.equals(threadContext.getVariables().get(LAST_SAMPLE_OK)))) {
                    if (log.isDebugEnabled() && onErrorStartNextLoop && !threadContext.isRestartNextLoop()) {
                        log.debug("StartNextLoop option is on, Last sample failed, starting next loop");
                    }
                    triggerEndOfLoopOnParentControllers(sam, threadContext);
                    sam = null;
                    threadContext.getVariables().put(LAST_SAMPLE_OK, TRUE);
                    threadContext.setRestartNextLoop(false);
                } else {
                    sam = threadGroupLoopController.next();
                }
            }
            if (threadGroupLoopController.isDone()) {
                running = false;
                log.info("Thread is done: {}", threadName);
            }
        }
    }// Might be found by controller.next()
     catch (JMeterStopTestException e) {
        // NOSONAR
        if (log.isInfoEnabled()) {
            log.info("Stopping Test: {}", e.toString());
        }
        shutdownTest();
    } catch (JMeterStopTestNowException e) {
        // NOSONAR
        if (log.isInfoEnabled()) {
            log.info("Stopping Test Now: {}", e.toString());
        }
        stopTestNow();
    } catch (JMeterStopThreadException e) {
        // NOSONAR
        if (log.isInfoEnabled()) {
            log.info("Stop Thread seen for thread {}, reason: {}", getThreadName(), e.toString());
        }
    } catch (Exception | JMeterError e) {
        log.error("Test failed!", e);
    } catch (ThreadDeath e) {
        // Must not ignore this one
        throw e;
    } finally {
        // prevent any further interrupts
        currentSampler = null;
        try {
            // make sure current interrupt is finished, prevent another starting yet
            interruptLock.lock();
            threadContext.clear();
            log.info("Thread finished: {}", threadName);
            threadFinished(iterationListener);
            // Tell the monitor we are done
            monitor.threadFinished(this);
            // Remove the ThreadLocal entry
            JMeterContextService.removeContext();
        } finally {
            // Allow any pending interrupt to complete (OK because currentSampler == null)
            interruptLock.unlock();
        }
    }
}
Also used : JMeterStopTestNowException(org.apache.jorphan.util.JMeterStopTestNowException) JMeterStopThreadException(org.apache.jorphan.util.JMeterStopThreadException) JMeterStopTestException(org.apache.jorphan.util.JMeterStopTestException) LoopIterationListener(org.apache.jmeter.engine.event.LoopIterationListener) JMeterError(org.apache.jorphan.util.JMeterError) TransactionSampler(org.apache.jmeter.control.TransactionSampler) Sampler(org.apache.jmeter.samplers.Sampler) JMeterStopTestException(org.apache.jorphan.util.JMeterStopTestException) JMeterStopThreadException(org.apache.jorphan.util.JMeterStopThreadException) JMeterStopTestNowException(org.apache.jorphan.util.JMeterStopTestNowException)

Example 28 with Sampler

use of org.apache.jmeter.samplers.Sampler 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());
    Sampler sampler = pack.getSampler();
    sampler.setThreadContext(threadContext);
    // TODO should this set the thread names for all the subsamples?
    // might be more efficient than fetching the name elsewhere
    sampler.setThreadName(threadName);
    TestBeanHelper.prepare(sampler);
    // Perform the actual sample
    currentSampler = sampler;
    if (!sampleMonitors.isEmpty()) {
        for (SampleMonitor sampleMonitor : sampleMonitors) {
            sampleMonitor.sampleStarting(sampler);
        }
    }
    SampleResult result = null;
    try {
        result = sampler.sample(null);
    } finally {
        if (!sampleMonitors.isEmpty()) {
            for (SampleMonitor sampleMonitor : sampleMonitors) {
                sampleMonitor.sampleEnded(sampler);
            }
        }
    }
    currentSampler = null;
    // If we got any results, then perform processing on the result
    if (result != null) {
        int nbActiveThreadsInThreadGroup = threadGroup.getNumberOfThreads();
        int nbTotalActiveThreads = JMeterContextService.getNumberOfThreads();
        result.setGroupThreads(nbActiveThreadsInThreadGroup);
        result.setAllThreads(nbTotalActiveThreads);
        result.setThreadName(threadName);
        SampleResult[] subResults = result.getSubResults();
        if (subResults != null) {
            for (SampleResult subResult : subResults) {
                subResult.setGroupThreads(nbActiveThreadsInThreadGroup);
                subResult.setAllThreads(nbTotalActiveThreads);
                subResult.setThreadName(threadName);
            }
        }
        threadContext.setPreviousResult(result);
        runPostProcessors(pack.getPostProcessors());
        checkAssertions(pack.getAssertions(), result, threadContext);
        // 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) {
            transactionSampler.addSubSamplerResult(result);
        }
        // Check if thread or test should be stopped
        if (result.isStopThread() || (!result.isSuccessful() && onErrorStopThread)) {
            if (transactionSampler != null) {
                doEndTransactionSampler(transactionSampler, current, transactionPack, threadContext);
            }
            stopThread();
        }
        if (result.isStopTest() || (!result.isSuccessful() && onErrorStopTest)) {
            if (transactionSampler != null) {
                doEndTransactionSampler(transactionSampler, current, transactionPack, threadContext);
            }
            shutdownTest();
        }
        if (result.isStopTestNow() || (!result.isSuccessful() && onErrorStopTestNow)) {
            if (transactionSampler != null) {
                doEndTransactionSampler(transactionSampler, current, transactionPack, threadContext);
            }
            stopTestNow();
        }
        if (result.isStartNextThreadLoop()) {
            threadContext.setRestartNextLoop(true);
        }
    } 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) SampleMonitor(org.apache.jmeter.samplers.SampleMonitor) SampleListener(org.apache.jmeter.samplers.SampleListener)

Example 29 with Sampler

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

the class JMeterThread method processSampler.

/**
     * Process the current sampler, handling transaction samplers.
     *
     * @param current sampler
     * @param parent sampler
     * @param threadContext
     * @return SampleResult if a transaction was processed
     */
private SampleResult processSampler(Sampler current, Sampler parent, JMeterContext threadContext) {
    SampleResult transactionResult = null;
    try {
        // Check if we are running a transaction
        TransactionSampler transactionSampler = null;
        if (current instanceof TransactionSampler) {
            transactionSampler = (TransactionSampler) current;
        }
        // Find the package for the transaction
        SamplePackage transactionPack = null;
        if (transactionSampler != null) {
            transactionPack = compiler.configureTransactionSampler(transactionSampler);
            // Check if the transaction is done
            if (transactionSampler.isTransactionDone()) {
                transactionResult = doEndTransactionSampler(transactionSampler, parent, transactionPack, threadContext);
                // Transaction is done, we do not have a sampler to sample
                current = null;
            } else {
                Sampler prev = current;
                // It is the sub sampler of the transaction that will be sampled
                current = transactionSampler.getSubSampler();
                if (current instanceof TransactionSampler) {
                    // recursive call
                    SampleResult res = processSampler(current, prev, threadContext);
                    threadContext.setCurrentSampler(prev);
                    current = null;
                    if (res != null) {
                        transactionSampler.addSubSamplerResult(res);
                    }
                }
            }
        }
        // Check if we have a sampler to sample
        if (current != null) {
            executeSamplePackage(current, transactionSampler, transactionPack, threadContext);
        }
        if (scheduler) {
            // checks the scheduler to stop the iteration
            stopSchedulerIfNeeded();
        }
    } catch (JMeterStopTestException e) {
        // NOSONAR
        if (log.isInfoEnabled()) {
            log.info("Stopping Test: {}", e.toString());
        }
        if (current != null && current instanceof TransactionSampler) {
            doEndTransactionSampler((TransactionSampler) current, parent, compiler.configureTransactionSampler((TransactionSampler) current), threadContext);
        }
        shutdownTest();
    } catch (JMeterStopTestNowException e) {
        // NOSONAR
        if (log.isInfoEnabled()) {
            log.info("Stopping Test with interruption of current samplers: {}", e.toString());
        }
        if (current != null && current instanceof TransactionSampler) {
            doEndTransactionSampler((TransactionSampler) current, parent, compiler.configureTransactionSampler((TransactionSampler) current), threadContext);
        }
        stopTestNow();
    } catch (JMeterStopThreadException e) {
        // NOSONAR
        if (log.isInfoEnabled()) {
            log.info("Stopping Thread: {}", e.toString());
        }
        if (current != null && current instanceof TransactionSampler) {
            doEndTransactionSampler((TransactionSampler) current, parent, compiler.configureTransactionSampler((TransactionSampler) current), threadContext);
        }
        stopThread();
    } catch (Exception e) {
        if (current != null) {
            log.error("Error while processing sampler: '{}'.", current.getName(), e);
        } else {
            log.error("Error while processing sampler.", e);
        }
    }
    return transactionResult;
}
Also used : JMeterStopTestNowException(org.apache.jorphan.util.JMeterStopTestNowException) JMeterStopThreadException(org.apache.jorphan.util.JMeterStopThreadException) JMeterStopTestException(org.apache.jorphan.util.JMeterStopTestException) TransactionSampler(org.apache.jmeter.control.TransactionSampler) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) TransactionSampler(org.apache.jmeter.control.TransactionSampler) JMeterStopTestException(org.apache.jorphan.util.JMeterStopTestException) JMeterStopThreadException(org.apache.jorphan.util.JMeterStopThreadException) JMeterStopTestNowException(org.apache.jorphan.util.JMeterStopTestNowException)

Example 30 with Sampler

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

the class JSR223TestElement method populateBindings.

/**
     * Populate variables to be passed to scripts
     * @param bindings Bindings
     */
protected void populateBindings(Bindings bindings) {
    final String label = getName();
    final String fileName = getFilename();
    final String scriptParameters = getParameters();
    // Use actual class name for log
    final Logger logger = LoggerFactory.getLogger(getClass());
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("log", logger);
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("Label", label);
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("FileName", fileName);
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("Parameters", scriptParameters);
    //$NON-NLS-1$
    String[] args = JOrphanUtils.split(scriptParameters, " ");
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("args", args);
    // Add variables for access to context and variables
    JMeterContext jmctx = JMeterContextService.getContext();
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("ctx", jmctx);
    JMeterVariables vars = jmctx.getVariables();
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("vars", vars);
    Properties props = JMeterUtils.getJMeterProperties();
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("props", props);
    // For use in debugging:
    // NOSONAR $NON-NLS-1$ (this name is fixed)
    bindings.put("OUT", System.out);
    // Most subclasses will need these:
    Sampler sampler = jmctx.getCurrentSampler();
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("sampler", sampler);
    SampleResult prev = jmctx.getPreviousResult();
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("prev", prev);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) Logger(org.slf4j.Logger) Properties(java.util.Properties)

Aggregations

Sampler (org.apache.jmeter.samplers.Sampler)33 SampleResult (org.apache.jmeter.samplers.SampleResult)11 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)9 JMeterContext (org.apache.jmeter.threads.JMeterContext)9 Test (org.junit.Test)9 TransactionSampler (org.apache.jmeter.control.TransactionSampler)6 DebugSampler (org.apache.jmeter.sampler.DebugSampler)6 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)5 Controller (org.apache.jmeter.control.Controller)3 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterStopTestException (org.apache.jorphan.util.JMeterStopTestException)3 JMeterStopTestNowException (org.apache.jorphan.util.JMeterStopTestNowException)3 JMeterStopThreadException (org.apache.jorphan.util.JMeterStopThreadException)3 Logger (org.slf4j.Logger)3 Properties (java.util.Properties)2 Argument (org.apache.jmeter.config.Argument)2 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)2 TestElement (org.apache.jmeter.testelement.TestElement)2 Cache (com.github.benmanes.caffeine.cache.Cache)1