Alex Martian
Alex Martian

Reputation: 3812

Jmeter: how to disable listener by code (groovy)

I've tried to disable view resutls tree by groovy code. The code runs, correctly shows and changes the name and enable property (as reported by log), but neither actual ceasing of info in GUI nor writing to file by the listener (both GUI and non-gui mode) happens. Listeners are processed at the end, so IMHO the code that is executed in setUp thread should have effect on logging of other threads. What enabled property do?

I've seen a workaround by editing jmeter plan file in place (JMeter: how can I disable a View Results Tree element from the command line?), but I'd like internal jmeter/groovy solution.

The code (interestingly each listener is processed twice, first printed view resuts tree, next already foo):

import org.apache.jmeter.engine.StandardJMeterEngine
import org.apache.jorphan.collections.HashTree
import org.apache.jorphan.collections.SearchByClass
import org.apache.jmeter.reporters.ResultCollector
def engine = ctx.getEngine()
def test = engine.getClass().getDeclaredField("test")
test.setAccessible(true)
def testPlanTreeRC = (HashTree) test.get(engine)
def rcSearch = new SearchByClass<>(ResultCollector.class)
testPlanTreeRC.traverse(rcSearch)
def rcTrees = rcSearch.getSearchResults()
for (rc in rcTrees) {
    log.error(rc.getName())
    if (rc.isEnabled()) {log.error("yes")} else {log.error("no")}
    rc.setEnabled(false)
    if (rc.isEnabled()) {log.error("yes")} else {log.error("no")}
    if (rc.getName()=="View Results Tree") {rc.setName ("foo")}
}

ADDED: when listener is disabled in test plan in GUI, it's not found by traverse tree code above.

Upvotes: 1

Views: 667

Answers (1)

Ori Marko
Ori Marko

Reputation: 58862

disabled property is used/checked by JMeter on startup so there must be a change in JMeter code

I open an enhancement Add option to disable View Results Tree/Listeners in non GUI

You can vote on

There are other options executing JMeter externally, using Taurus tool or execute JMeter using Java and disable it:

HashTree testPlanTree = SaveService.loadTree(new File("/path/to/your/testplan"));
SearchByClass<ResultCollector> listenersSearch = new SearchByClass<>(ResultCollector.class);
testPlanTree.traverse(listenersSearch);
Collection<ResultCollector> listeners = listenersSearch.getSearchResults();
listeners.forEach(listener -> listener.setProperty(TestElement.ENABLED, false));
jmeter.configure(testPlanTree);
jmeter.run();

Upvotes: 1

Related Questions