如何使用JMeter计算两个微服务中Post与Get API的同步耗时?
Absolutely! You can absolutely pull this off with JMeter
Here's a practical, step-by-step breakdown to set up your test plan and calculate the sync time between your Post and Get APIs:
1. Build the Base Test Structure
- Start by adding a Thread Group to your test plan (right-click Test Plan > Add > Threads (Users) > Thread Group). Tweak the number of threads/iterations to match your testing needs.
2. Add the Post API & Capture Its Initiation Time
- Under the Thread Group, add your HTTP Request for the Post API (right-click Thread Group > Add > Sampler > HTTP Request). Fill in all required details: method, URL, headers, request body, etc.
- To track exactly when the Post request starts, add a JSR223 PreProcessor to the Post request (right-click Post HTTP Request > Add > Pre Processors > JSR223 PreProcessor). Paste this code to store the start timestamp in a reusable variable:
If you want to measure time from when the Post request completes (instead of starts), swap the pre-processor for a JSR223 PostProcessor and name the variablevars.put("postInitiationTime", String.valueOf(System.currentTimeMillis()));postCompletionTime.
3. Set Up a While Controller to Repeat the Get Request
- After the Post request, add a While Controller (right-click Thread Group > Add > Logic Controller > While Controller). This will loop the Get request until we get a successful response.
- For the controller's condition, use an expression that checks if the Get request hasn't succeeded yet. Examples:
- If success means a 200 response code:
${__jexl3("${getResponseCode}" != "200",)} - If success depends on specific content in the response body:
${__jexl3(!${__groovy(vars.get("getResponseBody").contains("success_flag")),},)}
To avoid infinite loops, add a maximum iteration limit, e.g.:${__jexl3(${__counter(FALSE, getLoopCount)} < 100 && "${getResponseCode}" != "200",)}(caps retries at 100)
- If success means a 200 response code:
4. Configure the Get API Inside the Loop
- Inside the While Controller, add your HTTP Request for the Get API and fill in its details.
- Add a Response Assertion to the Get request (right-click Get HTTP Request > Add > Assertions > Response Assertion) to define what counts as a "successful" response—for example, checking for a 200 code or specific text in the response.
- Add a Fixed Timer to the Get request (right-click Get HTTP Request > Add > Timers > Fixed Timer) to add a delay between retries (e.g., 1000ms = 1 second) to avoid overwhelming your server.
- When the Get request succeeds, capture the timestamp with a JSR223 PostProcessor on the Get request:
vars.put("getSuccessTime", String.valueOf(System.currentTimeMillis()));
5. Calculate & Log the Total Sync Time
- After the While Controller, add a JSR223 Sampler (right-click Thread Group > Add > Sampler > JSR223 Sampler) to compute the time difference. Use code like this:
// Adjust variable name if you used postCompletionTime instead long postStart = Long.parseLong(vars.get("postInitiationTime")); long getSuccess = Long.parseLong(vars.get("getSuccessTime")); long totalSyncMs = getSuccess - postStart; vars.put("totalSyncTime", String.valueOf(totalSyncMs)); log.info("Total sync time (Post start to Get success): " + totalSyncMs + " milliseconds");
6. View Your Results
- Add listeners like View Results Tree or Summary Report to monitor test execution and see the
totalSyncTimevalue. You can also add a Debug Sampler to inspect all variables during testing.
内容的提问来源于stack exchange,提问作者Reem Sami
相关产品推荐
相关产品推荐

