Search in sources :

Example 41 with JMeterVariables

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

the class IntSum method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    int sum = 0;
    // trim() see bug 55871
    String varName = ((CompoundVariable) values[values.length - 1]).execute().trim();
    for (int i = 0; i < values.length - 1; i++) {
        sum += Integer.parseInt(((CompoundVariable) values[i]).execute());
    }
    try {
        // Has chances to be a var
        sum += Integer.parseInt(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 = Integer.toString(sum);
    if (vars != null && varName != null) {
        // vars will be null on TestPlan
        vars.put(varName.trim(), totalString);
    }
    return totalString;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 42 with JMeterVariables

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

the class IsVarDefined method execute.

@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String variableName = values[0].execute();
    JMeterVariables jMeterVariables = getVariables();
    if (jMeterVariables != null) {
        String variableValue = jMeterVariables.get(variableName);
        return Boolean.toString(variableValue != null);
    } else {
        return Boolean.FALSE.toString();
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 43 with JMeterVariables

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

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

the class RandomFromMultipleVars method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String variablesNamesSplitBySeparatorValue = variablesNamesSplitBySeparator.execute().trim();
    JMeterVariables vars = getVariables();
    String outputValue = "";
    String separator = "";
    if (vars != null) {
        // vars will be null on TestPlan
        List<String> results = new ArrayList<>();
        String[] variables = variablesNamesSplitBySeparatorValue.split(SEPARATOR);
        for (String currentVarName : variables) {
            if (!StringUtils.isEmpty(currentVarName)) {
                extractVariableValuesToList(currentVarName, vars, results);
            }
        }
        if (!results.isEmpty()) {
            int randomIndex = ThreadLocalRandom.current().nextInt(0, results.size());
            outputValue = results.get(randomIndex);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("RandomFromMultiResult didn't find <var>_matchNr in variables :'{}' using separator:'{}', will return empty value", variablesNamesSplitBySeparatorValue, separator);
            }
        }
        if (varName != null) {
            final String varTrim = varName.execute().trim();
            if (!varTrim.isEmpty()) {
                vars.put(varTrim, outputValue);
            }
        }
    }
    return outputValue;
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) ArrayList(java.util.ArrayList)

Example 45 with JMeterVariables

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

the class TimeFunction method execute.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("JdkObsolete")
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String datetime;
    if (format.length() == 0) {
        // Default to milliseconds
        datetime = Long.toString(System.currentTimeMillis());
    } else {
        // Resolve any aliases
        String fmt = aliases.get(format);
        if (fmt == null) {
            // Not found
            fmt = format;
        }
        if (DIVISOR_PATTERN.matcher(fmt).matches()) {
            // divisor is a positive number
            // should never case NFE
            long div = Long.parseLong(fmt.substring(1));
            datetime = Long.toString(System.currentTimeMillis() / div);
        } else {
            // Not synchronised, so can't be shared
            SimpleDateFormat df = new SimpleDateFormat(fmt);
            datetime = df.format(new Date());
        }
    }
    if (variable.length() > 0) {
        JMeterVariables vars = getVariables();
        if (vars != null) {
            // vars will be null on TestPlan
            vars.put(variable, datetime);
        }
    }
    return datetime;
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

JMeterVariables (org.apache.jmeter.threads.JMeterVariables)193 SampleResult (org.apache.jmeter.samplers.SampleResult)71 JMeterContext (org.apache.jmeter.threads.JMeterContext)64 BeforeEach (org.junit.jupiter.api.BeforeEach)46 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)27 Test (org.junit.jupiter.api.Test)24 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)19 Test (org.junit.Test)16 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)11 Before (org.junit.Before)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)6 TestPlan (org.apache.jmeter.testelement.TestPlan)6 HashMap (java.util.HashMap)5 ValueReplacer (org.apache.jmeter.engine.util.ValueReplacer)5 URL (java.net.URL)4 Arguments (org.apache.jmeter.config.Arguments)4 DebugSampler (org.apache.jmeter.sampler.DebugSampler)4