Skip to content

Modifying Feed Data

Christopher Bradford edited this page Oct 17, 2018 · 3 revisions

Often times it's required to modify feed data inside of the scenario being run before using in a query. An example of this is converting a CSV feeder value into a Solr Query to be sent to the Cluster.

Gatling Execs

Gatling provides two types of exec statements the standard exec() is when running Queries or Actions. The other is a special type of exec{} that enables access to the Gatling Users session and the ability to change the values in the Session.

One thing that is important to remember Gatling Sessions are immutable in that access to the existing is possible, but to add a new value to the Session a new Session with all values included must be returned. session.setAll() function should be the only setter function used in an exec method. Additionally a Gatling session type must be the return type of the exec{} method.

Using Custom Code Exec

The following is a scenario that uses a CSV feeder that includes the column realm_id which is needed to be included in a CQL Solr Query as part of the actions.queryWithSolr method.

    val solr = scenario("SolrQuery")
        .feed(csvFeeder)
        .exec { session =>
          val realmId = session.attributes("realm_id")
          session.setAll(Map("solr_query" -> new SolrQueryBuilder().withQuery("realmId:" + realmId.toString).buildCql)
        }
        .exec(actions.queryWithSolr)

The exec below reads the value of realm_id out of the session populated by the feeder before it defined by .exec { session => . Because Session attributes are stored in Scala's Any type it's important to convert the session variable to the correct type the the feed set it in or read as a string (which calls .toString) and convert manually.

This example also uses the SolrQueryBuilder helper to easily create a CQL or HTTP based Solr Query from the realm_id string.

.exec { session =>
     val realmId = session.attributes("realm_id").as[String]
     val newSession = session.attributes ++ 
           Map("solr_query" -> new SolrQueryBuilder().withQuery("realm_id:" + realmId).buildCql)
     session.setAll(newSession)
}

Session attributes are stored as Map[String, Any] this allows new attribute maps to be added to the existing attributes which is what newSession variable is doing.

Finally the session.setAll(newSession) is setting all of the attributes found in the newSession variable to the Users session to be used in the .exec(actions.queryWithSolr) query.

For reference below is the code for actions.queryWithSolr that the above exec{} method is editing the session for.

 private val solrQuery = QueryBuilder.select().from(keyspace, tables.trips)
      .where(QueryBuilder.eq("solr_query", raw(":solr_query")))


  private val solrQueryPreparedStatement = session.prepare(solrQuery)

  def queryWithSolr = {
    exec(
      cql("SolrQuery")
          .executeNamed(solrQueryPreparedStatement)
          .consistencyLevel(ConsistencyLevel.LOCAL_ONE)
          .check(rowCount greaterThan 0)
    )
  }