勉強メモ5_アップされてる記事を元にgatling構築

# gatlingを使うため、ApacheとPHP導入

以下↓↓の記事を参考
Apache導入
https://qiita.com/sango/items/b045a1da17606eda6f82
PHP導入
https://qiita.com/sango/items/a08c5b04df7125aaaad3
https://techacademy.jp/magazine/23054

```php
---------------Apache導入↓↓-------------------------
#ルートに変更
su - root
#apacheインストール
yum -y install httpd
#apache起動
systemctl start httpd
#apacheが動いてるかブラウザで確認
http://127.0.0.1/
```

```php
---------------PHP導入↓↓-------------------------
#PHPインストール
yum -y install php
#phpファイル作成するため、移動
cd /var/www/html/
```

/var/www/html/にPHPファイルを3つ作成

```php:api1.php
<?php
// 連想配列用意
$array = [
    'tokyo' => [
        '品川',
        '五反田',
        '三軒茶屋',
        '四谷'
    ]
];

// Origin null is not allowed by Access-Control-Allow-Origin.とかのエラー回避の 為、ヘッダー付与
header("Access-Control-Allow-Origin: *");

#echo json_encode($array);
echo json_encode($array,JSON_UNESCAPED_UNICODE);
?>
```
```php:api2.php
<?php
// 連想配列用意
$array = [
    'kanagawa' => [
        '横浜',
        '相模原',
        '湘南',
        '鎌倉'
    ]
];

// Origin null is not allowed by Access-Control-Allow-Origin.とかのエラー回避の 為、ヘッダー付与
header("Access-Control-Allow-Origin: *");

#echo json_encode($array);
echo json_encode($array,JSON_UNESCAPED_UNICODE);
?>
```

```php:api3.php
?php
// 連想配列用意
$array = [
    'saitama' => [
        '所沢',
        '狭山',
        '川口',
        '浦和',
        '小手指',
        '飯能'
    ]
];

// Origin null is not allowed by Access-Control-Allow-Origin.とかのエラー回避の 為、ヘッダー付与
header("Access-Control-Allow-Origin: *");

#echo json_encode($array);
echo json_encode($array,JSON_UNESCAPED_UNICODE);
?>
```
PHP実行

```php
#まずはApache再起動
systemctl restart httpd
#ブラウザでアクセス（JSON形式で市の名称がでればOK）
http://127.0.0.1/api1.php
http://127.0.0.1/api2.php
http://127.0.0.1/api3.php
```

#gatling 構築

以下の記事を参考に構築
https://medium.com/eureka-engineering/gatring-stress-test-14ac5efdfcbc

```php
#rootフォルダ移動
su - root
cd /root
#gatlingのライブラリーをダウンロード
curl -O https://repo1.maven.org/maven2/io/gatling/highcharts/gatling-charts-highcharts-bundle/3.3.1/gatling-charts-highcharts-bundle-3.3.1-bundle.zip
#gatlingのライブラリーを解凍
unzip gatling-charts-highcharts-bundle-3.3.1-bundle.zip
#javaがインストールされてるか確認(java8や11がないと動かないらしい)
java -version
#gatlingの実行ソース作成(BasicSimulation.scalaのベースの実行ファイルをコピー)
cd gatling-charts-highcharts-bundle-3.3.1/user-files/simulations/
mkdir api
cp -p computerdatabase/BasicSimulation.scala api/ApiSimulation.scala
```
上記でコピーした（ApiSimulation.scala）を作成

