从设置线程组(jmeter)获取线程组中的线程数
我有一个JMeter测试计划,该计划执行一次简单的操作.
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.
有什么想法吗?
理想情况下,我希望找到类似${__groovy(ctx.getTestPlan().getThreadGroupByIndex(1).getNumThreads())}
的东西,但找不到任何类似的东西.
Ideally I was hoping to find something like ${__groovy(ctx.getTestPlan().getThreadGroupByIndex(1).getNumThreads())}
, but I can't find anything of the sort.
为了清楚起见,我正在寻找在线程组属性的JMeter中直接分配的线程数.此问题与BlazeMeter完全无关,因此不是是
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)
您不知道线程数的快速表示测试设计不当.
The fast that you don't know the number of threads indicates that your test is badly designed.
事实上,您已经在6小时前获得了相同问题的答案,这表明您不愿意学习,更喜欢社区为您解决问题.
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:
- 如果您需要克服JMeter的限制,就必须编写脚本
- 推荐的方法是使用 JSR223测试元素和时髦的语言
- JMeter API参考位于 https://jmeter.apache.org/api/
- 查看Groovy是新的黑人文章,以获取使用Groovy的示例使用JMeter API
-
获取所有线程组的线程数的示例代码:
- 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())
}
演示: