如何使用JMeter获取API内部调用的响应时间
Hey there! I’ve tackled this exact problem a few times, so let’s break down how you can capture those internal API call response times using JMeter.
方法1:用HTTP(S) Test Script Recorder捕获所有请求
This is the most straightforward approach if you can replicate the main API call flow:
- First, set up JMeter as a proxy: Go to WorkBench > Add > Non-Test Elements > HTTP(S) Test Script Recorder. The default proxy port is 8888, but you can adjust it if needed.
- Configure your browser or the application making the main API call to use JMeter’s proxy (localhost:8888 for HTTP, same port for HTTPS).
- Don’t forget to install JMeter’s temporary root certificate (found in the
binfolder asApacheJMeterTemporaryRootCA.crt) into your browser/app’s trusted certificates—this lets JMeter decrypt HTTPS traffic. - Start the recorder, then trigger the main API request. JMeter will automatically capture every internal API call that the main request initiates.
- Once done, you’ll see all the sub-API requests in your test plan. Each one will have its own response time metrics, which you can view in the View Results Tree or analyze with the Aggregate Report or Summary Report listeners.
方法2:手动添加子API请求到测试计划
If you already know the exact internal APIs that the main call invokes, you can skip recording and build the test plan manually:
- Add a Thread Group to your test plan.
- Add the main HTTP Request first, then add separate HTTP Requests for each internal API that gets called.
- Make sure the order of requests matches the actual flow of the main API (if internal calls are sequential). If they’re parallel, you can use a Parallel Controller to simulate that.
- Each sub-request will track its own response time, so you’ll get granular metrics for every internal call right in JMeter’s listeners.
方法3:解析响应中的内嵌计时信息(进阶)
Sometimes the main API’s response body includes details about internal call times (like a JSON object listing each sub-API and its latency). If that’s the case, you can extract these values with a JSR223 PostProcessor:
- Add a JSR223 PostProcessor as a child of your main HTTP Request.
- Use Groovy (it’s the fastest and most reliable scripting option in JMeter) to parse the response. For example, if your response is JSON:
import groovy.json.JsonSlurper // Get the main API's response body def responseBody = prev.getResponseDataAsString() def jsonData = new JsonSlurper().parseText(responseBody) // Assume the internal calls are in a field called "internalApiCalls" jsonData.internalApiCalls.each { apiCall -> def apiName = apiCall.apiEndpoint def responseTime = apiCall.latencyMs // Log the timing for debugging log.info("Internal API ${apiName} took ${responseTime}ms") // Store the value in a JMeter variable for later use (like reporting) vars.put("${apiName}_responseTime", responseTime.toString()) }
- You can then use these variables in custom reports or pass them to other parts of your test plan.
Quick Tips
- Always use the latest version of JMeter—newer versions have better support for modern protocols and bug fixes around request capture.
- If internal calls use WebSockets instead of HTTP, swap out the HTTP Samplers with JMeter’s WebSocket Samplers (you’ll need to install the WebSocket plugin via JMeter Plugins Manager first).
- Use the Transaction Controller to group the main API and its internal calls together if you want to track both individual and combined response times.
内容的提问来源于stack exchange,提问作者Prasad
相关产品推荐
相关产品推荐

