You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JMeter线程组间如何传递一组值?多值传递遇覆盖问题求助

Got it, let's fix this multi-thread-group value passing issue in JMeter. The problem with your current approach is that ${__setProperty(categoryID, ${categoryID})} overwrites the same property key every time your loop runs, so you only end up with the last category ID instead of all 5. Here's how to collect and pass multiple values properly:

Solution: Pass Multiple Values Between Thread Groups in JMeter

Step 1: Collect Multiple Category IDs in the First Thread Group

Instead of overwriting a single property, we'll store all category IDs in a global collection. Use a JSR223 PostProcessor (way more efficient than Beanshell these days) right after the sampler that retrieves the category ID.

Add this code to the JSR223 PostProcessor:

// Get the current category ID from your sampler's response (adjust variable name if needed)
def currentCategoryID = vars.get("categoryID")

// Initialize a global list if it doesn't exist yet
if (!props.containsKey("categoryIDsList")) {
    props.put("categoryIDsList", new ArrayList<String>())
}

// Add the current ID to the global list
def categoryList = props.get("categoryIDsList")
categoryList.add(currentCategoryID)

// Store the total count for easy reference in the second thread group
props.put("totalCategoryIDs", categoryList.size().toString())

Step 2: Retrieve the IDs in the Second Thread Group

Now in your second thread group, you can fetch the entire list and use the IDs as needed. Here are two common implementation methods:

Option A: Iterate Through All IDs with a Loop Controller

  1. Add a Loop Controller to your second thread group. Set the loop count to ${__P(totalCategoryIDs, 0)} (this pulls the total count we stored earlier).
  2. Add a JSR223 PreProcessor inside the Loop Controller with this code:
// Get the global list of category IDs
def categoryList = props.get("categoryIDsList")

// Get current loop index (JMeter's __counter starts at 1, subtract 1 for 0-based list access)
def currentIndex = Integer.parseInt(vars.get("__counter(TRUE,)")) - 1

// Store the current ID in a local variable for use in samplers
vars.put("currentCategoryID", categoryList.get(currentIndex))
  1. You can now use ${currentCategoryID} in any samplers within the loop.

Option B: Fetch a Specific ID Directly

If you don't need to loop through all IDs and just want to grab a specific one (e.g., the 3rd ID), use a JSR223 PreProcessor in the second thread group with:

def categoryList = props.get("categoryIDsList")
vars.put("targetCategoryID", categoryList.get(2)) // 0-based index, so 2 = 3rd item

Critical Setup: Ensure Thread Groups Run in Order

Go to your Test Plan settings and check the box for "Run thread groups consecutively". This guarantees the first thread group finishes collecting all IDs before the second one starts trying to read them. Without this, the second thread group might access the list while it's still being populated, leading to missing or incomplete values.

Why Your Original Approach Failed

Every execution of ${__setProperty(categoryID, ${categoryID})} overwrote the value of the categoryID global property with the latest ID from the loop. By using a global list instead, we preserve all collected values instead of replacing them with each loop iteration.

内容的提问来源于stack exchange,提问作者a4aravind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:26:41