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

JMeter TCP采样器针对动态响应如何配置动态EOL字节值?

动态配置JMeter TCP采样器的EOL字节值

Great question! Handling dynamic EOL values in JMeter's TCP Sampler based on server responses is totally achievable with a mix of JMeter variables and JSR223 scripting. Below are two practical approaches, depending on how flexible you need to be:

方案一:基于TCP采样器+JSR223后置处理器(推荐,保留JMeter原生组件)

This approach uses JMeter's built-in TCP Sampler and a JSR223 PostProcessor to dynamically update the EOL byte variable after each response.

步骤1:初始化全局变量

Add a User Defined Variables element to your test plan. Create a variable named eol_byte with a default value (use the ASCII code for 'e' which is 101, since that's your fallback for approve/decline responses).

步骤2:配置TCP采样器

Set up your TCP Sampler with your server's host and port. In the EOL byte value field, reference the variable using:

${eol_byte}

This tells the sampler to use the dynamic value stored in eol_byte instead of a hardcoded number.

步骤3:添加JSR223后置处理器

Attach a JSR223 PostProcessor directly to the TCP Sampler. Use this Groovy script to check the server response and update the eol_byte variable:

// Get the raw response and clean up whitespace
def rawResponse = prev.getResponseDataAsString().trim()

// Update EOL byte based on response content
if (rawResponse in ['approve', 'decline']) {
    // Set EOL to ASCII value of 'e' (101)
    vars.put('eol_byte', '101')
} else if (rawResponse == 'hold') {
    // Set EOL to ASCII value of 'd' (100)
    vars.put('eol_byte', '100')
} else {
    // Optional: Fallback to default if response is unexpected
    vars.put('eol_byte', '101')
}

// Log the update for debugging (optional)
log.info("Updated EOL byte to: ${vars.get('eol_byte')} based on response: ${rawResponse}")
  • prev refers to the current TCP Sampler's result object, so we can fetch the response.
  • vars is JMeter's variable manager, used to update the global eol_byte variable for subsequent requests.

注意事项

  • For your first request, the default eol_byte value will be used. If your server's first response might use 'd' (for hold), adjust the default value accordingly.
  • Enable Compile script locally in the JSR223 PostProcessor settings to improve performance during load testing.

方案二:完全用JSR223 Sampler手动处理TCP连接(更灵活)

If you need full control over the TCP communication (e.g., if the initial response's EOL is unknown), use a JSR223 Sampler to handle the connection, reading, and EOL logic directly:

import java.net.Socket
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter

// Server configuration - replace with your details
def serverHost = "your-server-ip"
def serverPort = 1234
def requestPayload = "your-request-data" // Replace with your actual request

// Establish TCP connection
Socket socket = new Socket(serverHost, serverPort)
OutputStreamWriter output = new OutputStreamWriter(socket.getOutputStream())
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()))

// Send request to server
output.write(requestPayload)
output.flush()

// Read response until we hit 'e' or 'd' (the dynamic EOLs)
def responseBuilder = new StringBuilder()
int charCode
while ((charCode = input.read()) != -1) {
    char currentChar = (char) charCode
    responseBuilder.append(currentChar)
    
    // Stop reading when we hit either EOL character
    if (currentChar == 'e' || currentChar == 'd') {
        break
    }
}

def cleanResponse = responseBuilder.toString().trim()
log.info("Received response: ${cleanResponse}")

// Update EOL variable for subsequent requests
if (cleanResponse in ['approve', 'decline']) {
    vars.put('eol_byte', '101')
} else if (cleanResponse == 'hold') {
    vars.put('eol_byte', '100')
}

// Set sampler result for JMeter reports
prev.setResponseData(cleanResponse.getBytes())
prev.setResponseCode("200")
prev.setResponseMessage("Successfully received response")

// Clean up resources to avoid leaks
input.close()
output.close()
socket.close()

This approach bypasses the TCP Sampler entirely, giving you full control over how you read the response and handle dynamic EOLs. It's especially useful if your server's response doesn't follow a strict EOL pattern initially.


内容的提问来源于stack exchange,提问作者Anil Kumar R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:55:48