はじめての Gatling
- 2. 目次
• Gatling とは
• Gatling のインストール方法
• Gatling レコーダー
• Gatling の使い方
• Gatling のレポート
• Gatling の実例 Tips
- 3. Gatling とは
• http://gatling.io
• Scala ベースの高負荷テストフレームワーク
• ab や wrk のようなベンチマークに近いが、Scala でロ
ジックを記述することができる
• ab (Apache Bench):
• https://httpd.apache.org/docs/2.4/programs/
ab.html
• wrk:
• https://github.com/wg/wrk
- 8. レコーダーで生成されるシナリオの例
package computerdatabase // パッケージ名
import io.gatling.core.Predef._ // 必要なインポートたち
import io.gatling.http.Predef._
import scala.concurrent.duration._
class BasicSimulation extends Simulation { // クラス定義、Simulation を敬称しないといけない
val httpConf = http // HTTP リクエストの共通設定
.baseURL("http://computer-database.gatling.io") // リクエスト対象の URL
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // 共通の HTTP ヘッダー
.doNotTrackHeader("1") // DNT ヘッダーの送信
.acceptLanguageHeader("en-US,en;q=0.5") // 受け入れる言語ヘッダーの送信
.acceptEncodingHeader("gzip, deflate") // エンコーディングヘッダーの送信
.userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0") // ユーザエージェントの送信
val scn = scenario("BasicSimulation") // テストシナリオの定義
.exec(http("request_1") // HTTP リクエスト名、"request_1" は最終レポートに表示される
.get("/")) // URI / へ GET リクエストする
.pause(5) // 一時スリープタイム
setUp( // このシミュレーションで実行するシナリオのセットアップ
scn.inject(atOnceUsers(1)) // scn という名前で 1 シングルユーザでリクエストする
).protocols(httpConf) // 最初の HTTP リクエストの共通設定を付与する
}
- 10. 実行結果
• あわせて results 以下に HTML ファイルとして出力され
る
http://gatling.io/docs/2.1.1/general/reports.html
- 13. ある条件のときのみリクエストをする
• scenarioProperty が pv のときリクエストをする
val scenarioProperty = System.getProperty("scenario",
"").toString
val scn = scenario("DoIfBenchmark")
.doIf(scenarioProperty, "pv") {
exec(http("pv").get("/pv"))
}
- 14. ある割合ごとにリクエストをする
• / を 50%、/foo を 20%、/bar を 30% ずつリクエスト
する
val scn = scenario("RandomBenchmark")
.randomSwitch(
50.0 -> exec(http("top").get("/")),
20.0 -> exec(http("foo").get("/foo")),
30.0 -> exec(http("bar").get("/bar"))
)
- 15. リクエストに動的にクッキーを付与する
• / のクッキー uid を /cookie へ付与する
val scn = scenario("CookieBenchmark")
.exec(http("top")
.get("/")
.check(headerRegex("Set-Cookie", "uid=(.*);
Expires=.*; Domain=.*; Path=.*").saveAs("uid")))
.exec(addCookie(Cookie("uid", "${uid}")))
.exec(http("cookie").get("/"))
- 16. リクエストに動的にクッキーを付与する
• /baz へのリクエストは "bar UA" にする
val userAgent = Map("User-Agent" -> "bar UA")
val scn = scenario("UABenchmark")
.exec(http("baz")
.get("/baz")
.headers(userAgent)
)
- 17. ステータスコード 303 はステータス OK とする
• /redirect は 303 を返して、リダイレクトはしない
val scn = scenario("RedirectBenchmark")
.exec(http("top")
.get("/redirect")
.disableFollowRedirect
.check(status.is(303))
)