1
votes

I have a JMeter test plan which performs a simple action once.

The problem I am having is that my test setup needs to know how many threads the thread group will have. To make things clearer, here is a simple representation of the test plan:

setUp Thread Group
  needs to know the number of threads in the below thread group
Thread Group
  The number of threads for this thread group is determined via the "Number of Threads (users)" Thread Property.

I can dynamically get the number of threads from within the thread group, but I can't find any way of getting this number from within the setup thread group instead.

Any ideas?

Ideally I was hoping to find something like ${__groovy(ctx.getTestPlan().getThreadGroupByIndex(1).getNumThreads())}, but I can't find anything of the sort.

To be clear, I'm looking for the number of threads as assigned directly in JMeter in the Thread Group properties. This question has absolutely nothing to do with BlazeMeter and is therefore not a duplicate of Get number of threads (defined via BlazeMeter) in a thread group from the setup thread group (jmeter)

2
It's not a duplicate, the other question is about finding the number of threads as assigned by BlazeMeter whereas this one concerns finding it as assigned directly in JMeterShawn
How are you running your test? Via CMD prompt or from the UI?NaveenKumar Namachivayam
Mostly via the UI, but I can change if needed. What's the difference?Shawn
In non GUI you can pass the number of threads as a parameter. Running tests in GUI is not advised.NaveenKumar Namachivayam

2 Answers

-1
votes

The fast that you don't know the number of threads indicates that your test is badly designed.

The fact that you have already been given the answer for the same question 6 hours before indicates that you are unwilling to learn and prefer the community to solve problems for you.

Just in case I will repeat using simpler words:

  • if you need to overcome a JMeter limitation you have to go for scripting
  • the recommended approach is using JSR223 Test Elements and Groovy language
  • JMeter API reference lives at https://jmeter.apache.org/api/
  • Check out Groovy Is the New Black article for examples of using Groovy with JMeter API
  • Example code to get the number of threads for all thread groups:

    import org.apache.jmeter.engine.StandardJMeterEngine
    import org.apache.jmeter.threads.ThreadGroup
    import org.apache.jorphan.collections.HashTree
    import org.apache.jorphan.collections.SearchByClass
    import java.lang.reflect.Field
    
    StandardJMeterEngine engine = ctx.getEngine()
    Field test = engine.getClass().getDeclaredField("test")
    test.setAccessible(true)
    HashTree testPlanTree = (HashTree) test.get(engine)
    
    SearchByClass<ThreadGroup> threadGroupSearch = new SearchByClass<>(ThreadGroup.class)
    testPlanTree.traverse(threadGroupSearch)
    Collection<ThreadGroup> threadGroups = threadGroupSearch.getSearchResults()
    threadGroups.each { 
        log.info("Thread Group: " + it.getName() + "; Number of threads: " + it.getNumThreads())
    } 
    

    Demo:

    JMeter Groovy get thread groups number

1
votes

You can try defining a User Defined Variable in the Test Plan, let's say "Users_test" and assign it the number of virtual users you want to run the test.

Then simply use that variable in the Thread Group "Number of Threads (users): ${Users_test}, and you can do the same in the setUp.