JMeter pre-processed variable as part of report file name

前端 未结 2 1278
心在旅途
心在旅途 2021-01-07 05:37

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\

相关标签:
2条回答
  • 2021-01-07 05:49

    you cannot use a variable as part of a summary report file name as a variable exist per User thread and after summary report has been initialized.

    Use property instead:

    • https://jmeter.apache.org/usermanual/functions.html
    0 讨论(0)
  • 2021-01-07 06:04

    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:

    1. Get your Terradata session id outside of JMeter, i.e. using BTEQ or equivalent
    2. Pass the value to JMeter via -J command line argument like:

      jmeter -Jsession_id_1=1234 -n -t /path/to/testplan.jmx
      
    3. 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:

    1. You need to remove everything from "Filename" input of the Summary Report listener. Just let it be blank.
    2. Make sure that below code is executed only once and with only one thread.

    So:

    1. Add JSR223 PostProcessor as a child of the query1 sampler and after the Session PreProcessor
    2. Select groovy in the "Language" drop-down
    3. Put 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.

    0 讨论(0)
提交回复
热议问题