JMeter | Groovy | 随机替换捕获的大文本值中部分条目的子字符串
Hey there! I've got you covered on this JMeter scripting task. Let's break down how to modify your captured operationBPlans data to randomly set some cond values to true, then pass the updated content to your next request.
Step 1: Fix the Captured Data Format
First, notice your captured data is a series of unseparated JSON objects. We need to convert this into a valid JSON array so we can parse it properly—otherwise, we can't easily manipulate individual entries.
Step 2: Use a JSR223 PostProcessor with Groovy Script
Add a JSR223 PostProcessor right after the sampler that captures the operationBPlans value. Groovy is the best choice here because it's efficient and has built-in, robust JSON handling.
Here's the full script you can use (I'll break down each part below):
// Get the captured operationBPlans value (replace with your actual variable name if different) def rawData = vars.get("operationBPlans") if (rawData) { // Convert raw concatenated objects to a valid JSON array def validJson = "[" + rawData.replaceAll("}{", "},{") + "]" // Parse the JSON array into a list of editable maps def plans = new groovy.json.JsonSlurper().parseText(validJson) // Step 3: Randomly update some "cond" values to true def random = new Random() // Customize this: e.g., 0.3 means 30% of entries will be updated def updatePercentage = 0.3 plans.each { plan -> // Only update if the current value is false and random check passes if (random.nextDouble() <= updatePercentage && plan.cond == false) { plan.cond = true } } // Convert back to the original concatenated object format (remove array brackets) def updatedData = new groovy.json.JsonBuilder(plans).toString().replaceAll(/^\[|\]$/, "") // Save the result to a new variable for your next request vars.put("updatedOperationBPlans", updatedData) log.info("Successfully updated operationBPlans: " + updatedData) } else { log.warn("No operationBPlans data found to process—check your extractor!") }
Key Customization Options:
- Variable Name: If your captured variable isn't named
operationBPlans, replace that with the exact name from your Regular Expression Extractor. - Update Logic: Tweak how many entries get updated:
- To update a fixed number of entries (e.g., 2):
def numberOfUpdates = 2 def eligiblePlans = plans.findAll { it.cond == false } if (eligiblePlans.size() >= numberOfUpdates) { eligiblePlans.take(numberOfUpdates).each { it.cond = true } }
- To update a fixed number of entries (e.g., 2):
- Output Format: If your next request expects a JSON array instead of concatenated objects, remove the
.replaceAll(/^\[|\]$/, "")part.
How to Use the Updated Data:
In your next request, reference the new variable ${updatedOperationBPlans} wherever you need to pass the modified content.
Quick Checks:
- Ensure your Regular Expression Extractor captures the full
operationBPlanscontent (double-check your regex to avoid truncating data). - In the JSR223 PostProcessor, confirm the Language dropdown is set to
groovy(it's the default in modern JMeter versions, but always verify).
内容的提问来源于stack exchange,提问作者vikas

