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