Installing Java Environment
JMeter is a Java-based tool, so a compatible JDK (Java Development Kit) is required. For Ubuntu, you can install OpenJDK using the following commands:
sudo apt update
sudo apt install openjdk-11-jdk # Recommended version (JMeter 5.x+ supports Java 8+)
Verify the installation with:
java -version
This should display the installed Java version (e.g., “openjdk version 11.0.xx”).
Installing Apache JMeter
- Download JMeter: Use
wgetto fetch the latest stable version from the official Apache website:wget https://downloads.apache.org/jmeter/binaries/apache-jmeter-5.6.3.tgz # Replace with the latest version - Extract the Archive: Unzip the downloaded file to a directory (e.g.,
/optfor system-wide access):sudo tar -xzf apache-jmeter-5.6.3.tgz -C /opt - Configure Environment Variables: Edit the system profile file (
/etc/profile) to add JMeter to yourPATH:
Add these lines at the end (replacesudo vim /etc/profile/opt/apache-jmeter-5.6.3with your actual path):Save the file and apply changes:export JMETER_HOME=/opt/apache-jmeter-5.6.3 export PATH=$JMETER_HOME/bin:$PATHsource /etc/profile - Verify Installation: Run
jmeter -vto confirm JMeter is accessible. You should see version details (e.g., “Apache JMeter 5.6.3”).
Creating a Basic Test Plan
JMeter uses “test plans” (.jmx files) to define performance tests. Here’s how to create one via the GUI (for simplicity) and run it in non-GUI mode:
- Open JMeter GUI: Run
jmeterfrom the terminal to launch the graphical interface. - Add a Thread Group: Right-click “Test Plan” → “Add” → “Threads (Users)” → “Thread Group”. Configure:
- Number of Threads (users): Number of concurrent users (e.g., 50).
- Ramp-up Period (seconds): Time to start all threads (e.g., 10 seconds).
- Loop Count: Number of iterations (e.g., 10).
- Add an HTTP Request: Right-click the Thread Group → “Add” → “Sampler” → “HTTP Request”. Fill in:
- Server Name or IP: Your target server (e.g.,
example.com). - Path: The endpoint to test (e.g.,
/api/v1/users).
- Server Name or IP: Your target server (e.g.,
- Add Listeners: Listeners collect and display results. Right-click the Thread Group → “Add” → “Listener” → “Aggregate Report” (for summary metrics) and “View Results Tree” (for detailed request/response data, disable in production).
- Save the Test Plan: Click “File” → “Save As” and name it (e.g.,
my_test_plan.jmx).
Running Performance Tests (Non-GUI Mode)
Non-GUI mode is essential for high-concurrency tests—it reduces resource usage on the client machine. Use this command:
jmeter -n -t /path/to/my_test_plan.jmx -l /path/to/results.jtl -e -o /path/to/report
-n: Non-GUI mode.-t: Path to the test plan (.jmx) file.-l: Path to save results (.jtlfile, contains raw data).-e: Generate HTML report after the test.-o: Directory to store the HTML report (must be empty).
Example:
jmeter -n -t ~/jmeter/my_test_plan.jmx -l ~/jmeter/results.jtl -e -o ~/jmeter/report
After the test completes, open the HTML report in ~/jmeter/report/index.html to view metrics like response time, throughput, and error rate.
Monitoring System Resources During Testing
To analyze performance bottlenecks, monitor server resources (CPU, memory, disk I/O) during the test. Use these tools:
- Top: Real-time view of CPU and memory usage:
PresstopShift + Pto sort by CPU usage,Shift + Mfor memory. - Vmstat: System-wide performance stats (run every second for 60 seconds):
The log includes CPU idle time, memory usage, and disk I/O.vmstat 1 60 > vmstat.log - Nmon: Detailed resource monitoring (install via
sudo apt install nmon):
This generates anmon -f -s 1 -c 60.nmonfile (usenmon_analyzerto convert it into charts).
Best Practices for Effective Testing
- Use Non-GUI Mode Always: GUI mode consumes significant CPU/memory (~10-25% of system resources). Reserve it for test creation/debugging only.
- Limit Listeners in Production Tests: Listeners like “View Results Tree” store detailed request/response data, which increases memory usage. Disable them during high-concurrency tests and use them only to analyze specific requests.
- Optimize JMeter Memory: Edit the
jmeterscript (in thebindirectory) to increase heap size. For example, set:This allocates 1GB initial heap, 4GB maximum heap, and 512MB for metadata.HEAP="-Xms1g -Xmx4g -XX:MaxMetaspaceSize=512m" - Distribute Load for High Concurrency: For tests exceeding 500 concurrent users, use JMeter’s distributed testing feature (set up multiple “slave” machines to run tests in parallel).
Troubleshooting Common Issues
- Java Not Found: If you see
java: command not found, ensure Java is installed andJAVA_HOMEis correctly set in/etc/profile. - Port Conflicts: JMeter uses port 1099 by default for distributed testing. If another process uses it, change the port in
jmeter.properties(look forserver.rmi.localport). - Plugin Errors: If you encounter
NoClassDefFoundError(e.g., for WebSocket plugins), install the required dependencies. For WebSocket tests, downloadjetty-http,jetty-util, andwebsocket-clientJARs and place them in thelib/extdirectory.
以上就是关于“ubuntu jmeter性能测试”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm