ubuntu jmeter性能测试
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
wget
to 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.,
/opt
for 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.3
with your actual path):
Save the file and apply changes:export JMETER_HOME=/opt/apache-jmeter-5.6.3 export PATH=$JMETER_HOME/bin:$PATH
source /etc/profile
- Verify Installation: Run
jmeter -v
to 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
jmeter
from 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 (.jtl
file, 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:
Presstop
Shift + P
to sort by CPU usage,Shift + M
for 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
.nmon
file (usenmon_analyzer
to 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
jmeter
script (in thebin
directory) 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_HOME
is 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-client
JARs and place them in thelib/ext
directory.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: ubuntu jmeter性能测试
本文地址: https://pptw.com/jishu/716431.html