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
Script e = getJexlEngine().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 \"" + exp + "\"\n", e);
}
return str;
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class JSONPostProcessor method process.
@Override
public void process() {
JMeterContext context = getThreadContext();
JMeterVariables vars = context.getVariables();
List<String> jsonResponses = extractJsonResponse(context, vars);
String[] refNames = getRefNames().split(SEPARATOR);
String[] jsonPathExpressions = getJsonPathExpressions().split(SEPARATOR);
String[] defaultValues = getDefaultValues().split(SEPARATOR);
int[] matchNumbers = getMatchNumbersAsInt(defaultValues.length);
validateSameLengthOfArguments(refNames, jsonPathExpressions, defaultValues);
for (int i = 0; i < jsonPathExpressions.length; i++) {
int matchNumber = matchNumbers[i];
String currentRefName = refNames[i].trim();
String currentJsonPath = jsonPathExpressions[i].trim();
clearOldRefVars(vars, currentRefName);
try {
if (jsonResponses.isEmpty()) {
handleEmptyResponse(vars, defaultValues[i], currentRefName);
} else {
List<Object> extractedValues = extractValues(jsonResponses, currentJsonPath);
handleResult(vars, defaultValues[i], matchNumber, currentRefName, extractedValues);
}
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.error("Error processing JSON content in {}, message: {}", getName(), e.getLocalizedMessage(), e);
} else {
log.error("Error processing JSON content in {}, message: {}", getName(), e.getLocalizedMessage());
}
// if something goes wrong, add default value
vars.put(currentRefName, defaultValues[i]);
}
}
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class TestAction method sample.
/**
* {@inheritDoc}
*/
@Override
public SampleResult sample(Entry e) {
JMeterContext context = JMeterContextService.getContext();
int target = getTarget();
int action = getAction();
if (action == PAUSE) {
pause(getDurationAsString());
} else if (action == STOP || action == STOP_NOW) {
if (target == THREAD) {
if (log.isInfoEnabled()) {
log.info(MSG_STOP_CURRENT_THREAD, getName());
}
context.getThread().stop();
} else if (target == TEST) {
if (action == STOP_NOW) {
if (log.isInfoEnabled()) {
log.info(MSG_STOP_CURRENT_THREAD, getName());
}
context.getThread().stop();
if (log.isInfoEnabled()) {
log.info("Stopping all threads now from element {}", getName());
}
context.getEngine().stopTest();
} else {
if (log.isInfoEnabled()) {
log.info(MSG_STOP_CURRENT_THREAD, getName());
}
context.getThread().stop();
if (log.isInfoEnabled()) {
log.info("Stopping all threads from element {}", getName());
}
context.getEngine().askThreadsToStop();
}
}
} else if (action == RESTART_NEXT_LOOP) {
log.info("Restarting next thread loop from element {}", getName());
context.setTestLogicalAction(TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);
} else if (action == START_NEXT_ITERATION_CURRENT_LOOP) {
log.info("Switching to next loop iteration from element {}", getName());
context.setTestLogicalAction(TestLogicalAction.START_NEXT_ITERATION_OF_CURRENT_LOOP);
} else if (action == BREAK_CURRENT_LOOP) {
log.info("Breaking current loop from element {}", getName());
context.setTestLogicalAction(TestLogicalAction.BREAK_CURRENT_LOOP);
}
// This means no sample is saved
return null;
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class SplitFunctionTest method setUp.
@BeforeEach
public void setUp() {
JMeterContext jmeterContext = JMeterContextService.getContext();
jmeterContext.setVariables(new JMeterVariables());
vars = jmeterContext.getVariables();
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class TestChangeCase method setUp.
@BeforeEach
public void setUp() {
changeCase = new ChangeCase();
JMeterContext jmctx = JMeterContextService.getContext();
String data = "dummy data";
result = new SampleResult();
result.setResponseData(data, null);
JMeterVariables vars = new JMeterVariables();
jmctx.setVariables(vars);
jmctx.setPreviousResult(result);
}
Aggregations