Search in sources :

Example 76 with JMeterVariables

use of org.apache.jmeter.threads.JMeterVariables 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 77 with JMeterVariables

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

the class EscapeOroRegexpChars method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String valueToEscape = values[0].execute();
    //$NON-NLS-1$
    String varName = "";
    if (values.length >= PARAM_NAME) {
        varName = values[PARAM_NAME - 1].execute().trim();
    }
    String escapedValue = Perl5Compiler.quotemeta(valueToEscape);
    if (varName.length() > 0) {
        JMeterVariables vars = getVariables();
        if (vars != null) {
            // Can be null if called from Config item testEnded() method
            vars.put(varName, escapedValue);
        }
    }
    if (log.isDebugEnabled()) {
        String tn = Thread.currentThread().getName();
        log.debug(//$NON-NLS-1$
        tn + " name:" + varName + " value:" + //$NON-NLS-1$
        escapedValue);
    }
    return escapedValue;
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 78 with JMeterVariables

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

the class LongSum method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    long sum = 0;
    String varName = ((CompoundVariable) values[values.length - 1]).execute().trim();
    for (int i = 0; i < values.length - 1; i++) {
        sum += Long.parseLong(((CompoundVariable) values[i]).execute());
    }
    try {
        // Has chances to be a var
        sum += Long.parseLong(varName);
        // there is no variable name
        varName = null;
    } catch (NumberFormatException ignored) {
    // varName keeps its value and sum has not taken 
    // into account non numeric or overflowing number
    }
    String totalString = Long.toString(sum);
    if (vars != null && varName != null && varName.length() > 0) {
        // vars will be null on TestPlan
        vars.put(varName, totalString);
    }
    return totalString;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 79 with JMeterVariables

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

the class Property method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String propertyName = ((CompoundVariable) values[0]).execute();
    String propertyDefault = propertyName;
    if (values.length > 2) {
        // We have a 3rd parameter
        propertyDefault = ((CompoundVariable) values[2]).execute();
    }
    String propertyValue = JMeterUtils.getPropDefault(propertyName, propertyDefault);
    if (values.length > 1) {
        String variableName = ((CompoundVariable) values[1]).execute();
        if (variableName.length() > 0) {
            // Allow for empty name
            final JMeterVariables variables = getVariables();
            if (variables != null) {
                variables.put(variableName, propertyValue);
            }
        }
    }
    return propertyValue;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 80 with JMeterVariables

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

the class Random method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    long min = Long.parseLong(minimum.execute().trim());
    long max = Long.parseLong(maximum.execute().trim());
    long rand = ThreadLocalRandom.current().nextLong(min, max + 1);
    String randString = Long.toString(rand);
    if (varName != null) {
        JMeterVariables vars = getVariables();
        final String varTrim = varName.execute().trim();
        if (vars != null && varTrim.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varTrim, randString);
        }
    }
    return randString;
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Aggregations

JMeterVariables (org.apache.jmeter.threads.JMeterVariables)96 SampleResult (org.apache.jmeter.samplers.SampleResult)35 JMeterContext (org.apache.jmeter.threads.JMeterContext)33 Before (org.junit.Before)33 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)17 Test (org.junit.Test)16 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)8 TestPlan (org.apache.jmeter.testelement.TestPlan)6 HashMap (java.util.HashMap)5 Sampler (org.apache.jmeter.samplers.Sampler)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ValueReplacer (org.apache.jmeter.engine.util.ValueReplacer)4 Properties (java.util.Properties)3 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)3 Logger (org.slf4j.Logger)3 URL (java.net.URL)2 Arguments (org.apache.jmeter.config.Arguments)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)2