Search in sources :

Example 1 with JMeterStopThreadException

use of org.apache.jorphan.util.JMeterStopThreadException in project jmeter-plugins by undera.

the class RedisDataSet method iterationStart.

@Override
public void iterationStart(LoopIterationEvent event) {
    Jedis connection = null;
    try {
        connection = pool.getResource();
        String line = null;
        if (getMode.equals(GetMode.RANDOM_REMOVE)) {
            line = connection.lpop(redisKey);
        } else {
            line = connection.srandmember(redisKey);
        }
        if (line == null) {
            // i.e. no more data (nil)
            throw new JMeterStopThreadException("End of redis data detected, thread will exit");
        }
        final String names = variableNames;
        if (vars == null) {
            vars = JOrphanUtils.split(names, ",");
        }
        final JMeterContext context = getThreadContext();
        JMeterVariables threadVars = context.getVariables();
        String[] values = JOrphanUtils.split(line, delimiter, false);
        for (int a = 0; a < vars.length && a < values.length; a++) {
            threadVars.put(vars[a], values[a]);
        }
    } finally {
        pool.returnResource(connection);
    }
}
Also used : JMeterStopThreadException(org.apache.jorphan.util.JMeterStopThreadException) Jedis(redis.clients.jedis.Jedis) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 2 with JMeterStopThreadException

use of org.apache.jorphan.util.JMeterStopThreadException 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;
    // Check if we are running a transaction
    TransactionSampler transactionSampler = null;
    // Find the package for the transaction
    SamplePackage transactionPack = null;
    try {
        if (current instanceof TransactionSampler) {
            transactionSampler = (TransactionSampler) current;
            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());
        }
        shutdownTest();
    } catch (JMeterStopTestNowException e) {
        // NOSONAR
        if (log.isInfoEnabled()) {
            log.info("Stopping Test with interruption of current samplers: {}", e.toString());
        }
        stopTestNow();
    } catch (JMeterStopThreadException e) {
        // NOSONAR
        if (log.isInfoEnabled()) {
            log.info("Stopping Thread: {}", e.toString());
        }
        stopThread();
    } catch (Exception e) {
        if (current != null) {
            log.error("Error while processing sampler: '{}'.", current.getName(), e);
        } else {
            log.error("Error while processing sampler.", e);
        }
    }
    if (!running && transactionResult == null && transactionSampler != null && transactionPack != null) {
        transactionResult = doEndTransactionSampler(transactionSampler, parent, transactionPack, threadContext);
    }
    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 3 with JMeterStopThreadException

use of org.apache.jorphan.util.JMeterStopThreadException 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();
                boolean lastSampleOk = TRUE.equals(threadContext.getVariables().get(LAST_SAMPLE_OK));
                // - or the last sample failed AND the onErrorStartNextLoop option is enabled
                if (threadContext.getTestLogicalAction() != TestLogicalAction.CONTINUE || (onErrorStartNextLoop && !lastSampleOk)) {
                    if (log.isDebugEnabled() && onErrorStartNextLoop && threadContext.getTestLogicalAction() != TestLogicalAction.CONTINUE) {
                        log.debug("Start Next Thread Loop option is on, Last sample failed, starting next thread loop");
                    }
                    if (onErrorStartNextLoop && !lastSampleOk) {
                        triggerLoopLogicalActionOnParentControllers(sam, threadContext, JMeterThread::continueOnThreadLoop);
                    } else {
                        switch(threadContext.getTestLogicalAction()) {
                            case BREAK_CURRENT_LOOP:
                                triggerLoopLogicalActionOnParentControllers(sam, threadContext, JMeterThread::breakOnCurrentLoop);
                                break;
                            case START_NEXT_ITERATION_OF_THREAD:
                                triggerLoopLogicalActionOnParentControllers(sam, threadContext, JMeterThread::continueOnThreadLoop);
                                break;
                            case START_NEXT_ITERATION_OF_CURRENT_LOOP:
                                triggerLoopLogicalActionOnParentControllers(sam, threadContext, JMeterThread::continueOnCurrentLoop);
                                break;
                            default:
                                break;
                        }
                    }
                    threadContext.setTestLogicalAction(TestLogicalAction.CONTINUE);
                    sam = null;
                    setLastSampleOk(threadContext.getVariables(), true);
                } else {
                    sam = threadGroupLoopController.next();
                }
            }
            // It would be possible to add finally for Thread Loop here
            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
        currentSamplerForInterruption = null;
        // make sure current interrupt is finished, prevent another starting yet
        interruptLock.lock();
        try {
            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 4 with JMeterStopThreadException

use of org.apache.jorphan.util.JMeterStopThreadException in project jmeter by apache.

the class CSVDataSet method iterationStart.

@Override
public void iterationStart(LoopIterationEvent iterEvent) {
    FileServer server = FileServer.getFileServer();
    final JMeterContext context = getThreadContext();
    String delim = getDelimiter();
    if ("\\t".equals(delim)) {
        // $NON-NLS-1$
        // Make it easier to enter a Tab // $NON-NLS-1$
        delim = "\t";
    } else if (delim.isEmpty()) {
        log.debug("Empty delimiter, will use ','");
        delim = ",";
    }
    if (vars == null) {
        initVars(server, context, delim);
    }
    // TODO: fetch this once as per vars above?
    JMeterVariables threadVars = context.getVariables();
    String[] lineValues = {};
    try {
        if (getQuotedData()) {
            lineValues = server.getParsedLine(alias, recycle, firstLineIsNames || ignoreFirstLine, delim.charAt(0));
        } else {
            String line = server.readLine(alias, recycle, firstLineIsNames || ignoreFirstLine);
            lineValues = JOrphanUtils.split(line, delim, false);
        }
        for (int a = 0; a < vars.length && a < lineValues.length; a++) {
            threadVars.put(vars[a], lineValues[a]);
        }
    } catch (IOException e) {
        // treat the same as EOF
        log.error(e.toString());
    }
    if (lineValues.length == 0) {
        // i.e. EOF
        if (getStopThread()) {
            throw new JMeterStopThreadException("End of file:" + getFilename() + " detected for CSV DataSet:" + getName() + " configured with stopThread:" + getStopThread() + ", recycle:" + getRecycle());
        }
        for (String var : vars) {
            threadVars.put(var, EOFVALUE);
        }
    }
}
Also used : JMeterStopThreadException(org.apache.jorphan.util.JMeterStopThreadException) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) IOException(java.io.IOException) FileServer(org.apache.jmeter.services.FileServer)

