use of org.apache.jmeter.threads.JMeterVariables in project jmeter by apache.
the class AbstractJDBCTestElement method getStringFromResultSet.
/**
* Gets a Data object from a ResultSet.
*
* @param rs
* ResultSet passed in from a database query
* @return a Data object
* @throws java.sql.SQLException
* @throws UnsupportedEncodingException
*/
private String getStringFromResultSet(ResultSet rs) throws SQLException, UnsupportedEncodingException {
ResultSetMetaData meta = rs.getMetaData();
StringBuilder sb = new StringBuilder();
int numColumns = meta.getColumnCount();
for (int i = 1; i <= numColumns; i++) {
sb.append(meta.getColumnLabel(i));
if (i == numColumns) {
sb.append('\n');
} else {
sb.append('\t');
}
}
JMeterVariables jmvars = getThreadContext().getVariables();
String[] varNames = getVariableNames().split(COMMA);
String resultVariable = getResultVariable().trim();
List<Map<String, Object>> results = null;
if (resultVariable.length() > 0) {
results = new ArrayList<>();
jmvars.putObject(resultVariable, results);
}
int j = 0;
while (rs.next()) {
Map<String, Object> row = null;
j++;
for (int i = 1; i <= numColumns; i++) {
Object o = rs.getObject(i);
if (results != null) {
if (row == null) {
row = new HashMap<>(numColumns);
results.add(row);
}
row.put(meta.getColumnLabel(i), o);
}
if (o instanceof byte[]) {
o = new String((byte[]) o, ENCODING);
}
sb.append(o);
if (i == numColumns) {
sb.append('\n');
} else {
sb.append('\t');
}
if (i <= varNames.length) {
// i starts at 1
String name = varNames[i - 1].trim();
if (name.length() > 0) {
// Save the value in the variable if present
jmvars.put(name + UNDERSCORE + j, o == null ? null : o.toString());
}
}
}
}
// Remove any additional values from previous sample
for (String varName : varNames) {
String name = varName.trim();
if (name.length() > 0 && jmvars != null) {
// $NON-NLS-1$
final String varCount = name + "_#";
// Get the previous count
String prevCount = jmvars.get(varCount);
if (prevCount != null) {
int prev = Integer.parseInt(prevCount);
for (int n = j + 1; n <= prev; n++) {
jmvars.remove(name + UNDERSCORE + n);
}
}
// save the current count
jmvars.put(varCount, Integer.toString(j));
}
}
return sb.toString();
}
use of org.apache.jmeter.threads.JMeterVariables in project jmeter by apache.
the class PreCompiler method addNode.
/** {@inheritDoc} */
@Override
public void addNode(Object node, HashTree subTree) {
if (isRemote && node instanceof ResultCollector) {
try {
replacer.replaceValues((TestElement) node);
} catch (InvalidVariableException e) {
log.error("invalid variables", e);
}
}
if (isRemote) {
return;
}
if (node instanceof TestElement) {
try {
replacer.replaceValues((TestElement) node);
} catch (InvalidVariableException e) {
log.error("invalid variables", e);
}
}
if (node instanceof TestPlan) {
//A hack to make user-defined variables in the testplan element more dynamic
((TestPlan) node).prepareForPreCompile();
Map<String, String> args = ((TestPlan) node).getUserDefinedVariables();
replacer.setUserDefinedVariables(args);
JMeterVariables vars = new JMeterVariables();
vars.putAll(args);
JMeterContextService.getContext().setVariables(vars);
}
if (node instanceof Arguments) {
((Arguments) node).setRunningVersion(true);
Map<String, String> args = ((Arguments) node).getArgumentsAsMap();
replacer.addVariables(args);
JMeterContextService.getContext().getVariables().putAll(args);
}
}
use of org.apache.jmeter.threads.JMeterVariables in project jmeter by apache.
the class WhileController method endOfLoop.
/*
* Evaluate the condition, which can be:
* blank or LAST = was the last sampler OK?
* otherwise, evaluate the condition to see if it is not "false"
* If blank, only evaluate at the end of the loop
*
* Must only be called at start and end of loop
*
* @param loopEnd - are we at loop end?
* @return true means OK to continue
*/
private boolean endOfLoop(boolean loopEnd) {
String cnd = getCondition().trim();
log.debug("Condition string: '{}'", cnd);
boolean res;
// If blank, only check previous sample when at end of loop
if ((loopEnd && cnd.length() == 0) || "LAST".equalsIgnoreCase(cnd)) {
// $NON-NLS-1$
JMeterVariables threadVars = JMeterContextService.getContext().getVariables();
// $NON-NLS-1$
res = "false".equalsIgnoreCase(threadVars.get(JMeterThread.LAST_SAMPLE_OK));
} else {
// cnd may be null if next() called us
// $NON-NLS-1$
res = "false".equalsIgnoreCase(cnd);
}
log.debug("Condition value: '{}'", res);
return res;
}
use of org.apache.jmeter.threads.JMeterVariables 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.JMeterVariables 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;
}
Aggregations