```scala
package api　//パッケージ名指定

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration.
_
class ApiSimulation extends Simulation {

val httpProtocol = http
 .baseUrl("http://127.0.0.1")//ベースURL
 .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
 .doNotTrackHeader("1")
 .acceptLanguageHeader("en-US,en;q=0.5")
 .acceptEncodingHeader("gzip, deflate")
 .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")

//7秒おきに /api1.php /api2.php /api3.phpにGETするだけの簡単なテスト
val scn = scenario("api")
 .exec(
 http("request_1")
 .get("/api1.php")//作成したPHPファイル指定
 )
 .pause(7)
 .exec(
 http("request_2")
 .get("/api2.php")//作成したPHPファイル指定
 )
 .pause(7)
 .exec(
 http("request_3")
 .get("/api3.php")//作成したPHPファイル指定
 )
setUp(scn.inject(atOnceUsers(1)).protocols(httpProtocol)) //1Userで並列実行
}
```
gatling実行

```php
#gatling実行シェルファイルに移動
cd /root/gatling-charts-highcharts-bundle-3.3.1/bin/
#gatling実行
./gatling.sh
GATLING_HOME is set to /root/gatling-charts-highcharts-bundle-3.3.1
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] api.ApiSimulation
     [1] computerdatabase.BasicSimulation
     [2] computerdatabase.advanced.AdvancedSimulationStep01
     [3] computerdatabase.advanced.AdvancedSimulationStep02
     [4] computerdatabase.advanced.AdvancedSimulationStep03
     [5] computerdatabase.advanced.AdvancedSimulationStep04
     [6] computerdatabase.advanced.AdvancedSimulationStep05
0 ←0を選択する
Select run description (optional)
api:　←api:って入力する
Simulation api.ApiSimulation started...

================================================================================
2020-09-27 16:20:23                                           5s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=1      KO=0     )
> request_1                                                (OK=1      KO=0     )

---- api -----------------------------------------------------------------------
[--------------------------------------------------------------------------]  0%
          waiting: 0      / active: 1      / done: 0
================================================================================


================================================================================
2020-09-27 16:20:28                                          10s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=2      KO=0     )
> request_1                                                (OK=1      KO=0     )
> request_2                                                (OK=1      KO=0     )

---- api -----------------------------------------------------------------------
[--------------------------------------------------------------------------]  0%
          waiting: 0      / active: 1      / done: 0
================================================================================


================================================================================
2020-09-27 16:20:32                                          14s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=3      KO=0     )
> request_1                                                (OK=1      KO=0     )
> request_2                                                (OK=1      KO=0     )
> request_3                                                (OK=1      KO=0     )

---- api -----------------------------------------------------------------------
[##########################################################################]100%
          waiting: 0      / active: 0      / done: 1
================================================================================

Simulation api.ApiSimulation completed in 14 seconds
Parsing log file(s)...
Parsing log file(s) done
Generating reports...

================================================================================
---- Global Information --------------------------------------------------------
> request count                                          3 (OK=3      KO=0     )
> min response time                                      4 (OK=4      KO=-     )
> max response time                                     60 (OK=60     KO=-     )
> mean response time                                    26 (OK=26     KO=-     )
> std deviation                                         24 (OK=24     KO=-     )
> response time 50th percentile                         14 (OK=14     KO=-     )
> response time 75th percentile                         37 (OK=37     KO=-     )
> response time 95th percentile                         55 (OK=55     KO=-     )
> response time 99th percentile                         59 (OK=59     KO=-     )
> mean requests/sec                                    0.2 (OK=0.2    KO=-     )
---- Response Time Distribution ------------------------------------------------
> t < 800 ms                                             3 (100%)
> 800 ms < t < 1200 ms                                   0 (  0%)
> t > 1200 ms                                            0 (  0%)
> failed                                                 0 (  0%)
================================================================================

Reports generated in 0s.
Please open the following file: /root/gatling-charts-highcharts-bundle-3.3.1/results/apisimulation-20200927072016312/index.html
```
上記の最後に「file: /root/gatling-charts-highcharts-bundle-3.3.1/results/apisimulation-20200927072016312/index.html」
と記載されているので、ブラウザでアクセスしてみる。
アクセスすると、下記みたいなのが表示されたよ。
![キャプチャ.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/730323/c5d98f8e-a0d7-d968-c2e8-a3ff90d8260f.jpeg)

一旦、ここまでで終了