如何在JMeter非GUI模式下获取RSP与TSP(无需HTML及手动解析JTL)
Got it, let's break this down—you're running JMeter headless on a server, dealing with multi-threaded tests, and need a quick way to grab both response times (RSP) and throughput (TSP) without HTML reports or loading JTLs locally. Perfect, here are two solid, actionable solutions:
1. Use JMeter's Built-In Aggregate Report for Out-of-the-Box CSV
The easiest way is to leverage JMeter's Aggregate Report listener, which natively outputs both response time metrics (average, median, 90th percentile, etc.) and throughput directly to a CSV. You can set this up either by modifying your test plan once, or via command line without touching the original script.
Option A: Modify Your Test Plan (One-Time Setup)
- If you can temporarily open your test plan in JMeter GUI (even just once), add
Listeners > Aggregate Reportto your test plan. - Configure the listener: Check "Save Table Data" and set the output path to something like
./test_results.csv. - Run your test headless as usual:
After the test finishes,jmeter -n -t your_test_plan.jmx -l raw_results.jtl -j jmeter.logtest_results.csvwill have all the RSP metrics you need (average, min, max, percentiles) plus a Throughput column (measured in requests/second) which is your TSP.
Option B: Dynamic Command-Line Setup (No Script Changes)
If you can't modify the original test plan, use a properties file to dynamically enable the Aggregate Report output:
- Create a file named
aggregate_config.propertieswith these lines:aggregate_report.save.summarized_results=true aggregate_report.output_file=./test_results.csv - Run your test with this properties file:
This will generate the same CSV with RSP and TSP metrics without altering your original test plan.jmeter -n -t your_test_plan.jmx -l raw_results.jtl -j jmeter.log -q aggregate_config.properties
2. Custom CSV with JSR223 Listener (For Minimal, Tailored Output)
If you only need the bare essentials (e.g., just average response time and real-time throughput) and want to avoid extra columns, use a JSR223 Listener with Groovy (JMeter's fastest scripting language) to write directly to a CSV during the test.
- Add
Listeners > JSR223 Listenerto your test plan. - Select "Groovy" as the language, then paste this script:
// Initialize CSV header once at test start if (ctx.getThreadNum() == 0 && vars.get("csvInit") == null) { def csv = new File("./custom_metrics.csv") csv.write("Timestamp,Average_RSP_ms,Throughput_TSP_req_sec\n") vars.put("csvInit", "done") } // Calculate core metrics def totalRequests = ctx.getEngine().getSampleCount() def elapsedSeconds = (System.currentTimeMillis() - ctx.getEngine().getStartTime()) / 1000 def throughput = totalRequests / elapsedSeconds def avgResponseTime = ctx.getEngine().getResultCollector().getAverage() // Write to CSV every 10 seconds (adjust interval as needed) def currentTime = new Date().format("yyyy-MM-dd HH:mm:ss") if (elapsedSeconds % 10 == 0 && vars.get("lastWrite") != elapsedSeconds.toString()) { def csv = new File("./custom_metrics.csv") csv.append("${currentTime},${avgResponseTime},${String.format("%.2f", throughput)}\n") vars.put("lastWrite", elapsedSeconds.toString()) } - Run your test headless—this script will write a row of RSP and TSP data every 10 seconds (you can adjust the interval by changing the
% 10value) tocustom_metrics.csv.
Quick Notes:
- Make sure JMeter's
bindirectory is in your system PATH, or use the full path to thejmeterexecutable. - Groovy is included with JMeter 3.2+, so no extra setup is needed for scripting.
- The Aggregate Report's throughput metric is JMeter's standard calculation: total requests divided by the total elapsed time of the test, which aligns with standard TSP definitions.
内容的提问来源于stack exchange,提问作者Abhishek Prakash

