Gatlingのインストールと動作確認ができたら、簡単なテストケースを作成してテストを実行してみます。
CSVを使用する場合は以下を参照
テストケース基礎
テストケースを記述したファイルを置くディレクトリは、デフォルトでは以下のなります。
user-files/simulations/computerdatabase/
このディレクトリを確認すると以下のようになっています。
$ ls -al user-files/simulations/computerdatabase/ 合計 16 drwxr-xr-x 3 vagrant vagrant 106 2月 12 19:15 . drwxr-xr-x 3 vagrant vagrant 29 8月 30 19:15 .. -rw-r--r-- 1 vagrant vagrant 2635 2月 12 19:14 BasicSimulation.scala drwxr-xr-x 2 vagrant vagrant 4096 8月 30 19:15 advanced
Gatlingを実行したとき、最初に以下のような選択が表示されますが、これはこのディレクトリからscalaファイルを見つけて表示しています。 そのため、テストケースをここに作成すれば、Gatlingの設定を変更せずに、自作のテストケースを実行できるようになります。
Choose a simulation number:
[0] computerdatabase.BasicSimulation
[1] computerdatabase.advanced.AdvancedSimulationStep01
[2] computerdatabase.advanced.AdvancedSimulationStep02
[3] computerdatabase.advanced.AdvancedSimulationStep03
[4] computerdatabase.advanced.AdvancedSimulationStep04
[5] computerdatabase.advanced.AdvancedSimulationStep05
簡単なテストケース作成
テストケースは、BasicSimulation.scalaを基に作成してみます。 BasicSimulation.scalaを修正しても良いのですが、ここではBasicSimulation.scalaをコピーしてMySimulation.scalaを作成します。
cd user-files/simulations/computerdatabase/ cp BasicSimulation.scala MySimulation.scala
-
簡単なテストケース
BasicSimulation.scalaは、ちょっと長いので、できるだけ短くします。 今回は以下のようなテストケースとします。
例 user-files/simulations/computerdatabase/MySimulation.scala
package computerdatabase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class MySimulation extends Simulation {
val host = "http://127.0.0.1" // 接続先サーバー
val httpConf = http
.baseURL(host)
.userAgentHeader("Gatling Test") // ユーザーエージェント指定
val scn = scenario("Test Scenario")
.exec(http("test_request")
.get("/")
.check(status.is(200))) // HTTPステータスが200なら成功
setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}
このテストケースは以下のような設定です。
- 接続先はローカルWebサーバーの"http://127.0.0.1"
- アクセスするときのユーザーエージェントは、"Gatling Test"
- トップページの"/"にGETでアクセス
- HTTPステータスが200ならテスト成功
- アクセス回数は1回だけ
実行と結果確認
正常に実行できると以下のようになります。
$ bin/gatling.sh
GATLING_HOME is set to /home/vagrant/gatling-charts-highcharts-bundle-2.3.0
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Choose a simulation number:
[0] computerdatabase.BasicSimulation
[1] computerdatabase.MySimulation
[2] computerdatabase.advanced.AdvancedSimulationStep01
[3] computerdatabase.advanced.AdvancedSimulationStep02
[4] computerdatabase.advanced.AdvancedSimulationStep03
[5] computerdatabase.advanced.AdvancedSimulationStep04
[6] computerdatabase.advanced.AdvancedSimulationStep05
1
Select simulation id (default is 'mysimulation'). Accepted characters are a-z, A-Z, 0-9, - and _
Select run description (optional)
Simulation computerdatabase.MySimulation started...
================================================================================
2018-02-12 20:24:43 3s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=1 KO=0 )
> test_request (OK=1 KO=0 )
---- Test Scenario -------------------------------------------------------------
[##########################################################################]100%
waiting: 0 / active: 0 / done:1
================================================================================
Simulation computerdatabase.MySimulation completed in 1 seconds
Parsing log file(s)...
Parsing log file(s) done
Generating reports...
================================================================================
---- Global Information --------------------------------------------------------
> request count 1 (OK=1 KO=0 )
> min response time 1887 (OK=1887 KO=- )
> max response time 1887 (OK=1887 KO=- )
> mean response time 1887 (OK=1887 KO=- )
> std deviation 0 (OK=0 KO=- )
> response time 50th percentile 1887 (OK=1887 KO=- )
> response time 75th percentile 1887 (OK=1887 KO=- )
> response time 95th percentile 1887 (OK=1887 KO=- )
> response time 99th percentile 1887 (OK=1887 KO=- )
> mean requests/sec 0.5 (OK=0.5 KO=- )
---- Response Time Distribution ------------------------------------------------
> t < 800 ms 0 ( 0%)
> 800 ms < t < 1200 ms 0 ( 0%)
> t > 1200 ms 1 (100%)
> failed 0 ( 0%)
================================================================================
Reports generated in 0s.
Please open the following file: /home/vagrant/gatling-charts-highcharts-bundle-2.3.0/results/mysimulation-1518434680263/index.html
アクセスログを確認して、ユーザーエージェントが"Gatling Test"のアクセスが記録されているかを確認します。 CentOSのapacheでは、アクセスログは以下になります。
/var/log/httpd/access_logGatlingからのアクセスが正常に行われていれば、アクセスログに以下のようなログが在るはずです。
127.0.0.1 - - [12/Feb/2018:20:24:41 +0900] "GET / HTTP/1.1" 200 3691 "-" "Gatling Test"
このログでは、最終項目がユーザーエージェントで、"Gatling Test"になっています。
実行結果
実行結果のindex.htmlは以下のようになります。 1回だけのアクセスで、テスト結果がOK、レスポンスタイムは1887msとなっています。
次:実行スクリプト
