use of org.apache.jmeter.control.TransactionSampler in project jmeter by apache.
the class TestCompiler method saveTransactionControllerConfigs.
private void saveTransactionControllerConfigs(TransactionController tc) {
List<ConfigTestElement> configs = new LinkedList<>();
List<Controller> controllers = new LinkedList<>();
List<SampleListener> listeners = new LinkedList<>();
List<Timer> timers = new LinkedList<>();
List<Assertion> assertions = new LinkedList<>();
LinkedList<PostProcessor> posts = new LinkedList<>();
LinkedList<PreProcessor> pres = new LinkedList<>();
for (int i = stack.size(); i > 0; i--) {
addDirectParentControllers(controllers, stack.get(i - 1));
for (Object item : testTree.list(stack.subList(0, i))) {
if (item instanceof SampleListener) {
listeners.add((SampleListener) item);
}
if (item instanceof Assertion) {
assertions.add((Assertion) item);
}
}
}
SamplePackage pack = new SamplePackage(configs, listeners, timers, assertions, posts, pres, controllers);
pack.setSampler(new TransactionSampler(tc, tc.getName()));
pack.setRunningVersion(true);
transactionControllerConfigMap.put(tc, pack);
}
use of org.apache.jmeter.control.TransactionSampler in project jmeter by apache.
the class JMeterThread method triggerEndOfLoopOnParentControllers.
/**
* Trigger end of loop on parent controllers up to Thread Group
* @param sam Sampler Base sampler
* @param threadContext
*/
private void triggerEndOfLoopOnParentControllers(Sampler sam, JMeterContext threadContext) {
TransactionSampler transactionSampler = null;
if (sam instanceof TransactionSampler) {
transactionSampler = (TransactionSampler) sam;
}
Sampler realSampler = findRealSampler(sam);
if (realSampler == null) {
throw new IllegalStateException("Got null subSampler calling findRealSampler for:" + (sam != null ? sam.getName() : "null") + ", sam:" + sam);
}
// Find parent controllers of current sampler
FindTestElementsUpToRootTraverser pathToRootTraverser = new FindTestElementsUpToRootTraverser(realSampler);
testTree.traverse(pathToRootTraverser);
// Trigger end of loop condition on all parent controllers of current sampler
List<Controller> controllersToReinit = pathToRootTraverser.getControllersToRoot();
for (Controller parentController : controllersToReinit) {
if (parentController instanceof AbstractThreadGroup) {
AbstractThreadGroup tg = (AbstractThreadGroup) parentController;
tg.startNextLoop();
} else {
parentController.triggerEndOfLoop();
}
}
// then we still need to report the Transaction in error (and create the sample result)
if (transactionSampler != null) {
SamplePackage transactionPack = compiler.configureTransactionSampler(transactionSampler);
doEndTransactionSampler(transactionSampler, null, transactionPack, threadContext);
}
}
use of org.apache.jmeter.control.TransactionSampler in project jmeter by apache.
the class JMeterThread method executeSamplePackage.
/*
* Execute the sampler with its pre/post processors, timers, assertions
* Broadcast the result to the sample listeners
*/
private void executeSamplePackage(Sampler current, TransactionSampler transactionSampler, SamplePackage transactionPack, JMeterContext threadContext) {
threadContext.setCurrentSampler(current);
// Get the sampler ready to sample
SamplePackage pack = compiler.configureSampler(current);
runPreProcessors(pack.getPreProcessors());
// Hack: save the package for any transaction controllers
threadVars.putObject(PACKAGE_OBJECT, pack);
delay(pack.getTimers());
Sampler sampler = pack.getSampler();
sampler.setThreadContext(threadContext);
// TODO should this set the thread names for all the subsamples?
// might be more efficient than fetching the name elsewhere
sampler.setThreadName(threadName);
TestBeanHelper.prepare(sampler);
// Perform the actual sample
currentSampler = sampler;
if (!sampleMonitors.isEmpty()) {
for (SampleMonitor sampleMonitor : sampleMonitors) {
sampleMonitor.sampleStarting(sampler);
}
}
SampleResult result = null;
try {
result = sampler.sample(null);
} finally {
if (!sampleMonitors.isEmpty()) {
for (SampleMonitor sampleMonitor : sampleMonitors) {
sampleMonitor.sampleEnded(sampler);
}
}
}
currentSampler = null;
// If we got any results, then perform processing on the result
if (result != null) {
int nbActiveThreadsInThreadGroup = threadGroup.getNumberOfThreads();
int nbTotalActiveThreads = JMeterContextService.getNumberOfThreads();
result.setGroupThreads(nbActiveThreadsInThreadGroup);
result.setAllThreads(nbTotalActiveThreads);
result.setThreadName(threadName);
SampleResult[] subResults = result.getSubResults();
if (subResults != null) {
for (SampleResult subResult : subResults) {
subResult.setGroupThreads(nbActiveThreadsInThreadGroup);
subResult.setAllThreads(nbTotalActiveThreads);
subResult.setThreadName(threadName);
}
}
threadContext.setPreviousResult(result);
runPostProcessors(pack.getPostProcessors());
checkAssertions(pack.getAssertions(), result, threadContext);
// Do not send subsamples to listeners which receive the transaction sample
List<SampleListener> sampleListeners = getSampleListeners(pack, transactionPack, transactionSampler);
notifyListeners(sampleListeners, result);
compiler.done(pack);
// Add the result as subsample of transaction if we are in a transaction
if (transactionSampler != null) {
transactionSampler.addSubSamplerResult(result);
}
// Check if thread or test should be stopped
if (result.isStopThread() || (!result.isSuccessful() && onErrorStopThread)) {
if (transactionSampler != null) {
doEndTransactionSampler(transactionSampler, current, transactionPack, threadContext);
}
stopThread();
}
if (result.isStopTest() || (!result.isSuccessful() && onErrorStopTest)) {
if (transactionSampler != null) {
doEndTransactionSampler(transactionSampler, current, transactionPack, threadContext);
}
shutdownTest();
}
if (result.isStopTestNow() || (!result.isSuccessful() && onErrorStopTestNow)) {
if (transactionSampler != null) {
doEndTransactionSampler(transactionSampler, current, transactionPack, threadContext);
}
stopTestNow();
}
if (result.isStartNextThreadLoop()) {
threadContext.setRestartNextLoop(true);
}
} else {
// Finish up
compiler.done(pack);
}
}
use of org.apache.jmeter.control.TransactionSampler in project jmeter by apache.
the class JMeterThread method processSampler.
/**
* Process the current sampler, handling transaction samplers.
*
* @param current sampler
* @param parent sampler
* @param threadContext
* @return SampleResult if a transaction was processed
*/
private SampleResult processSampler(Sampler current, Sampler parent, JMeterContext threadContext) {
SampleResult transactionResult = null;
try {
// Check if we are running a transaction
TransactionSampler transactionSampler = null;
if (current instanceof TransactionSampler) {
transactionSampler = (TransactionSampler) current;
}
// Find the package for the transaction
SamplePackage transactionPack = null;
if (transactionSampler != null) {
transactionPack = compiler.configureTransactionSampler(transactionSampler);
// Check if the transaction is done
if (transactionSampler.isTransactionDone()) {
transactionResult = doEndTransactionSampler(transactionSampler, parent, transactionPack, threadContext);
// Transaction is done, we do not have a sampler to sample
current = null;
} else {
Sampler prev = current;
// It is the sub sampler of the transaction that will be sampled
current = transactionSampler.getSubSampler();
if (current instanceof TransactionSampler) {
// recursive call
SampleResult res = processSampler(current, prev, threadContext);
threadContext.setCurrentSampler(prev);
current = null;
if (res != null) {
transactionSampler.addSubSamplerResult(res);
}
}
}
}
// Check if we have a sampler to sample
if (current != null) {
executeSamplePackage(current, transactionSampler, transactionPack, threadContext);
}
if (scheduler) {
// checks the scheduler to stop the iteration
stopSchedulerIfNeeded();
}
} catch (JMeterStopTestException e) {
// NOSONAR
if (log.isInfoEnabled()) {
log.info("Stopping Test: {}", e.toString());
}
if (current != null && current instanceof TransactionSampler) {
doEndTransactionSampler((TransactionSampler) current, parent, compiler.configureTransactionSampler((TransactionSampler) current), threadContext);
}
shutdownTest();
} catch (JMeterStopTestNowException e) {
// NOSONAR
if (log.isInfoEnabled()) {
log.info("Stopping Test with interruption of current samplers: {}", e.toString());
}
if (current != null && current instanceof TransactionSampler) {
doEndTransactionSampler((TransactionSampler) current, parent, compiler.configureTransactionSampler((TransactionSampler) current), threadContext);
}
stopTestNow();
} catch (JMeterStopThreadException e) {
// NOSONAR
if (log.isInfoEnabled()) {
log.info("Stopping Thread: {}", e.toString());
}
if (current != null && current instanceof TransactionSampler) {
doEndTransactionSampler((TransactionSampler) current, parent, compiler.configureTransactionSampler((TransactionSampler) current), threadContext);
}
stopThread();
} catch (Exception e) {
if (current != null) {
log.error("Error while processing sampler: '{}'.", current.getName(), e);
} else {
log.error("Error while processing sampler.", e);
}
}
return transactionResult;
}
use of org.apache.jmeter.control.TransactionSampler in project jmeter by apache.
the class JMeterThread method doEndTransactionSampler.
private SampleResult doEndTransactionSampler(TransactionSampler transactionSampler, Sampler parent, SamplePackage transactionPack, JMeterContext threadContext) {
SampleResult transactionResult;
// Get the transaction sample result
transactionResult = transactionSampler.getTransactionResult();
transactionResult.setThreadName(threadName);
transactionResult.setGroupThreads(threadGroup.getNumberOfThreads());
transactionResult.setAllThreads(JMeterContextService.getNumberOfThreads());
// Check assertions for the transaction sample
checkAssertions(transactionPack.getAssertions(), transactionResult, threadContext);
// Notify listeners with the transaction sample result
if (!(parent instanceof TransactionSampler)) {
notifyListeners(transactionPack.getSampleListeners(), transactionResult);
}
compiler.done(transactionPack);
return transactionResult;
}
Aggregations