0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apache JMeterの使い方

Last updated at Posted at 2025-11-07

🎯 ゴール

Java サービスのエンドポイントに対して JMeter を使って負荷テストを実行

Maven コマンドでビルド&テストを自動化

mvn verify でテストレポートを生成

📁 ディレクトリ構成

load-test-sample/
├── pom.xml
├── src/
│   └── test/
│       └── jmeter/
│           └── simple-test.jmx
└── src/
    └── main/
        └── java/
            └── com/example/
                └── App.java

⚙️ 1. pom.xml 設定

Maven 用の JMeter プラグインを使用します。
ここでは jmeter-maven-plugin
を利用します。

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                            http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.example</groupId>
   <artifactId>load-test-sample</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>

   <properties>
       <maven.compiler.source>17</maven.compiler.source>
       <maven.compiler.target>17</maven.compiler.target>
       <jmeter.version>5.6.3</jmeter.version>
   </properties>

   <dependencies>
       <!-- テスト対象のJavaアプリが依存しているライブラリをここに追加 -->
   </dependencies>

   <build>
       <plugins>
           <!-- JMeter Maven Plugin -->
           <plugin>
               <groupId>com.lazerycode.jmeter</groupId>
               <artifactId>jmeter-maven-plugin</artifactId>
               <version>3.8.0</version>
               <executions>
                   <execution>
                       <id>jmeter-tests</id>
                       <phase>verify</phase>
                       <goals>
                           <goal>jmeter</goal>
                       </goals>
                   </execution>
               </executions>
               <configuration>
                   <testFilesIncluded>
                       <jMeterTestFile>simple-test.jmx</jMeterTestFile>
                   </testFilesIncluded>
                   <resultsFileFormat>xml</resultsFileFormat>
                   <resultsOutputIsCSV>false</resultsOutputIsCSV>
               </configuration>
           </plugin>
       </plugins>
   </build>
</project>

🧪 2. サンプル JMeter テスト (src/test/jmeter/simple-test.jmx)

以下は、HTTP GET リクエストを 10 ユーザー × 5 ループで送るサンプルです。

src/test/jmeter/simple-test.jmx
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.6.3">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Sample Test Plan" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="HTTP Load Test" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <stringProp name="LoopController.loops">5</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">10</stringProp>
        <stringProp name="ThreadGroup.ramp_time">3</stringProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
      </ThreadGroup>
      <hashTree>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="GET /api/hello" enabled="true">
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments">
            <collectionProp name="Arguments.arguments"/>
          </elementProp>
          <stringProp name="HTTPSampler.domain">localhost</stringProp>
          <stringProp name="HTTPSampler.port">8080</stringProp>
          <stringProp name="HTTPSampler.protocol">http</stringProp>
          <stringProp name="HTTPSampler.path">/api/hello</stringProp>
          <stringProp name="HTTPSampler.method">GET</stringProp>
          <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
        </HTTPSamplerProxy>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

🧱 3. テスト対象の Java アプリ(例)

sample.java
package com.example;

import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class App {
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/api/hello", exchange -> {
            String response = "Hello from Java!";
            exchange.sendResponseHeaders(200, response.length());
            try (OutputStream os = exchange.getResponseBody()) {
                os.write(response.getBytes());
            }
        });
        server.start();
        System.out.println("Server running on http://localhost:8080/api/hello");
    }
}

🚀 4. 実行手順

# サーバー起動
mvn compile exec:java -Dexec.mainClass="com.example.App"

# 別ターミナルで負荷テスト実行
mvn verify

📊 5. 結果出力

target/jmeter/results/

・XML形式またはCSV形式のレポート
・HTMLレポートを出したい場合は以下を追加:

<configuration>
    <generateReports>true</generateReports>
</configuration>

結果は:

target/jmeter/reports/simple-test.html

で確認できます。

サイト

Apache JMeter公式サイト

Gatlingのバージョン(安定)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?