1

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)

Community
  • 1
  • 1
Shawn
  • 10,931
  • 18
  • 81
  • 126

2 Answers2

1

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.

uru917
  • 91
  • 13
  • +1 Yes, this is plan B. I was hoping to get access specifically to the value from the Thread Group Properties. – Shawn Apr 20 '17 at 14:00
-2

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

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • 2
    I've accepted your answer, but I think it would be much better without the trash talk. Your assumptions about my behavior contribute nothing to the community and they are wrong. I was not given an answer to the same question 6 hours prior, in fact it is not the same question, as I have explained in my edit. You may even notice that the working answer to the previous question does not work in this case. In both cases I did try myself with scripting and failed. Indeed, your code is far from trivial to someone new to groovy and the jmeter runtime.` – Shawn Apr 20 '17 at 14:22
  • That is why I marked this question as a possible duplicate :) – NaveenKumar Namachivayam Apr 20 '17 at 14:45