use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class Jexl2Function 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
Expression 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.JMeterContext 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.JMeterContext in project jmeter by apache.
the class JSR223TestElement method populateBindings.
/**
* Populate variables to be passed to scripts
* @param bindings Bindings
*/
protected void populateBindings(Bindings bindings) {
final String label = getName();
final String fileName = getFilename();
final String scriptParameters = getParameters();
// Use actual class name for log
final Logger logger = LoggerFactory.getLogger(getClass());
// $NON-NLS-1$ (this name is fixed)
bindings.put("log", logger);
// $NON-NLS-1$ (this name is fixed)
bindings.put("Label", label);
// $NON-NLS-1$ (this name is fixed)
bindings.put("FileName", fileName);
// $NON-NLS-1$ (this name is fixed)
bindings.put("Parameters", scriptParameters);
//$NON-NLS-1$
String[] args = JOrphanUtils.split(scriptParameters, " ");
// $NON-NLS-1$ (this name is fixed)
bindings.put("args", args);
// Add variables for access to context and variables
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:
// NOSONAR $NON-NLS-1$ (this name is fixed)
bindings.put("OUT", System.out);
// Most subclasses will need these:
Sampler sampler = jmctx.getCurrentSampler();
// $NON-NLS-1$ (this name is fixed)
bindings.put("sampler", sampler);
SampleResult prev = jmctx.getPreviousResult();
// $NON-NLS-1$ (this name is fixed)
bindings.put("prev", prev);
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class BeanShellTestElement method getBeanShellInterpreter.
/**
* Get the interpreter and set up standard script variables.
* <p>
* Sets the following script variables:
* <ul>
* <li>ctx</li>
* <li>Label</li>
* <li>prev</li>
* <li>props</li>
* <li>vars</li>
* </ul>
* @return the interpreter
*/
protected BeanShellInterpreter getBeanShellInterpreter() {
if (isResetInterpreter()) {
try {
bshInterpreter.reset();
} catch (ClassNotFoundException e) {
log.error("Cannot reset BeanShell: " + e.toString());
}
}
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
//$NON-NLS-1$
bshInterpreter.set("ctx", jmctx);
//$NON-NLS-1$
bshInterpreter.set("Label", getName());
//$NON-NLS-1$
bshInterpreter.set("prev", jmctx.getPreviousResult());
bshInterpreter.set("props", JMeterUtils.getJMeterProperties());
//$NON-NLS-1$
bshInterpreter.set("vars", vars);
} catch (JMeterException e) {
log.warn("Problem setting one or more BeanShell variables " + e);
}
return bshInterpreter;
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class FunctionProperty method getStringValue.
/**
* Executes the function (and caches the value for the duration of the test
* iteration) if the property is a running version. Otherwise, the raw
* string representation of the function is provided.
*
* @see JMeterProperty#getStringValue()
*/
@Override
public String getStringValue() {
// Expensive, so
JMeterContext ctx = JMeterContextService.getContext();
// once
if (!isRunningVersion()) /*|| !ctx.isSamplingStarted()*/
{
log.debug("Not running version, return raw function string");
return function.getRawParameters();
}
if (!ctx.isSamplingStarted()) {
return function.execute();
}
log.debug("Running version, executing function");
int iter = ctx.getVariables() != null ? ctx.getVariables().getIteration() : -1;
if (iter < testIteration) {
testIteration = -1;
}
if (iter > testIteration || cacheValue == null) {
testIteration = iter;
cacheValue = function.execute();
}
return cacheValue;
}
Aggregations