Example 5 with JMeterStopThreadException

use of org.apache.jorphan.util.JMeterStopThreadException in project jmeter by apache.

the class StringFromFile method execute.

/**
 * {@inheritDoc}
 */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String myValue = ERR_IND;
    // $NON-NLS-1$
    String myName = "StringFromFile_";
    if (values.length >= PARAM_NAME) {
        myName = ((CompoundVariable) values[PARAM_NAME - 1]).execute().trim();
    }
    /*
         * To avoid re-opening the file repeatedly after an error, only try to
         * open it in the first execute() call (It may be re=opened at EOF, but
         * that will cause at most one failure.)
         */
    if (firstTime) {
        openFile();
        firstTime = false;
    }
    if (null != myBread) {
        // Did we open the file?
        try {
            String line = myBread.readLine();
            if (line == null) {
                // EOF, re-open file
                String tn = Thread.currentThread().getName();
                // $NON-NLS-1$
                log.info("{} EOF on  file {}", tn, fileName);
                closeFile();
                openFile();
                if (myBread != null) {
                    line = myBread.readLine();
                } else {
                    line = ERR_IND;
                    if (myEnd != COUNT_UNUSED) {
                        // Are we processing a file
                        // sequence?
                        log.info("{} Detected end of sequence.", tn);
                        throw new JMeterStopThreadException("End of sequence");
                    }
                }
            }
            myValue = line;
        } catch (IOException e) {
            String tn = Thread.currentThread().getName();
            // $NON-NLS-1$
            log.error("{} error reading file {}", tn, e.toString());
        }
    } else {
        // File was not opened successfully
        if (myEnd != COUNT_UNUSED) {
            // Are we processing a file sequence?
            if (log.isInfoEnabled()) {
                log.info("{} Detected end of sequence.", Thread.currentThread().getName());
            }
            throw new JMeterStopThreadException("End of sequence");
        }
    }
    if (myName.length() > 0) {
        JMeterVariables vars = getVariables();
        if (vars != null) {
            // Can be null if called from Config item testEnded() method
            vars.put(myName, myValue);
        }
    }
    if (log.isDebugEnabled()) {
        // $NON-NLS-1$
        log.debug("{} name:{} value:{}", Thread.currentThread().getName(), myName, myValue);
    }
    return myValue;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterStopThreadException(org.apache.jorphan.util.JMeterStopThreadException) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) IOException(java.io.IOException)

Aggregations

JMeterStopThreadException (org.apache.jorphan.util.JMeterStopThreadException)5 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)3 IOException (java.io.IOException)2 TransactionSampler (org.apache.jmeter.control.TransactionSampler)2 Sampler (org.apache.jmeter.samplers.Sampler)2 JMeterContext (org.apache.jmeter.threads.JMeterContext)2 JMeterStopTestException (org.apache.jorphan.util.JMeterStopTestException)2 JMeterStopTestNowException (org.apache.jorphan.util.JMeterStopTestNowException)2 LoopIterationListener (org.apache.jmeter.engine.event.LoopIterationListener)1 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)1 SampleResult (org.apache.jmeter.samplers.SampleResult)1 FileServer (org.apache.jmeter.services.FileServer)1 JMeterError (org.apache.jorphan.util.JMeterError)1 Jedis (redis.clients.jedis.Jedis)1