Search in sources :

Example 36 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable in project jmeter by apache.

the class FileToString method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String fileName = ((CompoundVariable) values[0]).execute();
    //means platform default
    String encoding = null;
    if (values.length >= ENCODING) {
        encoding = ((CompoundVariable) values[ENCODING - 1]).execute().trim();
        if (encoding.length() <= 0) {
            // empty encoding, return to platform default
            encoding = null;
        }
    }
    //$NON-NLS-1$
    String myName = "";
    if (values.length >= PARAM_NAME) {
        myName = ((CompoundVariable) values[PARAM_NAME - 1]).execute().trim();
    }
    String myValue = ERR_IND;
    try {
        File file = new File(fileName);
        if (file.exists() && file.canRead()) {
            myValue = FileUtils.readFileToString(new File(fileName), encoding);
        } else {
            log.warn("Could not read open: " + fileName + " ");
        }
    } catch (IOException e) {
        log.warn("Could not read file: " + fileName + " " + e.getMessage(), e);
    }
    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()) {
        String tn = Thread.currentThread().getName();
        log.debug(//$NON-NLS-1$
        tn + " name:" + myName + " value:" + //$NON-NLS-1$
        myValue);
    }
    return myValue;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) IOException(java.io.IOException) File(java.io.File)

Example 37 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable in project jmeter by apache.

the class Groovy method execute.

/** {@inheritDoc} */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    Bindings bindings = scriptEngine.createBindings();
    populateBindings(bindings);
    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$ 
            bindings.put("sampler", currentSampler);
        }
        if (previousResult != null) {
            //$NON-NLS-1$
            bindings.put("prev", previousResult);
        }
        // $NON-NLS-1$ (this name is fixed)
        bindings.put("log", log);
        // Add variables for access to context and variables
        bindings.put("threadName", Thread.currentThread().getName());
        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:
        // $NON-NLS-1$ (this name is fixed)
        bindings.put("OUT", System.out);
        // Execute the script
        Object out = scriptEngine.eval(script, bindings);
        if (out != null) {
            resultStr = out.toString();
        }
        if (varName.length() > 0) {
            // vars will be null on TestPlan
            if (vars != null) {
                vars.put(varName, resultStr);
            }
        }
    } catch (// Mainly for bsh.EvalError
    Exception ex) {
        log.warn("Error running groovy script", ex);
    }
    if (log.isDebugEnabled()) {
        log.debug("__groovy(" + 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) Properties(java.util.Properties) Bindings(javax.script.Bindings)

Example 38 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable 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 39 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable in project jmeter by apache.

the class IterationCounter method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    boolean perThread = Boolean.parseBoolean(((CompoundVariable) variables[0]).execute());
    //$NON-NLS-1$
    String varName = "";
    if (variables.length >= 2) {
        // Ensure variable has been provided
        varName = ((CompoundVariable) variables[1]).execute().trim();
    }
    //$NON-NLS-1$
    String counterString = "";
    if (perThread) {
        int threadCounter;
        threadCounter = perThreadInt.get().intValue() + 1;
        perThreadInt.set(Integer.valueOf(threadCounter));
        counterString = String.valueOf(threadCounter);
    } else {
        synchronized (this) {
            globalCounter++;
            counterString = String.valueOf(globalCounter);
        }
    }
    // vars will be null on Test Plan
    if (vars != null && varName.length() > 0) {
        vars.put(varName, counterString);
    }
    return counterString;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 40 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable 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)

Aggregations

CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)103 Test (org.junit.Test)77 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)16 LinkedList (java.util.LinkedList)9 JMeterContext (org.apache.jmeter.threads.JMeterContext)7 File (java.io.File)4 IOException (java.io.IOException)3 LocalDateTime (java.time.LocalDateTime)3 HashMap (java.util.HashMap)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 InvalidVariableException (org.apache.jmeter.functions.InvalidVariableException)2 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)2 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 JMeterStopThreadException (org.apache.jorphan.util.JMeterStopThreadException)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 DecimalFormat (java.text.DecimalFormat)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1