In my test plan I have JDBC PreProcessor that captures a single value that I\'m trying to save into a variable. Then I want to reuse this variable as part of summary report\
Changing filenames of Listeners test elements on the fly is not really supported in JMeter as Listeners are being initialized before any variables. The recommended way is:
Pass the value to JMeter via -J command line argument like:
jmeter -Jsession_id_1=1234 -n -t /path/to/testplan.jmx
Refer the session id value via __P() function where required as
${__P(session_id_1,)}
If for any reason you still need to do it inside JMeter test script, here is a possible solution, however keep in mind the following:
So:
query1
sampler and after the Session PreProcessor
groovy
in the "Language" drop-downPut the following code into JSR223 PostProcessor "Script" area:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
StandardJMeterEngine engine = ctx.getEngine();
Field test = engine.getClass().getDeclaredField("test");
test.setAccessible(true);
HashTree testPlanTree = (HashTree) test.get(engine);
SearchByClass summaryReportsSearch = new SearchByClass(ResultCollector.class);
testPlanTree.traverse(summaryReportsSearch);
Collection summaryReports = summaryReportsSearch.getSearchResults();
ResultCollector summaryReport = summaryReports.iterator().next();
Class [] fileNameParam = new Class[1];
fileNameParam[0] = String.class;
Method setFileName = summaryReport.getClass().getDeclaredMethod("setFilenameProperty", fileNameParam);
setFileName.setAccessible(true);
setFileName.invoke(summaryReport, new String(vars.get("session_id_1")));
Method init = summaryReport.getClass().getDeclaredMethod("initializeFileOutput");
init.setAccessible(true);
init.invoke(summaryReport);
If you're using JMeter 3.0 - groovy is bundled. For previous JMeter versions you will need to install groovy language support manually, check out Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! article for groovy engine installation instructions and scripting best practices.