Thực thi kịch bản JMeter tự động qua Maven Plugin

Để chạy kiểm thử hiệu năng với JMeter, thông thường cần cài đặt thủ công và giải nén công cụ. Giải pháp tích hợp jmeter-maven-plugin cho phép thực thi trực tiếp file JMX thông qua lệnh mvn verify, loại bỏ bước cài đặt phụ thuộc và hỗ trợ tự động hóa quy trình kiểm thử trên môi trường CI/CD. Hướng dẫn triển khai chi tiết như sau:

Cấu hình dự án Maven

Tạo dự án mới với các dependency cần thiết và khai báo plugin trong phần build. Lưu ý sử dụng phiên bản tương thích với JMeter 5.6.2:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>performance-testing</artifactId>
  <version>1.0</version>

  <dependencies>
    <dependency>
      <groupId>org.apache.jmeter</groupId>
      <artifactId>jmeter-core</artifactId>
      <version>5.6.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jmeter</groupId>
      <artifactId>jmeter-http</artifactId>
      <version>5.6.2</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>com.lazerycode.jmeter</groupId>
        <artifactId>jmeter-maven-plugin</artifactId>
        <version>3.5.0</version>
        <executions>
          <execution>
            <id>run-performance-tests</id>
            <goals>
              <goal>performance-test</goal>
            </goals>
            <configuration>
              <testResultsTimestamp>true</testResultsTimestamp>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Tổ chức kịch bản kiểm thử

Đặt file JMX trong thư mục src/test/jmeter. Ví dụ kịch bản kiểm tra tải cho trang chủ Baidu:

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2">
  <hashTree>
    <TestPlan testname="Load Test Plan" enabled="true">
      <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup testname="Load Test Group" enabled="true">
        <elementProp name="ThreadGroup.main_controller">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <stringProp name="LoopController.loops">200</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">20</stringProp>
        <stringProp name="ThreadGroup.ramp_time">5</stringProp>
      </ThreadGroup>
      <hashTree>
        <HTTPSamplerProxy testname="Baidu Homepage Check" enabled="true">
          <stringProp name="HTTPSampler.domain">www.baidu.com</stringProp>
          <stringProp name="HTTPSampler.method">GET</stringProp>
        </HTTPSamplerProxy>
        <ResponseAssertion testname="HTTP 200 Validator">
          <collectionProp name="Asserion.test_strings">
            <stringProp name="test_string">200</stringProp>
          </collectionProp>
          <stringProp name="Assertion.test_field">Assertion.response_code</stringProp>
        </ResponseAssertion>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

Quy trình thực thi và phân tích kết quả

Sử dụng lệnh mvn verify để khởi chạy kiểm thử (yêu cầu Maven 3.8.1+). Plugin sẽ tự động:

  • Tải JMeter phiên bản tương ứng
  • Thực thi tất cả kịch bản trong thư mục src/test/jmeter
  • Xuất báo cáo dưới dạng HTML và CSV

Kết quả được lưu trong thư mục target/jmeter với cấu trúc:

  • reports/: Báo cáo HTML chi tiết về chỉ số hiệu năng
  • results/: Dữ liệu thô định dạng CSV cho phân tích sâu
  • testFiles/: Bản sao kịch bản JMX đã thực thi

Thông số hiệu năng chính như throughput, latency, và tỷ lệ lỗi được hiển thị trực quan trong báo cáo, giúp đánh giá nhanh hiệu suất ứng dụng dưới tải trọng xác định.

Thẻ: Maven jmeter performance-testing continuous-integration

Đăng vào ngày 6 tháng 6 lúc 22:00