ubuntu

ubuntu jmeter性能测试

小樊
62
2025-09-25 11:01:59
栏目: 智能运维

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

  1. 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
    
  2. 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
    
  3. Configure Environment Variables: Edit the system profile file (/etc/profile) to add JMeter to your PATH:
    sudo vim /etc/profile
    
    Add these lines at the end (replace /opt/apache-jmeter-5.6.3 with your actual path):
    export JMETER_HOME=/opt/apache-jmeter-5.6.3
    export PATH=$JMETER_HOME/bin:$PATH
    
    Save the file and apply changes:
    source /etc/profile
    
  4. 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:

  1. Open JMeter GUI: Run jmeter from the terminal to launch the graphical interface.
  2. 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).
  3. 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).
  4. 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).
  5. 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

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:

  1. Top: Real-time view of CPU and memory usage:
    top
    
    Press Shift + P to sort by CPU usage, Shift + M for memory.
  2. Vmstat: System-wide performance stats (run every second for 60 seconds):
    vmstat 1 60 > vmstat.log
    
    The log includes CPU idle time, memory usage, and disk I/O.
  3. Nmon: Detailed resource monitoring (install via sudo apt install nmon):
    nmon -f -s 1 -c 60
    
    This generates a .nmon file (use nmon_analyzer to convert it into charts).

Best Practices for Effective Testing

Troubleshooting Common Issues

0
看了该问题的人还看了