Mykola Rudenko
Mykola Rudenko

Reputation: 314

How to inject data from csv file in Gatling?

I have a problem with the implementation of Gatling scenario. It's on Scala DSL, maybe somebody already had this issue?

The goal: we need to Inject data in out scenario. Basically, we have - our page http://ourPage.com/ like the root for all relative URLs - the list of URLs in CSV file, these URLs basically are 650000 id from our Oracle database, that in combination (root+Urls) will simulate the number of users that we choose.

How to inject data from CSV file in Gatling?

Include that file exist in the right directory (data) and has right data inside

Hopefully my message understandable I will appreciate any kind of help

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))
}

Upvotes: 2

Views: 8484

Answers (3)

riorio
riorio

Reputation: 6836

Here is a fully working solution, that is based on this post, where the target URL is http://localhost:8080/cat?name=XYZ and the names of the cats are coming from a CSV file with a name column:

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))
}

Upvotes: 0

Mykola Rudenko
Mykola Rudenko

Reputation: 314

My solution

   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))

}

Upvotes: 2

Mateusz Gruszczynski
Mateusz Gruszczynski

Reputation: 1510

Well that code above produces an empty scenario (and I think it should not compile). Its because feed() method is not used in scenario builder chain but separately. What you need to do is to call all the steps as chain fe.:

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

If it is not the issue and you just pasted code with error then check if CSV file has proper format. First line of CSV should contain attribute names (I know often people forget about it) fe.:

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

Upvotes: 3

Related Questions