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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations