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;
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class CookieManager method add.
/**
* Add a cookie.
*
* @param c cookie to be added
*/
public void add(Cookie c) {
String cv = c.getValue();
String cn = c.getName();
// Can't have two matching cookies
removeMatchingCookies(c);
if (DELETE_NULL_COOKIES && (null == cv || cv.length() == 0)) {
if (log.isDebugEnabled()) {
log.debug("Dropping cookie with null value {}", c.toString());
}
} else {
if (log.isDebugEnabled()) {
log.debug("Add cookie to store {}", c.toString());
}
getCookies().addItem(c);
if (SAVE_COOKIES) {
JMeterContext context = getThreadContext();
if (context.isSamplingStarted()) {
context.getVariables().put(COOKIE_NAME_PREFIX + cn, cv);
}
}
}
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class TestSwitchController method testFunction.
/*
* N.B. Requires ApacheJMeter_functions.jar to be on the classpath,
* otherwise the function cannot be resolved.
*/
@Test
public void testFunction() throws Exception {
JMeterContext jmctx = JMeterContextService.getContext();
Map<String, String> variables = new HashMap<>();
ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
jmctx.setVariables(new JMeterVariables());
JMeterVariables jmvars = jmctx.getVariables();
jmvars.put("VAR", "100");
StringProperty prop = new StringProperty(SwitchController.SWITCH_VALUE, "${__counter(TRUE,VAR)}");
JMeterProperty newProp = transformer.transformValue(prop);
newProp.setRunningVersion(true);
GenericController controller = new GenericController();
SwitchController switch_cont = new SwitchController();
switch_cont.setProperty(newProp);
controller.addTestElement(new TestSampler("before"));
controller.addTestElement(switch_cont);
switch_cont.addTestElement(new TestSampler("0"));
switch_cont.addTestElement(new TestSampler("1"));
switch_cont.addTestElement(new TestSampler("2"));
switch_cont.addTestElement(new TestSampler("3"));
controller.addTestElement(new TestSampler("after"));
controller.initialize();
assertEquals("100", jmvars.get("VAR"));
for (int i = 1; i <= 3; i++) {
assertEquals("Loop " + i, "before", nextName(controller));
assertEquals("Loop " + i, "" + i, nextName(controller));
assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
assertEquals("Loop " + i, "after", nextName(controller));
assertNull(nextName(controller));
}
int i = 4;
assertEquals("Loop " + i, "before", nextName(controller));
assertEquals("Loop " + i, "0", nextName(controller));
assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
assertEquals("Loop " + i, "after", nextName(controller));
assertNull(nextName(controller));
assertEquals("4", jmvars.get("VAR"));
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class JavaScript method executeWithRhino.
/**
* @param previousResult {@link SampleResult}
* @param currentSampler {@link Sampler}
* @param jmctx {@link JMeterContext}
* @param vars {@link JMeterVariables}
* @param script Javascript code
* @param varName variable name
* @return result as String
* @throws InvalidVariableException
*/
private String executeWithRhino(SampleResult previousResult, Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars, String script, String varName) throws InvalidVariableException {
Context cx = Context.enter();
String resultStr = null;
try {
Scriptable scope = cx.initStandardObjects(null);
// Set up some objects for the script to play with
// $NON-NLS-1$
scope.put("log", scope, log);
// $NON-NLS-1$
scope.put("ctx", scope, jmctx);
// $NON-NLS-1$
scope.put("vars", scope, vars);
// $NON-NLS-1$
scope.put("props", scope, JMeterUtils.getJMeterProperties());
// Previously mis-spelt as theadName
// $NON-NLS-1$
scope.put("threadName", scope, Thread.currentThread().getName());
// $NON-NLS-1$
scope.put("sampler", scope, currentSampler);
// $NON-NLS-1$
scope.put("sampleResult", scope, previousResult);
// $NON-NLS-1$
Object result = cx.evaluateString(scope, script, "<cmd>", 1, null);
resultStr = Context.toString(result);
if (varName != null && vars != null) {
// vars can be null if run from TestPlan
vars.put(varName, resultStr);
}
} catch (RhinoException e) {
log.error("Error processing Javascript: [{}]", script, e);
throw new InvalidVariableException("Error processing Javascript: [" + script + "]", e);
} finally {
Context.exit();
}
return resultStr;
}
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
JexlScript e = threadLocalJexl.get().createScript(exp);
Object o = e.execute(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 \"{}\"\n", exp, e);
}
return str;
}
Aggregations