如何在加特林从csv文件注入数据?

时间:2018-09-02 16:46:56

标签: scala scalatest gatling scala-gatling

我在实施加特林场景时遇到问题。在Scala DSL上,也许有人已经遇到了这个问题?

目标:我们需要在out场景中注入数据。 基本上,我们有 -我们的页面http://ourPage.com/像所有相对URL的根一样 -CSV文件中的URL列表,这些URL基本上是来自Oracle数据库的650000 id,结合使用(root + Urls)将模拟我们选择的用户数。

如何在Gatling中从CSV文件注入数据?

将该文件包含在正确的目录(数据)中,并且在其中包含正确的数据

希望我的信息可以理解 我将不胜感激

Log:

18:42:54.456 [ERROR] i.g.c.ZincCompiler$ - C:\Users\nikol\OneDrive\Desktop\gatling-charts-highcharts-bundle-2.3.1\user-files\simulations\computerdatabase\BasicSimulation.scala:37: not found: value Article_ID
18:42:54.458 [ERROR] i.g.c.ZincCompiler$ -   feed(csv(Article_ID.csv))
18:42:54.459 [ERROR] i.g.c.ZincCompiler$ -            ^
18:42:54.584 [ERROR] i.g.c.ZincCompiler$ - C:\Users\nikol\OneDrive\Desktop\gatling-charts-highcharts-bundle-2.3.1\user-files\simulations\computerdatabase\BasicSimulation.scala:40: not found: value Article_ID
18:42:54.584 [ERROR] i.g.c.ZincCompiler$ -       .get(s"${Article_ID}") // changet value from Article_ID.csv to Article_ID
18:42:54.584 [ERROR] i.g.c.ZincCompiler$ -                ^
18:42:54.635 [ERROR] i.g.c.ZincCompiler$ - two errors found
18:42:54.639 [ERROR] i.g.c.ZincCompiler$ - Compilation crashed


package computerdatabase

import io.gatling.core.Predef._
import io.gatling.http.Predef._

import scala.concurrent.duration._
import io.gatling.jdbc.Predef._

class BasicSimulation extends Simulation {

  val httpConf = http
    .baseURL("http://my_link.com") // Here is the root for all relative URLs and this is example, this is not real link;
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // Here are the common headers
    .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 headers_10 = Map("Content-Type" -> "application/x-www-form-urlencoded") // Note the headers specific to a given request
  val scn = scenario("Scenario Name") // A scenario is a chain of requests and pauses

  feed(csv(Article_ID.csv))

    .exec(http("Request")
      .get(s"${Article_ID}") // changet value from Article_ID.csv to Article_ID
    .pause(7))

  setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}

3 个答案:

答案 0 :(得分:1)

上面的代码会产生一个空的情况(我认为不应编译)。这是因为feed()方法未在方案构建器链中使用,而是单独使用。您需要做的就是将所有步骤称为fe链。

val scn = scenario("Scenario Name")
  .feed(csv(Article_ID.csv))
  .exec(http("Request"))
    .get(s"${Article_ID}")
  .pause(7))

如果这不是问题,并且您刚刚粘贴了错误代码,则检查CSV文件格式是否正确。 CSV的第一行应包含属性名称(我知道人们通常会忘记它),例如:

Article_ID, OtherColumn, AnotherColumn
1, Test, Lorem Ipsum
2, Abc, Dolor Sit Amet
3, Xyz, Consectetur Adipiscing

答案 1 :(得分:0)

  

我的解决方案

   object Article {
        val feeder = csv("search.csv").random // randomly id from the csv file

        val search = feed(feeder)
          .exec(http("unike_Article") // execute your request 
            .get("/article/88.8888/${searchCriterion}") 
          )
          .pause(2)
  }

  val users = scenario("Users").exec(Article.search)

  setUp(users.inject(rampUsersPerSec(2)to(20)during(3 minutes)).protocols(httpConf))

}

答案 2 :(得分:0)

这是一个完全有效的解决方案,基于this post,目标URL为http://localhost:8080/cat?name=XYZ,而猫的名字来自带有name列的CSV文件:

import java.util.concurrent.TimeUnit
import io.gatling.core.Predef.{Simulation, scenario, _}
import io.gatling.http.Predef.{http, status}
import scala.concurrent.duration.FiniteDuration

class MySimulation extends Simulation{

val baseURL = "http://localhost:8080"
val httpConf = http.baseUrl(baseURL)
val csvFeeder = csv("cats.csv").random

val scn = scenario("my cats")
    .feed(csvFeeder)
    .exec(http("my cats")
    .get(baseURL + "/cat")
    .queryParam("name", "${name}"))

setUp(
    scn.inject(
    constantUsersPerSec(100) during (FiniteDuration(10,TimeUnit.SECONDS)),
    )
    .protocols(httpConf))
}
相关问题