Search in sources :

Example 6 with JMeterContext

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

the class JavaScript method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    String script = ((CompoundVariable) values[0]).execute();
    // Allow variable to be omitted
    String varName = values.length < 2 ? null : ((CompoundVariable) values[1]).execute().trim();
    String resultStr = "";
    if (useRhinoEngine) {
        resultStr = executeWithRhino(previousResult, currentSampler, jmctx, vars, script, varName);
    } else {
        resultStr = executeWithNashorn(previousResult, currentSampler, jmctx, vars, script, varName);
    }
    return resultStr;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 7 with JMeterContext

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

the class BeanShell method execute.

/** {@inheritDoc} */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    if (// did we find BeanShell?
    bshInterpreter == null) {
        throw new InvalidVariableException("BeanShell not found");
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    String script = ((CompoundVariable) values[0]).execute();
    //$NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    //$NON-NLS-1$
    String resultStr = "";
    try {
        // Pass in some variables
        if (currentSampler != null) {
            //$NON-NLS-1$
            bshInterpreter.set("Sampler", currentSampler);
        }
        if (previousResult != null) {
            //$NON-NLS-1$
            bshInterpreter.set("SampleResult", previousResult);
        }
        // Allow access to context and variables directly
        //$NON-NLS-1$
        bshInterpreter.set("ctx", jmctx);
        //$NON-NLS-1$
        bshInterpreter.set("vars", vars);
        //$NON-NLS-1$
        bshInterpreter.set("props", JMeterUtils.getJMeterProperties());
        //$NON-NLS-1$
        bshInterpreter.set("threadName", Thread.currentThread().getName());
        // Execute the script
        Object bshOut = bshInterpreter.eval(script);
        if (bshOut != null) {
            resultStr = bshOut.toString();
        }
        if (vars != null && varName.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varName, resultStr);
        }
    } catch (// Mainly for bsh.EvalError
    Exception ex) {
        log.warn("Error running BSH script", ex);
    }
    if (log.isDebugEnabled()) {
        log.debug("__Beanshell(" + script + "," + varName + ")=" + resultStr);
    }
    return resultStr;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 8 with JMeterContext

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

the class TransactionController method notifyListeners.

/**
     * Create additional SampleEvent in NON Parent Mode
     */
protected void notifyListeners() {
    // TODO could these be done earlier (or just once?)
    JMeterContext threadContext = getThreadContext();
    JMeterVariables threadVars = threadContext.getVariables();
    SamplePackage pack = (SamplePackage) threadVars.getObject(JMeterThread.PACKAGE_OBJECT);
    if (pack == null) {
        // If child of TransactionController is a ThroughputController and TPC does
        // not sample its children, then we will have this
        // TODO Should this be at warn level ?
        log.warn("Could not fetch SamplePackage");
    } else {
        SampleEvent event = new SampleEvent(res, threadContext.getThreadGroup().getName(), threadVars, true);
        // We must set res to null now, before sending the event for the transaction,
        // so that we can ignore that event in our sampleOccured method
        res = null;
        lnf.notifyListeners(event, pack.getSampleListeners());
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) SamplePackage(org.apache.jmeter.threads.SamplePackage) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleEvent(org.apache.jmeter.samplers.SampleEvent)

Example 9 with JMeterContext

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

the class CompoundVariable method execute.

public String execute() {
    if (isDynamic || permanentResults == null) {
        JMeterContext context = JMeterContextService.getContext();
        SampleResult previousResult = context.getPreviousResult();
        Sampler currentSampler = context.getCurrentSampler();
        return execute(previousResult, currentSampler);
    }
    // $NON-NLS-1$
    return permanentResults;
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult)

Example 10 with JMeterContext

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

the class AbstractFunction method execute.

public String execute() throws InvalidVariableException {
    JMeterContext context = JMeterContextService.getContext();
    SampleResult previousResult = context.getPreviousResult();
    Sampler currentSampler = context.getCurrentSampler();
    return execute(previousResult, currentSampler);
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult)

Aggregations

JMeterContext (org.apache.jmeter.threads.JMeterContext)47 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)33 SampleResult (org.apache.jmeter.samplers.SampleResult)21 Test (org.junit.Test)11 Sampler (org.apache.jmeter.samplers.Sampler)9 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)8 Before (org.junit.Before)8 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)7 Properties (java.util.Properties)3 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterException (org.apache.jorphan.util.JMeterException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 BeanShellInterpreter (org.apache.jmeter.util.BeanShellInterpreter)2 MatchResult (org.apache.oro.text.regex.MatchResult)2