Search code examples
variablesjmeterbeanshell

How to get the thread and the sampler name in JMeter


I want to get the thread and the sampler names in JMeter Test Plan and keep it in the variables.

I already keep the Test Plan name using the BeanShell Post Processor:

import org.apache.jmeter.services.FileServer;

String testPlanName = FileServer.getFileServer().getScriptName(); vars.put("testPlanName", testPlanName)

I use it in the Email notification to send the messages like "Test plan ${testPlanName} have received the response code ${responseCode}

But I want to include the thread name and the sampler name the same way.

I already tried to add the BeanShell Post Processor .getThreadname() and .getSampleName() and import import org.apache.jmeter.samplers.SampleResult; but it doesn't work.

How can I get that names and use it like variables?


Solution

  • There are three functions, you may use them in BeanShell:

    1. __samplerName()

      The samplerName function returns the name (i.e. label) of the current sampler. The function does not work in Test elements that don't have an associated sampler. For example the Test Plan. Configuration elements also don't have an associated sampler. However some Configuration elements are referenced directly by samplers, such as the HTTP Header Manager and Http Cookie Manager, and in this case the functions are resolved in the context of the Http Sampler. Pre-Processors, Post-Processors and Assertions always have an associated Sampler.

      Example: ${__samplerName()}

    2. __TestPlanName()

      The TestPlanName function returns the name of the current test plan (can be used in Including Plans to know the name of the calling test plan).

      Example: ${__TestPlanName}

      Note, that __TestPlanName will return the File name of Test Plan, which can be different with Test Plan Name.

    3. __threadGroupName()

      The thread group name function simply returns the name of the thread group being executed.

      Example: ${__threadGroupName}

    For full details on using BeanShell, please see: