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

无法从JMeter的JSR223 Sampler中获取值问题求助

Troubleshooting Your JMeter JSR223 Sampler Value Retrieval Issue

Hey there, let's work through why your JSR223 Sampler isn't pulling the values you expect. I spotted a few key issues in your code that are likely blocking the logic from running correctly—let's break them down and fix it step by step:

1. Incorrect Size Comparison Logic

Your code compares options.size() (which returns an integer) to string values "1" and "2". Integer-to-string comparisons will never evaluate to true, so the entire block inside your if statement never executes.

Fix: Swap the string literals for integer values:

if(options.size() == 1 || options.size() == 2) {

2. Truncated RandomUtils Method Call

Your code cuts off at RandomUtils.nextInt(0, va...—this is missing the upper bound parameter, which should be values.size() to pick a valid random index within the values list. A malformed method call here will throw an error and stop the script in its tracks.

Fix: Complete the method call properly:

def randomValue = values.get(RandomUtils.nextInt(0, values.size()))

3. Add Null Safety Checks

To avoid unexpected NullPointerExceptions (for example, if options or values is empty or null), add quick guard clauses before accessing their properties.

Corrected Full Code

Here's the revised code with all fixes plus debug logs to help you verify each step of the process:

import com.jayway.jsonpath.JsonPath
import org.apache.commons.lang3.RandomUtils
import org.apache.jmeter.samplers.SampleResult

// Capture and log the previous sampler's response for debugging
def prevResponse = prev.getResponseDataAsString()
log.info("Previous sampler response: " + prevResponse)

// Extract the options array from the response
def options = JsonPath.read(prevResponse, '$.options')
log.info("Extracted options count: " + options.size())

// Validate options size with integer comparisons + null check
if(options != null && (options.size() == 1 || options.size() == 2)) {
    def randomOption = options.get(0)
    log.info("Selected option data: " + randomOption)
    
    // Extract and store the 'code' value
    def code = randomOption.get("code")
    vars.put('code1', code)
    log.info("Stored 'code1' variable: " + code)
    
    // Extract values list and pick a random entry
    def values = randomOption.get('values')
    if(values != null && !values.isEmpty()) {
        def randomValue = values.get(RandomUtils.nextInt(0, values.size()))
        log.info("Selected random value: " + randomValue)
        // Optional: Store the random value in a JMeter variable if needed
        // vars.put('selectedValue', randomValue.toString())
    } else {
        log.warn("Values list is null or empty—can't pick a random value!")
    }
} else {
    log.warn("Options list is null or doesn't have 1/2 items—skipping logic!")
}

Quick Debugging Tips

  • Open the JMeter Log Viewer (View → Log Viewer) to check the debug messages from the log.info() calls. This will tell you:
    • If the previous sampler's response matches what you expect
    • If the options array is being extracted correctly
    • Whether your code is entering the if blocks as intended
  • Test your JSON Path expression $.options using JMeter's built-in JSON Path Tester (under the Tools menu) to confirm it targets the right data in your response.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:08:51