温馨提示×

ubuntu jmeter压力测试

小樊
39
2025-09-25 11:06:00
栏目: 智能运维

Installing Java Environment
JMeter is a Java-based tool, so installing OpenJDK (or Oracle JDK) is mandatory. For Ubuntu, run the following commands to install OpenJDK 11 (a commonly used LTS version):

sudo apt update sudo apt install openjdk-11-jdk 

Verify installation with:

java -version 

This should display the installed Java version (e.g., openjdk version "11.0.xx").

Downloading and Installing JMeter

  1. Download the latest stable JMeter binary from the official Apache website. Use wget to fetch the tarball (replace 5.6.2 with the latest version):
    wget https://dlcdn.apache.org/jmeter/binaries/apache-jmeter-5.6.2.tgz 
  2. Extract the tarball to a directory like /opt (for system-wide access) or /usr/local:
    sudo tar -xvzf apache-jmeter-5.6.2.tgz -C /opt 
  3. (Optional but recommended) Configure environment variables for easy access. Edit ~/.bashrc (or /etc/profile for system-wide effect) and add:
    export JMETER_HOME=/opt/apache-jmeter-5.6.2 export PATH=$JMETER_HOME/bin:$PATH 
    Reload the file to apply changes:
    source ~/.bashrc 
  4. Verify JMeter installation by checking its version:
    jmeter -v 
    You should see JMeter’s version, Java version, and other details.

Creating a Basic Test Plan
A test plan defines the load scenario (e.g., number of users, requests). Here’s how to create one via the JMeter GUI (graphical interface):

  1. Launch JMeter: Run jmeter from the terminal (GUI mode).
  2. Add a Thread Group: Right-click “Test Plan” → “Add” → “Threads (Users)” → “Thread Group”. Configure:
    • Number of Threads (users): Simulated concurrent users (e.g., 50).
    • Ramp-up Period (seconds): Time to start all threads (e.g., 10 seconds for gradual load).
    • Loop Count: Times each thread repeats the test (e.g., 1 for a single run).
  3. Add an HTTP Request: Right-click the Thread Group → “Add” → “Sampler” → “HTTP Request”. Set:
    • Server Name or IP: Your target server’s address (e.g., example.com).
    • Port Number: Server port (e.g., 80 for HTTP, 443 for HTTPS).
    • Path: Endpoint to test (e.g., /api/login for a login API).
  4. Add Listeners: Listeners collect and display results. Right-click the Thread Group → “Add” → “Listener” → choose:
    • Aggregate Report: Shows key metrics (avg response time, throughput, error rate).
    • View Results Tree: Displays detailed request/response data (useful for debugging).
      Save the test plan as a .jmx file (e.g., my_test_plan.jmx) using “File” → “Save”.

Running JMeter in Non-GUI Mode (Recommended for Load Testing)
The GUI mode is resource-intensive and not suitable for high-load tests. Use the command line instead:

jmeter -n -t /path/to/your_test_plan.jmx -l /path/to/results.jtl 
  • -n: Non-GUI mode (no GUI window).
  • -t: Path to the test plan file.
  • -l: Path to save results (.jtl file, a CSV-formatted log of all requests).

Generating HTML Reports
To visualize results in an easy-to-read HTML format, run the following command after the test completes:

jmeter -e -o /path/to/report/directory 
  • -e: Generate HTML report after test execution.
  • -o: Directory to save the report (must be empty or non-existent).
    The report includes dashboards for response times, throughput, errors, and more. Open the index.html file in a browser to view it.

Distributed Load Testing (Optional)
For simulating massive loads (thousands of users), use multiple machines (master-slave setup):

  1. Install JMeter on all machines (master + slaves) and ensure they have the same version.
  2. Configure Slave Nodes: On each slave, edit jmeter.properties (in the bin directory) and set:
    server.rmi.ssl.disable=true # Disable SSL for simplicity (not recommended for production) server_port=1099 # Port for RMI communication 
  3. Configure Master Node: On the master, edit jmeter.properties and add slave IPs to remote_hosts:
    remote_hosts=slave1_ip:1099,slave2_ip:1099 
  4. Start Slaves: On each slave, run:
    jmeter-server 
  5. Run Distributed Test: On the master, execute:
    jmeter -n -t /path/to/test_plan.jmx -l /path/to/results.jtl -r 
    -r: Start all remote slaves specified in remote_hosts.

Tips for Effective Testing

  • Use Variables for Dynamic Data: Replace hardcoded values (e.g., usernames, passwords) with variables (e.g., ${username}) and use CSV Data Set Config to read from a file.
  • Monitor Server Resources: Use tools like top, htop, or vmstat on the target server to track CPU, memory, and disk usage during the test.
  • Run Multiple Iterations: Execute tests multiple times to account for variability and get reliable averages.
  • Avoid Overloading the Master: The master machine should have enough resources (CPU, memory) to coordinate the test; avoid running slaves on the same machine as the master.

0