ゴリラでもわかるエンジニアライフ

マイケル(子ゴリ)君とともにみなさんの成長のためにあるブログです

GatlingをIntelliJで開発するためのセットアップ方法

こんばんは、さばみそです

負荷テスト....それはサーバーサイドエンジニアが必ず通る道であり、SLAを満たせるかなどの性能保証されているかの確認をするためのテストですね

今回は、そんな負荷テストツールのGatling[ガトリング]のセットアップ方法をハンズオン形式で説明したいと思います

Gatlingとは

負荷テストツールで、一番有名なのは、JMeterだと思います

しかし、JMeterのデメリットとして動作が思いなどがあり、負荷テストサーバーのスペック上げないといけないみたいな場面に出会った人は少なくないと思います。

Gatlingでは、Scalaで書かれており、JMeterに比べて、実行が軽い、コードで書くからシナリオの見通しが良いというメリットがあります

Scalaか.....と苦手意識をもつ人もいるかと思いますが、安心してください

公式にも、以下が書かれており、ドキュメントが充実しているので、Scala初心者でも簡単に作成することができます

Gatling simulation scripts are written in Scala, but don’t panic! You can use all the basic functions of Gatling without knowing much about Scala. In most situations the DSL will cover most of your needs and you’ll be able to build your scenarios.

f:id:dankan-0101:20190820010909p:plain
Gatling

公式ドキュメント

https://gatling.io/docs/current/quickstart#quickstart

再現環境

TL;DR

  1. IntelliJ プロジェクト作成
  2. Create from archetype にチェックを入れて、 Add Archetype を選択
  3. GroupId: io.gatling.highcharts ArtifactId: gatling-highcharts-maven-archetype Version: 3.1.1 を入力して、OK(2019/06/10時点)
  4. 残りの手順は、通常通り進み、Finishを押す

セットアップ方法(図解)

IntelliJのプロジェクト作成
プロジェクトの作成

  1. まずIntelliJを起動して、新規プロジェクトを作成します

Create from archetype にチェックを入れて、 Add Archetype を選択
Create from archetype にチェックを入れて、 Add Archetype を選択

  1. Gatlingは、Mavenで実行されるので、Maveプロジェクトを作成します

この時に、Add Archetypeを押して、依存ライブラリにGatlingを追加します

ArchetypeにGatlingライブラリを追加
ArchetypeにGatlingライブラリを追加

各項目に、以下の値を入力して、OKを押して、Nextを押します

GroupId : io.gatling.highcharts

ArtifactId : gatling-highcharts-maven-archetype

Version : 3.1.1

プロジェクト設定
プロジェクト設定

ここでは、作成したいプロジェクトの設定を入力します

Gatlingライブラリの追加
Gatlingライブラリの追加

Add Archetypeで追加した、Gatlingライブラリの確認画面が出るので、何も変更せずNext

プロジェクト作成完了
プロジェクト作成完了

最後に、プロジェクト名などを確認し、問題なければFinishを押し、プロジェクト作成が完了です

Gatlingのbuild実行画面
Gatlingのbuild実行画面

プロジェクト作成後に、すぐGatlingのbuildが走り、必要なディレクトリやファイルが作成されます

ディレクトリ構成

└── src
    └── test
        ├── resources  ->  ここの配下に、設定ファイルなどを追加していく
        │   ├── bodies
        │   ├── data
        │   ├── gatling.conf
        │   ├── logback.xml
        │   └── recorder.conf
        └── scala  -> ここの配下に、負荷テストシナリオを追加していく
            ├── Engine.scala
            ├── IDEPathHelper.scala
            └── Recorder.scala

buildが成功すると、上記のようなディレクトリ構成が作成されます

負荷テストシナリオを、scala配下に追加していけば、実装が可能です!

負荷テストの実行

以下のコマンドを実行するだけで、負荷テストが実行でき、レポートが作成されます!

mvn gatling:test -Dgatling.simulationClass=XXXXSimulation

ハマったこと

たまに、以下のようなエラーが出て、buildに失敗する場合があります

Downloading: 
http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml 
Downloaded: 
http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml 
(701 B at 2.7 KB/sec) 
[INFO] 
[INFO] 
------------------------------------------------------------------------ 
[INFO] Building Maven Stub Project (No POM) 1 
[INFO] 
------------------------------------------------------------------------ 
[INFO] 
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ 
standalone-pom >>> 
[INFO] 
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ 
standalone-pom <<< 
[INFO] 
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ 
standalone-pom --- 
[INFO] Generating project in Batch mode 
[INFO] Archetype defined by properties 
Downloading: 
http://repo.maven.apache.org/maven2/com/excilys/ebi/gatling/highcharts/gatling-highcharts-maven-hetype/1.3.0/gatling-highcharts-maven-archetype-1.3.0.jar 
[INFO] 
------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] 
------------------------------------------------------------------------ 
[ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-archetype-plugin:2.2:generate 
(default-cli) on project standalone-pom: The desired archetype does 
not exist 
(com.excilys.ebi.gatling.highcharts:gatling-highcharts-maven-archetype:1.3.0) 
-> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with 
the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, 
please read the following articles: 
[ERROR] [Help 1] 
http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException 

そんな場合は、以下のコマンドを実行すれば解決できました

mvn archetype:generate -DarchetypeCatalog=http://repository.excilys.com/content/groups/public/archetype-catalog.xml

参考文献

Gatling: Step by step guide to IntelliJ integration · GitHub