JMeter 4.0中统计特定超时次数并避免测试失败的技术需求
Hey there, let's break down how to fix your two key needs: counting those specific timeout messages without failing your test, and addressing the timestamp duplication issue that's causing errors.
1. 统计特定超时消息的次数到变量
To track how many times the Service is not available. Request timeout message appears, use a JSR223 PostProcessor (far more efficient than BeanShell in modern load testing tools like JMeter). Here's the step-by-step:
- Add a JSR223 PostProcessor as a child of your POST request sampler
- Paste this Groovy code into the script field:
// Grab the full response text from the current sample def responseText = prev.getResponseDataAsString() // Check if the timeout message exists in the response if (responseText.contains("Service is not available. Request timeout")) { // Fetch current count from global properties (persists across threads/thread groups) def currentCount = props.get("timeoutTotalCount") ?: 0 // Increment the count currentCount++ // Save back to global properties (use vars instead if you need a thread-local variable) props.put("timeoutTotalCount", currentCount) // Optional: Save to a thread-local variable for per-thread tracking vars.put("threadTimeoutCount", String.valueOf(currentCount)) }
- After your test runs, you can access the total count via
${__P(timeoutTotalCount,0)}in any sampler or listener.
2. 确保超时情况不导致测试失败
You mentioned you added response assertions to keep the test running, but here's a cleaner approach:
- If you had an assertion checking for the timeout message, disable or remove it (it's unnecessary now that we're using the post-processor)
- Alternatively, if you want to keep assertions for other failures, adjust the timeout-related assertion's settings:
- Tick the "Continue after failure" box
- Set the "Failure action" to "Ignore" so the sample isn't marked as failed
This way, only genuine errors (like the timestamp duplication) will flag samples as failed, while timeouts are counted but treated as normal behavior.
Bonus: Fix the Timestamp Duplication Error
Since you're hitting duplicate timestamp issues with concurrent POST requests, generate a unique timestamp per request to avoid collisions:
- Add a JSR223 PreProcessor as a child of your POST request
- Use this code to create a unique timestamp (combines system time with a random number to avoid even millisecond collisions):
// Generate a unique timestamp: current time in ms + random 0-999 integer def uniqueTimestamp = System.currentTimeMillis() + new Random().nextInt(1000) // Store as a variable to use in your request's timestamp field vars.put("uniqueTimestamp", String.valueOf(uniqueTimestamp))
- Replace your hardcoded timestamp in the POST request payload/parameters with
${uniqueTimestamp}
This will eliminate the database duplicate key error entirely for concurrent requests.
Let me know if you need help adjusting any of these steps for your specific test setup!
内容的提问来源于stack exchange,提问作者bogdanov

