Using Groovy Regular Expressions in JMeter for Performance Testing
This article demonstrates how to apply Groovy regular expressions within JMeter to extract fields from API responses, showing step‑by‑step configuration of a thread group, JSR223 post‑processor script, and variable handling for performance testing.
Previously, several articles covered how Groovy can assist testing in JMeter, including handling assertions, variables, command execution, request parameters, and regular expressions.
Regular expressions are powerful text patterns used to locate matching strings; in Apache JMeter they can be used via the built‑in Regular Expression Extractor or written in Groovy for greater flexibility.
Combining Groovy with regular expressions provides more flexibility and saves time, especially when extracting multiple parameters with a single script instead of adding separate extractors for each request.
This article shows how to use Groovy regular expressions to extract a specific field from an API response during performance testing with JMeter.
First, create a simple Thread Group and a basic request.
Add a JSR223 Post‑Processor to the request.
Sample API response:
{
"success": 1,
"gt": "3c73c021ac3bfea7b5df8d461b5573c5",
"challenge": "60e86734e0dfb7db48c5661ff9c5c935",
"new_captcha": true
}The goal is to extract the value of the challenge field. Although parsing JSON directly would be preferable, this example uses a regular‑expression approach and stores the result in a thread‑private variable.
Groovy script used in the JSR223 Post‑Processor:
def response = prev.getResponseDataAsString()
log.info("响应内容:"+ response)
def re = response =~ /challenge":.+?"/
log.info("提取结果:"+ re[0])
def a = re[0] - "challenge:\"" - "\""
vars.put("MY1",a)
log.warn("修改后的MY1参数是:${vars.get("MY1")}")Console output after running the test:
2020-03-08 17:45:20,035 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2020-03-08 17:45:20,038 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2020-03-08 17:45:20,040 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2020-03-08 17:45:20,233 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : 线程组
2020-03-08 17:45:20,233 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group 线程组.
2020-03-08 17:45:20,233 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2020-03-08 17:45:20,233 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 perThread=1000.0 delayedStart=false
2020-03-08 17:45:20,233 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2020-03-08 17:45:20,233 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2020-03-08 17:45:20,234 INFO o.a.j.t.JMeterThread: Thread started: 线程组 1-1
2020-03-08 17:45:20,495 INFO o.a.j.a.J.JSR223 正则提取: 响应内容:{"success":1,"gt":"3c73c021ac3bfea7b5df8d461b5573c5","challenge":"b001a7188421320f77fb4631dbd72116","new_captcha":true}
2020-03-08 17:45:20,495 INFO o.a.j.a.J.JSR223 正则提取: 提取结果:challenge":"b001a7188421320f77fb4631dbd72116"
2020-03-08 17:45:20,496 WARN o.a.j.a.J.JSR223 正则提取: 修改后的MY1参数是:b001a7188421320f77fb4631dbd72116
2020-03-08 17:45:20,496 INFO o.a.j.t.JMeterThread: Thread is done: 线程组 1-1
2020-03-08 17:45:20,496 INFO o.a.j.t.JMeterThread: Thread finished: 线程组 1-1
2020-03-08 17:45:20,496 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2020-03-08 17:45:20,497 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)On the next request in the same thread, the variable MY1 contains the extracted challenge value.
Disclaimer: This article was originally published on the “FunTester” public account; redistribution without permission (except by Tencent Cloud) is prohibited.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
