Search in sources :

Example 36 with JMeterContext

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

the class Jexl2Function method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    //$NON-NLS-1$
    String str = "";
    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();
    //$NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    try {
        JexlContext jc = new MapContext();
        //$NON-NLS-1$
        jc.set("log", log);
        //$NON-NLS-1$
        jc.set("ctx", jmctx);
        //$NON-NLS-1$
        jc.set("vars", vars);
        //$NON-NLS-1$
        jc.set("props", JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        //$NON-NLS-1$
        jc.set("threadName", Thread.currentThread().getName());
        //$NON-NLS-1$ (may be null)
        jc.set("sampler", currentSampler);
        //$NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult);
        //$NON-NLS-1$
        jc.set("OUT", System.out);
        // Now evaluate the script, getting the result
        Expression e = getJexlEngine().createExpression(exp);
        Object o = e.evaluate(jc);
        if (o != null) {
            str = o.toString();
        }
        if (vars != null && varName.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varName, str);
        }
    } catch (Exception e) {
        log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
    }
    return str;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Example 37 with JMeterContext

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

the class Jexl3Function method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    //$NON-NLS-1$
    String str = "";
    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();
    //$NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    try {
        JexlContext jc = new MapContext();
        //$NON-NLS-1$
        jc.set("log", log);
        //$NON-NLS-1$
        jc.set("ctx", jmctx);
        //$NON-NLS-1$
        jc.set("vars", vars);
        //$NON-NLS-1$
        jc.set("props", JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        //$NON-NLS-1$
        jc.set("threadName", Thread.currentThread().getName());
        //$NON-NLS-1$ (may be null)
        jc.set("sampler", currentSampler);
        //$NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult);
        //$NON-NLS-1$
        jc.set("OUT", System.out);
        // Now evaluate the script, getting the result
        JexlExpression e = getJexlEngine().createExpression(exp);
        Object o = e.evaluate(jc);
        if (o != null) {
            str = o.toString();
        }
        if (vars != null && varName.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varName, str);
        }
    } catch (Exception e) {
        log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
    }
    return str;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 38 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext 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)

Example 39 with JMeterContext

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

the class BeanShellTestElement method getBeanShellInterpreter.

/**
     * Get the interpreter and set up standard script variables.
     * <p>
     * Sets the following script variables:
     * <ul>
     * <li>ctx</li>
     * <li>Label</li>
     * <li>prev</li>
     * <li>props</li>
     * <li>vars</li>
     * </ul>
     * @return the interpreter
     */
protected BeanShellInterpreter getBeanShellInterpreter() {
    if (isResetInterpreter()) {
        try {
            bshInterpreter.reset();
        } catch (ClassNotFoundException e) {
            log.error("Cannot reset BeanShell: " + e.toString());
        }
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    try {
        //$NON-NLS-1$
        bshInterpreter.set("ctx", jmctx);
        //$NON-NLS-1$
        bshInterpreter.set("Label", getName());
        //$NON-NLS-1$
        bshInterpreter.set("prev", jmctx.getPreviousResult());
        bshInterpreter.set("props", JMeterUtils.getJMeterProperties());
        //$NON-NLS-1$
        bshInterpreter.set("vars", vars);
    } catch (JMeterException e) {
        log.warn("Problem setting one or more BeanShell variables " + e);
    }
    return bshInterpreter;
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterException(org.apache.jorphan.util.JMeterException) JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 40 with JMeterContext

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

the class FunctionProperty method getStringValue.

/**
     * Executes the function (and caches the value for the duration of the test
     * iteration) if the property is a running version. Otherwise, the raw
     * string representation of the function is provided.
     *
     * @see JMeterProperty#getStringValue()
     */
@Override
public String getStringValue() {
    // Expensive, so
    JMeterContext ctx = JMeterContextService.getContext();
    // once
    if (!isRunningVersion()) /*|| !ctx.isSamplingStarted()*/
    {
        log.debug("Not running version, return raw function string");
        return function.getRawParameters();
    }
    if (!ctx.isSamplingStarted()) {
        return function.execute();
    }
    log.debug("Running version, executing function");
    int iter = ctx.getVariables() != null ? ctx.getVariables().getIteration() : -1;
    if (iter < testIteration) {
        testIteration = -1;
    }
    if (iter > testIteration || cacheValue == null) {
        testIteration = iter;
        cacheValue = function.execute();
    }
    return cacheValue;
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext)

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