Gatlingでシナリオテストを書く
- Apr 09, 2019
- AWS
takeokunn.xyzの高速化にあたって、せっかくだからシナリオテストも書いてみるかと思い立ち実際にやってみた。
シナリオテスト用のオープンソースはたくさん転がっているのだが、ノリと勘で Gattling
を選んだ。
公式ページ: https://gatling.io/
Gatling is a highly capable load testing tool. It is designed for ease of use, maintainability and high performance.
scala
で書かれたツール。 Enterprise向けのサービスもやっているみたいだが、基本はオープンソース。
使い方はいたって簡単。 BasicSimulation
や AdvancedSimulation
などにテストしたい項目を書いて sh bin/gatling.sh
を叩けばあとは勝手に動いてくれる。
実行結果はグラフィカルに見ることができ、中々面白い。
実際に takeokunn.xyz に対してシナリオテストをしてみた。
package computerdatabase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class BasicSimulation extends Simulation {
val httpProtocol = http
.baseUrl("https://takeokunn.xyz")
.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")
val scn = scenario("BasicSimulation")
.exec(http("top").get("/"))
.pause(5)
.exec(http("page1").get("/blog/post/publish-npm-library-build-format"))
.pause(10)
.exec(http("page2").get("/blog/post/mysql-localhost-127001"))
.pause(10)
.exec(http("page3").get("/blog/post/docker-node-express"))
setUp(
scn.inject(
atOnceUsers(10),
rampUsers(500) during (10 minutes)
).protocols(httpProtocol)
)
}
scenario
は細かく制御できる。
inject部分はドキュメントによると以下
https://gatling.io/docs/current/general/simulation_setup/
1. nothingFor(duration): Pause for a given duration.
2. atOnceUsers(nbUsers): Injects a given number of users at once.
3. rampUsers(nbUsers) during(duration): Injects a given number of users with a linear ramp over a given duration.
4. constantUsersPerSec(rate) during(duration): Injects users at a constant rate, defined in users per second, during a given duration. Users will be injected at regular intervals.
5. constantUsersPerSec(rate) during(duration) randomized: Injects users at a constant rate, defined in users per second, during a given duration. Users will be injected at randomized intervals.
6. rampUsersPerSec(rate1) to (rate2) during(duration): Injects users from starting rate to target rate, defined in users per second, during a given duration. Users will be injected at regular intervals.
7. rampUsersPerSec(rate1) to(rate2) during(duration) randomized: Injects users from starting rate to target rate, defined in users per second, during a given duration. Users will be injected at randomized intervals.
8. heavisideUsers(nbUsers) during(duration): Injects a given number of users following a smooth approximation of the heaviside step function stretched to a given duration.
全部使ったわけではないのでわからないが、細かく設定できるようだ。
リザルト画面かっこいい
参考記事