🔫

MavenではじめるGatling生活

2022/03/19に公開

はじめに

負荷テストを行うツールとして有名なGatlingについて、今回はMaven経由で実行する方法や、実行可能なJarファイルの作成方法について紹介します。

動作環境

各種バージョン

  • Windows (WSL2 Ubuntu 18.04.6 LTS)
  • openjdk 11.0.13
  • Apache Maven 3.6.0

ディレクトリ構成

├── pom.xml (mavenの設定ファイル)
├── results (テスト結果の格納先)
├── src (Gatlingスクリプトの本体)
│   └── main
│       ├── resources
│       │   ├── gatling.conf
│       │   └── logback-test.xml
│       └── scala
│           └── simulation
│               └── PeakModel.scala (メインのScalaファイル)
└── target (ビルドされたソースの格納先)

pom.xml

基本設定

packagingはjarを指定

dependencyには公式サイトを参考に以下を追加する

  • gatling-charts-highcharts
<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>org.xxxx</groupId>
    <artifactId>gatling</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <gatling.version>3.5.1</gatling.version>
        <gatling-maven-plugin.version>3.1.2</gatling-maven-plugin.version>
        <maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
        <maven-shade-plugin.version>3.2.4</maven-shade-plugin.version>
        <scala-maven-plugin.version>4.5.3</scala-maven-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.gatling.highcharts</groupId>
            <artifactId>gatling-charts-highcharts</artifactId>
            <version>${gatling.version}</version>
        </dependency>
    </dependencies>

    <build>
      ~後述~
    </build>

</project>

Pluginの設定

公式ページにはこちらのGithubを参考にしてと書いてあるので、まず以下の3つを追加する。

  • maven-jar-plugin
  • scala-maven-plugin
  • gatling-maven-plugin
<build>
    <testSourceDirectory>src/main/scala</testSourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>${maven-jar-plugin.version}</version>
        </plugin>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>${scala-maven-plugin.version}</version>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                    <configuration>
                        <jvmArgs>
                            <jvmArg>-Xss100M</jvmArg>
                        </jvmArgs>
                        <args>
                            <arg>-target:jvm-1.8</arg>
                            <arg>-deprecation</arg>
                            <arg>-feature</arg>
                            <arg>-unchecked</arg>
                            <arg>-language:implicitConversions</arg>
                            <arg>-language:postfixOps</arg>
                        </args>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-maven-plugin</artifactId>
            <version>${gatling-maven-plugin.version}</version>
            <configuration>
                <configFolder>src/main/resources</configFolder>
                <resultsFolder>target/gatling/results</resultsFolder>
                <simulationsFolder>src/main/scala/simulation</simulationsFolder>
            </configuration>
        </plugin>
    </plugins>
</build>

さらに、今回はmavenから実行ができるだけでなく、実行可能なJarファイルとしてパッケージ化したいので、

  • maven-shade-plugin

も追加した。

Jarにマージされる際に多くのWarningが出るため、こちらを参考にさせて頂き、除外ファイルを指定する。

<!-- 省略 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>${maven-shade-plugin.version}</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>io.gatling.app.Gatling</mainClass>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer">
                        <addHeader>false</addHeader>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.properties.PropertiesTransformer">
                        <resource>META-INF/io.netty.versions.properties</resource>
                        <alreadyMergedKey>already_merged</alreadyMergedKey>
                    </transformer>
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>LICENSE</exclude>
                            <exclude>NOTICE</exclude>
                            <exclude>module-info.class</exclude>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                            <exclude>META-INF/*.MF</exclude>
                            <exclude>META-INF/versions/*/module-info.class</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

Jarファイル作成方法

mvnコマンドからpackageするだけ

$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< org.xxx:gatling >--------------------------
[INFO] Building gatling 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gatling ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- scala-maven-plugin:4.5.3:add-source (scala-compile-first) @ gatling ---

~中略~

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  35.625 s
[INFO] Finished at: 2022-03-19T12:14:15+09:00
[INFO] ------------------------------------------------------------------------

実行方法

Mavenから実行

simulationClassを指定する

$ mvn gatling:test -Dgatling.simulationClass=simulation.PeakModel
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< org.xxx:gatling >--------------------------
[INFO] Building gatling 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- gatling-maven-plugin:3.1.2:test (default-cli) @ gatling ---

~中略~

Simulation simulation.PeakModel started...

================================================================================
2022-03-19 12:11:57                                           5s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=0      KO=163   )
> group1 / TOP PAGE / TOP_post_pickup                      (OK=0      KO=97    )
> group2 / TOP PAGE / TOP_post_pickup                      (OK=0      KO=66    )
---- Errors --------------------------------------------------------------------
> jsonPath($.success).find.is(true) preparation crashed: Jackson    163 (100.0%)
 failed to parse into a valid AST: c.f.j.c.JsonParseException:...

---- シナリオ1 ---------------------------------------------------------------------
          active: 0      / done: 97
---- シナリオ2 ---------------------------------------------------------------------
          active: 1      / done: 66
================================================================================

Jarファイルから実行

-sでクラス名を指定する

$ java -jar target/gatling-1.0.0.jar -s simulation.PeakModel
Simulation simulation.PeakModel started...

================================================================================
2022-03-19 12:15:37                                           5s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=0      KO=98    )
> group1 / TOP PAGE / TOP_post_pickup                      (OK=0      KO=39    )
> group2 / TOP PAGE / TOP_post_pickup                      (OK=0      KO=59    )
---- Errors --------------------------------------------------------------------
> jsonPath($.success).find.is(true) preparation crashed: Jackson     98 (100.0%)
 failed to parse into a valid AST: c.f.j.c.JsonParseException:...

---- シナリオ1 ---------------------------------------------------------------------
          active: 1      / done: 39
---- シナリオ2 ---------------------------------------------------------------------
          active: 1      / done: 59
================================================================================

Discussion