JMeter随机交错测试计划配置需求:实现A、B请求重复执行与不重复随机数字请求的交错执行
Great question! I’ve run into this exact scenario before, and the trick is to combine a few JMeter controllers to get that interleaved, non-repeating behavior you want. Here’s how to set it up step by step:
Step 1: Generate a Random, Non-Repeating Sequence of Requests 1-8
First, we need to create a shuffled list of numbers 1 through 8 so each request runs exactly once in random order. Add a JSR223 Sampler at the very top of your Thread Group (before any other controllers) and paste this Groovy code:
// Create a list of numbers 1-8 def requestList = (1..8).toList() // Shuffle the list to randomize order Collections.shuffle(requestList) // Store each shuffled number in a JMeter variable (request_1, request_2, ..., request_8) for (int i = 0; i < requestList.size(); i++) { vars.put("request_${i+1}", requestList[i].toString()) } // Store the total count of requests (8) for later use vars.put("total_requests", requestList.size().toString())
This code will generate a random, non-repeating order and save it to JMeter variables we’ll use next.
Step 2: Set Up a ForEach Controller to Loop Through the Shuffled Sequence
Add a ForEach Controller right after the JSR223 Sampler. Configure it like this:
- Input variable prefix:
request(matches the prefix we used in the Groovy code) - Start index for loop:
1 - End index for loop:
${total_requests}(uses the variable we saved earlier) - Output variable name:
current_request(this will hold the current number from our shuffled list each loop)
Step 3: Add Your Interleaved A, B, and Target Requests Inside the ForEach Controller
Inside the ForEach Controller, add the following elements in order:
HTTP Request A: Your first fixed request
HTTP Request B: Your second fixed request
Switch Controller: This will route to the correct numbered request based on the
current_requestvariable. Configure the Switch Controller with:- Switch value:
${current_request}
Then, add your 8 numbered HTTP requests (1 through 8) as direct children of the Switch Controller. Make sure the order of these requests matches their numbers (i.e., first child is HTTP Request 1, second is HTTP Request 2, etc.)—the Switch Controller will pick the child index matching the
current_requestvalue.- Switch value:
Step 4: Configure the Thread Group
Set your Thread Group’s Loop Count to 1—the ForEach Controller will handle looping through all 8 requests automatically.
How It Works
When you run the test:
- The JSR223 Sampler shuffles 1-8 and saves the order to variables.
- The ForEach Controller loops 8 times (once for each numbered request).
- Each loop runs:
- HTTP Request A → HTTP Request B → The next unique numbered request from our shuffled list.
This gives you exactly the execution flow you want: A, B, 7 → A, B, 1 → A, B, 6 → ... with every numbered request running exactly once, in random order, preceded by A and B each time.
内容的提问来源于stack exchange,提问作者Dave Wishart

