LoginSignup
28
28

More than 5 years have passed since last update.

便利すぎる負荷試験テストツールGatlingの使い方~戻り値チェック編~

Last updated at Posted at 2015-12-15

Gatlingテストシリーズ
便利すぎる負荷試験テストツールGatlingの使い方~自力ソース編~
便利すぎる負荷試験テストツールGatlingの使い方~ブラウザ操作記憶編~

に続き最終編は戻り値チェックです。
基本的な使い方を知りたい方は以前の記事をチェックしてみてください。
負荷試験ツールなのに戻り値のチェックも出来るのかって??
出来るのです、そうGatlingならね。

まずはhttpステータスのチェック。

これは簡単です。
httpステータスが「200」である事のテストはこんな感じに書けばできます

package computerdatabase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class ApiSimulation extends Simulation {
  val httpConf = http
    .baseURL("http://localhost:8080") // Here is the root for all relative URLs
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // Here are the common headers
    .doNotTrackHeader("1")
    .acceptLanguageHeader("ja,en-US;q=0.7,en;q=0.3")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0")

  val scn = scenario("Sample")
    .exec(http("request_1")
      .get("/")
      .check(status.is(200)))
    .pause(100 milliseconds)

  setUp(scn.inject(rampUsers(10) over(5 seconds)) .protocols(httpConf))

}
.check(status.is(200))

が今までより追加されてますね。

続きまして、戻り値がjsonの場合の確認行ってみましょう。

Gatlingではjsonpathを使って、jsonの値チェックを行えます。

jsonpathについてはこちら
https://github.com/gatling/jsonpath

例えばこんなjsonが返ってくるAPIがあるとします

{
  "foo": 1,
  "bar": {"hoge": "baz" }
}

このjsonの値が正しいかチェックするには以下のように書きましょう

package computerdatabase

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class JsonSimulation extends Simulation {

  val httpConf = http
    .baseURL("http://localhost:8080/") // Here is the root for all relative URLs
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // Here are the common headers
    .doNotTrackHeader("1")
    .acceptLanguageHeader("ja,en-US;q=0.7,en;q=0.3")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0")

  val scn = scenario("ItemViewCount")
    .exec(
      http("request_1")
      .get("/")
      .check(status.is(200),
             jsonPath("$..foo").ofType[Int].is(1),
             jsonPath("$..bar.hoge").ofType[String].is("baz")
             ))

  setUp(scn.inject(rampUsers(10) over(5 seconds)) .protocols(httpConf))
}
             jsonPath("$..foo").ofType[Int].is(1),
             jsonPath("$..bar.hoge").ofType[String].is("baz")

ここでjsonの戻り値をチェックしています。
ん〜、いいですね!!

xmlの場合はxpath使えばチェック出来ます。

負荷試験できて、httpステータスの確認も出来て、値の確認も出来る。
もう負荷試験だけにかかわらず、acceptance testとかから使えそうですよね!

みなさん是非ともGatling使ってみてください。
こんなことも出来るよなどのフィードバックもお待ちしています!!

28
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
28